// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What `GET /me` returns: who the token belongs to. //! //! It is the cheapest call Graph has, which makes it the way to check that a //! token works before starting anything long. For a program that files mail it //! is also where the address to file it under comes from -- and that is less //! obvious than it looks, which is why `address` exists. //! //! The strings are borrowed from the arena of whatever parsed them, so a `User` //! must not outlive it. const User = @This(); const std = @import("std"); const json = @import("json.zig"); /// The object id: a GUID, immutable, and the only member worth storing. /// Everything else about a user can be changed by an administrator, including /// both of the things that look like identifiers below. id: []const u8, /// The primary SMTP address, and `null` for an account that has no mailbox -- /// which is not exotic: a guest account, or a licence that does not include /// Exchange. Code that assumes this is present is code that crashes on the /// first user without a mailbox. mail: ?[]const u8 = null, /// What the user signs in as. Usually an email address and usually the same as /// `mail`, but it is a *sign-in* name rather than a mailbox: the two differ /// whenever the primary address has been changed without changing the login, /// which is what happens when an organisation is renamed. user_principal_name: ?[]const u8 = null, display_name: ?[]const u8 = null, given_name: ?[]const u8 = null, surname: ?[]const u8 = null, job_title: ?[]const u8 = null, office_location: ?[]const u8 = null, /// An IETF language tag such as `en-US`. `null` far more often than not. preferred_language: ?[]const u8 = null, pub const ParseError = error{Malformed} || std.mem.Allocator.Error; pub fn fromJson(value: std.json.Value) ParseError!User { if (value != .object) return error.Malformed; return .{ .id = json.string(value, "id") orelse return error.Malformed, // `nonEmptyString`, because Graph writes `null` for a user with no // mailbox but has also been seen to write `""`, and an empty address // is not an address. .mail = json.nonEmptyString(value, "mail"), .user_principal_name = json.nonEmptyString(value, "userPrincipalName"), .display_name = json.string(value, "displayName"), .given_name = json.string(value, "givenName"), .surname = json.string(value, "surname"), .job_title = json.string(value, "jobTitle"), .office_location = json.string(value, "officeLocation"), .preferred_language = json.string(value, "preferredLanguage"), }; } /// The address to file this mailbox under: `mail`, or the sign-in name when /// there is no mailbox address, or `null` when there is neither. /// /// The order matters. `mail` is the mailbox and `userPrincipalName` is the /// login, and where they differ it is the mailbox that the messages were /// addressed to. pub fn address(u: *const User) ?[]const u8 { return u.mail orelse u.user_principal_name; } /// A name for a person, for a log line or a prompt: the display name, or the /// address, or the object id -- something is always returned. pub fn label(u: *const User) []const u8 { if (u.display_name) |d| if (d.len > 0) return d; return u.address() orelse u.id; } const testing = std.testing; fn parse(text: []const u8) !std.json.Parsed(std.json.Value) { return std.json.parseFromSlice(std.json.Value, testing.allocator, text, .{}); } test "the shape /me returns" { var p = try parse( \\{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users/$entity", \\ "businessPhones":["+1 425 555 0109"],"displayName":"Adele Vance", \\ "givenName":"Adele","jobTitle":"Retail Manager","mail":"AdeleV@contoso.com", \\ "mobilePhone":null,"officeLocation":"18/2111","preferredLanguage":"en-US", \\ "surname":"Vance","userPrincipalName":"AdeleV@contoso.com", \\ "id":"87d349ed-44d7-43e1-9a83-5f2406dee5bd"} ); defer p.deinit(); const u: User = try .fromJson(p.value); try testing.expectEqualStrings("87d349ed-44d7-43e1-9a83-5f2406dee5bd", u.id); try testing.expectEqualStrings("AdeleV@contoso.com", u.mail.?); try testing.expectEqualStrings("Adele Vance", u.display_name.?); try testing.expectEqualStrings("Retail Manager", u.job_title.?); try testing.expectEqualStrings("AdeleV@contoso.com", u.address().?); try testing.expectEqualStrings("Adele Vance", u.label()); // Members this does not model -- `businessPhones`, `mobilePhone` -- are // ignored rather than being an `UnknownField`, which is the whole reason // nothing here parses into a struct. } test "an account with no mailbox falls back to the sign-in name" { var p = try parse( \\{"id":"abc","mail":null,"userPrincipalName":"guest_example.com#EXT#@contoso.com", \\ "displayName":"A Guest"} ); defer p.deinit(); const u: User = try .fromJson(p.value); try testing.expectEqual(@as(?[]const u8, null), u.mail); try testing.expectEqualStrings("guest_example.com#EXT#@contoso.com", u.address().?); } test "an empty address is not an address" { var p = try parse("{\"id\":\"abc\",\"mail\":\"\",\"userPrincipalName\":\"\"}"); defer p.deinit(); const u: User = try .fromJson(p.value); try testing.expectEqual(@as(?[]const u8, null), u.address()); // And there is still something to put in a log line. try testing.expectEqualStrings("abc", u.label()); } test "the mailbox wins over the login where they differ" { // What an organisation rename leaves behind: the mail was moved to the new // domain and the sign-in name was not. var p = try parse( \\{"id":"abc","mail":"jeff@newname.com","userPrincipalName":"jeff@oldname.onmicrosoft.com"} ); defer p.deinit(); const u: User = try .fromJson(p.value); try testing.expectEqualStrings("jeff@newname.com", u.address().?); } test "no id at all is malformed" { var p = try parse("{\"mail\":\"a@b\"}"); defer p.deinit(); try testing.expectError(error.Malformed, User.fromJson(p.value)); var q = try parse("[]"); defer q.deinit(); try testing.expectError(error.Malformed, User.fromJson(q.value)); }