Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Accept Microsoft's domain owner verified claim for personal accounts (#3389)
Luke Vellaa7d426cddff8
diff --git a/apps/docs/self-hosting/single-sign-on.mdx b/apps/docs/self-hosting/single-sign-on.mdxindex 02b3edc2f..8337fe927 100644--- a/apps/docs/self-hosting/single-sign-on.mdx+++ b/apps/docs/self-hosting/single-sign-on.mdx@@ -45,16 +45,18 @@ After creating the application, set the redirect URI to:https://<YOUR_DOMAIN>/api/auth/callback/microsoft-entra-id```-On a multi-tenant endpoint (`MICROSOFT_TENANT_ID` set to `common`, `organizations` or `consumers`) any tenant can assert any email address, so Rallly needs Microsoft to confirm the address before it creates an account from it. Microsoft does not include that information unless the app registration asks for it, so a multi-tenant registration must emit the verified email claims: without them every new Microsoft sign-in fails with "Your email address is not verified". Add the `verified_primary_email` and `verified_secondary_email` optional claims to the ID token: in the app registration open **Manifest** and add these two entries to the `optionalClaims.idToken` array, keeping any claims already there:+On a multi-tenant endpoint (`MICROSOFT_TENANT_ID` set to `common`, `organizations` or `consumers`) any tenant can assert any email address, so Rallly needs Microsoft to confirm the address before it creates an account from it. Microsoft does not include that information unless the app registration asks for it, so a multi-tenant registration must emit the verified email claims: without them every new Microsoft sign-in fails with "Your email address is not verified". Work and school accounts are vouched for by `verified_primary_email` and `verified_secondary_email`; personal Microsoft accounts (Outlook, Hotmail, Live) are vouched for by `xms_edov`, which Microsoft only emits alongside the `email` claim. Add all four as optional claims on the ID token. In the app registration open **Token configuration**, choose **Add optional claim**, pick the **ID** token type and tick `email`, `verified_primary_email`, `verified_secondary_email` and `xms_edov`. If the registration lives in an Azure AD B2C tenant, that blade is hidden; open **Manifest** instead and add these entries to the `optionalClaims.idToken` array, keeping any claims already there:```json+{ "name": "email", "source": null, "essential": false, "additionalProperties": [] },{ "name": "verified_primary_email", "source": null, "essential": false, "additionalProperties": [] },-{ "name": "verified_secondary_email", "source": null, "essential": false, "additionalProperties": [] }+{ "name": "verified_secondary_email", "source": null, "essential": false, "additionalProperties": [] },+{ "name": "xms_edov", "source": null, "essential": false, "additionalProperties": [] }```-If `optionalClaims` is `null`, replace it with `{ "idToken": [ ...the two entries... ] }`.+If `optionalClaims` is `null`, replace it with `{ "idToken": [ ...the four entries... ] }`.-Once the claims are present, a Microsoft sign-in whose address is not among the verified ones fails with "Your email address is not verified". Existing users are not affected. Single-tenant registrations do not need this.+Once the claims are present, a Microsoft sign-in whose address Microsoft does not vouch for fails with "Your email address is not verified". Existing users are not affected. Single-tenant registrations do not need this.</Accordion><ParamField path="MICROSOFT_TENANT_ID">diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.tsindex e77332568..70ee7e996 100644--- a/apps/web/src/lib/auth.ts+++ b/apps/web/src/lib/auth.ts@@ -50,6 +50,7 @@ import {LOCALE_COOKIE_NAME,LOCALE_COOKIE_OPTIONS,} from "@/lib/locale/constants";+import { isMicrosoftEmailVerified } from "@/lib/oauth/microsoft-claims";import { track } from "@/lib/posthog";import { getValueByPath } from "@/lib/utils/get-value-by-path";@@ -251,6 +252,12 @@ export const authLib = betterAuth({clientId: env.MICROSOFT_CLIENT_ID,clientSecret: env.MICROSOFT_CLIENT_SECRET,redirectURI: absoluteUrl("/api/auth/callback/microsoft-entra-id"),+ // better-auth only reads the verified email lists, which personal+ // Microsoft accounts do not carry; they are vouched for by+ // `xms_edov` instead.+ mapProfileToUser: (profile) => ({+ emailVerified: isMicrosoftEmailVerified(profile),+ }),}: undefined,},diff --git a/apps/web/src/lib/oauth/microsoft-claims.test.ts b/apps/web/src/lib/oauth/microsoft-claims.test.tsnew file mode 100644index 000000000..d531bf894--- /dev/null+++ b/apps/web/src/lib/oauth/microsoft-claims.test.ts@@ -0,0 +1,51 @@+import { describe, expect, it } from "vitest";+import { isMicrosoftEmailVerified } from "./microsoft-claims";++describe("isMicrosoftEmailVerified", () => {+ it("trusts the domain owner verified claim personal accounts carry", () => {+ expect(+ isMicrosoftEmailVerified({ email: "[email protected]", xms_edov: true }),+ ).toBe(true);+ });++ it("trusts an explicit email_verified claim", () => {+ expect(+ isMicrosoftEmailVerified({+ email: "[email protected]",+ email_verified: true,+ }),+ ).toBe(true);+ });++ it("trusts the verified email lists work accounts carry", () => {+ expect(+ isMicrosoftEmailVerified({+ email: "[email protected]",+ verified_primary_email: ["[email protected]"],+ }),+ ).toBe(true);+ expect(+ isMicrosoftEmailVerified({+ email: "[email protected]",+ verified_secondary_email: ["[email protected]", "[email protected]"],+ }),+ ).toBe(true);+ });++ it("refuses an address the lists do not contain", () => {+ expect(+ isMicrosoftEmailVerified({+ email: "[email protected]",+ verified_primary_email: ["[email protected]"],+ }),+ ).toBe(false);+ });++ it("refuses when Microsoft vouches for nothing", () => {+ expect(isMicrosoftEmailVerified({ email: "[email protected]" })).toBe(false);+ expect(+ isMicrosoftEmailVerified({ email: "[email protected]", xms_edov: false }),+ ).toBe(false);+ expect(isMicrosoftEmailVerified({ xms_edov: false })).toBe(false);+ });+});diff --git a/apps/web/src/lib/oauth/microsoft-claims.ts b/apps/web/src/lib/oauth/microsoft-claims.tsnew file mode 100644index 000000000..13616efbe--- /dev/null+++ b/apps/web/src/lib/oauth/microsoft-claims.ts@@ -0,0 +1,23 @@+// Microsoft vouches for an address in three ways: `email_verified` (rare),+// `xms_edov` (the domain owner verified it, which is what personal Microsoft+// accounts carry) or the `verified_*_email` lists (Entra work accounts). The+// last two are optional claims the app registration must request.+export function isMicrosoftEmailVerified(claims: {+ email?: string | null;+ email_verified?: boolean;+ xms_edov?: boolean;+ verified_primary_email?: string[];+ verified_secondary_email?: string[];+}) {+ if (claims.email_verified === true || claims.xms_edov === true) {+ return true;+ }+ const email = claims.email?.toLowerCase();+ if (!email) {+ return false;+ }+ return [+ ...(claims.verified_primary_email ?? []),+ ...(claims.verified_secondary_email ?? []),+ ].some((verified) => verified.toLowerCase() === email);+}