Something went wrong. Try again.
A Microsoft 365 mail client library for Zig, over Microsoft Graph
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// 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));}