// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The Microsoft Graph delegated permissions this library can use, written out //! so that a typo is a compile error rather than a consent screen that refuses. //! //! ## Two spellings, one meaning //! //! A permission may be written in full -- `https://graph.microsoft.com/Mail.Read` //! -- or bare, as `Mail.Read`. The identity platform treats them as the same //! thing: if the resource identifier is left off the `scope` parameter, the //! resource is assumed to be Microsoft Graph. The full form is what is written //! here, because a program that also talks to Outlook's own API or to Key Vault //! has to name the resource anyway, and a bare `Mail.Read` beside a //! `https://vault.azure.net/...` is a request for two resources at once, which //! is refused. //! //! `bare` converts, for the places where the short form reads better. //! //! ## `offline_access` is not optional //! //! This is the one that catches everybody. On the v2.0 endpoint an application //! **must explicitly request `offline_access`** to be issued a refresh token. //! Without it the grant dies in about an hour and the next run has to send the //! user back to a browser. It is the Microsoft equivalent of Google's //! `access_type=offline`, and `mirror` below includes it for that reason. //! //! ## Ask for the narrowest set that works //! //! A refresh token is only as dangerous as the scopes attached to it, and these //! are not all the same size. `Mail.ReadBasic` cannot see the body of a //! message; `Mail.Read` can see everything in the mailbox; `Mail.ReadWrite` can //! also delete it. The `.Shared` variants reach into mailboxes somebody else //! has delegated, which is a larger grant than it looks. const std = @import("std"); /// The resource these permissions belong to. Everything below is this with a /// permission appended. pub const resource = "https://graph.microsoft.com"; // -- OpenID Connect --------------------------------------------------------- /// Sign the user in and get a `sub` claim. Required for an ID token. pub const openid = "openid"; /// The user's primary email address, as an `email` claim. Not always present: /// an account need not have one. pub const email = "email"; /// Name, preferred username and object id, in the ID token. pub const profile = "profile"; /// **Required for a refresh token.** Without it the access token is all there /// is, it lasts about an hour, and there is no way to get another without /// sending the user back to a browser. pub const offline_access = "offline_access"; // -- the user --------------------------------------------------------------- /// Sign in and read the signed-in user's own profile. What `Client.getUser` /// needs. pub const user_read = resource ++ "/User.Read"; // -- mail ------------------------------------------------------------------- /// Read mail *except* its content: no `body`, no `bodyPreview`, no /// `uniqueBody`, no attachments, no extensions. /// /// The narrowest scope that can walk a mailbox and see who a message is from /// and what it is called. It is enforced rather than advisory, and the MIME /// endpoint is part of what it cannot reach -- a `$value` fetch under this /// scope is refused. pub const mail_read_basic = resource ++ "/Mail.ReadBasic"; /// Read everything in the signed-in user's mailbox, bodies and attachments /// included. What a program that mirrors a mailbox to local storage needs. pub const mail_read = resource ++ "/Mail.Read"; /// Read, create, update and delete mail. A superset of `mail_read`, and the /// one to justify to yourself before asking for it: it can empty a mailbox. pub const mail_read_write = resource ++ "/Mail.ReadWrite"; /// Send mail as the signed-in user. Cannot read anything. pub const mail_send = resource ++ "/Mail.Send"; /// Read mail in mailboxes the signed-in user has delegate access to, as well /// as their own. A larger grant than it looks: which mailboxes those are is /// decided elsewhere and can change without this application being told. pub const mail_read_shared = resource ++ "/Mail.Read.Shared"; pub const mail_read_write_shared = resource ++ "/Mail.ReadWrite.Shared"; pub const mail_send_shared = resource ++ "/Mail.Send.Shared"; // -- mailbox settings ------------------------------------------------------- /// Time zone, working hours, automatic replies and language. Worth having /// when rendering a `dateTimeTimeZone`, and not otherwise. pub const mailbox_settings_read = resource ++ "/MailboxSettings.Read"; pub const mailbox_settings_read_write = resource ++ "/MailboxSettings.ReadWrite"; // -- sets ------------------------------------------------------------------- /// What a program that mirrors a mailbox to local storage needs: the messages /// and their MIME content, the signed-in user's address to file them under, /// and a refresh token so that it works twice. pub const mirror: []const []const u8 = &.{ openid, offline_access, user_read, mail_read, }; /// The same walk without the message content, for a program that only needs to /// know what exists: an index, a report, a duplicate finder. pub const index_only: []const []const u8 = &.{ openid, offline_access, user_read, mail_read_basic, }; /// The permission with the resource identifier stripped, for the places the /// short spelling reads better. `Mail.Read` out of /// `https://graph.microsoft.com/Mail.Read`; an OpenID Connect scope, which has /// no resource, is returned unchanged. pub fn bare(scope: []const u8) []const u8 { if (!std.mem.startsWith(u8, scope, resource ++ "/")) return scope; return scope[resource.len + 1 ..]; } /// Whether a granted scope list covers `wanted`, in either spelling. /// /// Worth calling after signing in, because the identity platform grants what /// the administrator has consented to rather than what was asked for: a /// request for `Mail.Read` in a tenant that permits only `Mail.ReadBasic` /// succeeds and comes back with the narrower one. Finding that out here is /// better than finding it out as a 403 forty thousand messages into a walk. pub fn granted(scopes: []const []const u8, wanted: []const u8) bool { const short = bare(wanted); for (scopes) |s| { // Case-insensitively: the identity platform echoes back the casing of // its own registration, which is not always the casing that was sent. if (std.ascii.eqlIgnoreCase(bare(s), short)) return true; } return false; } const testing = std.testing; test "the Graph permissions name their resource and each is distinct" { const all = [_][]const u8{ user_read, mail_read_basic, mail_read, mail_read_write, mail_send, mail_read_shared, mail_read_write_shared, mail_send_shared, mailbox_settings_read, mailbox_settings_read_write, }; for (all, 0..) |a, i| { try testing.expect(std.mem.startsWith(u8, a, resource ++ "/")); for (all[i + 1 ..]) |b| try testing.expect(!std.mem.eql(u8, a, b)); } } test "the OpenID Connect scopes carry no resource" { for ([_][]const u8{ openid, email, profile, offline_access }) |s| { try testing.expect(std.mem.indexOfScalar(u8, s, '/') == null); // And `bare` leaves them alone rather than mangling them. try testing.expectEqualStrings(s, bare(s)); } } test "bare strips the resource" { try testing.expectEqualStrings("Mail.Read", bare(mail_read)); try testing.expectEqualStrings("User.Read", bare(user_read)); // Something belonging to another resource is not this one's to strip. try testing.expectEqualStrings( "https://vault.azure.net/user_impersonation", bare("https://vault.azure.net/user_impersonation"), ); } test "a mirroring app asks for a refresh token" { // The whole reason `mirror` exists rather than being spelled out at each // call site: leaving `offline_access` off is silent, and the program works // perfectly for an hour. try testing.expect(granted(mirror, offline_access)); try testing.expect(granted(mirror, mail_read)); try testing.expect(granted(index_only, offline_access)); } test "granted matches either spelling and either casing" { const from_the_wire: []const []const u8 = &.{ "openid", "profile", "Mail.ReadBasic", "User.Read", "offline_access", }; // Asked for in full, granted in short: the same permission. try testing.expect(granted(from_the_wire, mail_read_basic)); try testing.expect(granted(from_the_wire, user_read)); // Asked for `Mail.Read`, granted `Mail.ReadBasic` -- which is what a // tenant that will not consent to the wider one does, and is exactly the // case worth checking for after signing in. try testing.expect(!granted(from_the_wire, mail_read)); const odd_casing: []const []const u8 = &.{"mail.read"}; try testing.expect(granted(odd_casing, mail_read)); }