Repository navigation
feat(auth): let editors sign in with a Microsoft work account - #3254
Conversation
Companies that run on Microsoft 365 have no GitHub or Google identity for their editors, and Cloudflare Access replaces every other login method and only runs on Cloudflare. microsoft() adds Entra ID as a built-in provider next to github() and google(), on the shared OAuth routes with PKCE. Entra sends no email_verified claim, so the provider reads the profile from the ID token, verified against the tenant's published keys, and decides verification itself. With a directory (tenant) ID, an account that signs in through that directory counts as verified when its address is in the domain of its sign-in name, which Entra allows only in the directory's verified domains, or when the optional xms_edov claim confirms the domain. A directory admin can set a member's email to any domain, and Microsoft passes it to single-tenant apps unfiltered, so trusting it would let auto-linking hand over an existing EmDash user. Guests, identified by an idp claim that differs from the issuer, and sign-ins through common, organizations or consumers do not count as verified. The emailVerified option overrides this. Because allowed-domain self-signup does not check the flag for other providers, the Microsoft provider asks the consumer to refuse self-signup for an unverified address, so a multi-tenant setup cannot hand out a role for an address the tenant does not control. Providers opt into the ID-token path through a new verifyIdToken hook on OAuthProvider, which also makes the authorization request carry a nonce. jose joins @emdash-cms/auth for the signature check, at the version core already uses. Implements discussion emdash-cms#1626.
🦋 Changeset detectedLatest commit: 2fe1e35 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Scope checkThis PR changes 943 lines across 19 files. Large PRs are harder to review and more likely to be closed without review. If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs. See CONTRIBUTING.md for contribution guidelines. |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
This PR is the right approach for #1626: it adds Microsoft Entra ID as a built-in OAuth provider, reuses the existing GitHub/Google route machinery, and correctly treats Entra as OIDC by verifying the ID token locally instead of relying on a provider-supplied email_verified claim. The tenant/domain verification logic is careful about guests, multi-tenant segments, and admin-set email addresses.
I checked the new provider implementation (packages/auth/src/oauth/providers/microsoft.ts), the OAuth consumer changes, the Astro start/callback routes, the admin button/virtual-module wiring, the unit and route tests, and the docs/changeset. Overall the implementation is clean and well-tested, but I found one correctness issue in the xms_edov claim handling and two polish items in the callback error mapping and changeset.
Headline issues
xms_edovis currently accepted only as the booleantrue, but Microsoft typically returns this optional claim as a string ("true"/"false"). Users who add the claim and rely on it for cross-domain verification will be rejected as unverified.- The OAuth callback error switch does not handle the new
id_token_invalidcode added by this PR, so nonce/signature/tenant failures fall back to a generic message. - The changeset ends with an internal-API paragraph that is more appropriate for the PR description than a CHANGELOG entry.
No other blocking concerns: public routes, virtual-module registration, jose/workerd pre-bundling, state-store nonce handling, and SQL safety are all correct.
Findings
-
[needs fixing]
packages/auth/src/oauth/providers/microsoft.ts:134-141The domain-ownership check only treats
xms_edov === trueas verified. Microsoft returns this optional claim as a string by default (e.g."true"/"false") unless the app registration explicitly requests a JSON format, so Entra users who configure the claim and have an email in a different domain from their UPN will still be rejected as unverified.Accept the claim in the forms Microsoft actually emits:
const xmsEdovVerified = claims.xms_edov === true || claims.xms_edov === "true" || claims.xms_edov === 1; const domainOwned = claims.email === undefined || domainOf(claims.email) === domainOf(claims.preferred_username) || xmsEdovVerified;
Add a test case for
xms_edov: "true"and for false-ish string values. -
[suggestion]
packages/core/src/astro/routes/api/auth/oauth/[provider]/callback.ts:282-288The new
id_token_invaliderror code introduced by the Microsoft provider falls through to the genericdefaultcase, so users who hit a nonce mismatch, bad signature, or wrong tenant get "Authentication failed" instead of a more actionable message.Add a case before
user_not_found:case "id_token_invalid": message = "Could not verify your identity with Microsoft. Please try again."; break;
-
[suggestion]
.changeset/microsoft-login-provider.md:16The last paragraph lists internal API signature changes (
createAuthorizationUrl,handleOAuthCallback,findOrCreateOAuthUser,isValidMicrosoftTenant) that are not useful to someone deciding whether to upgrade. A changeset should focus on user-facing behavior; internal mechanics belong in the PR description.Consider trimming the final paragraph to:
`@emdash-cms/auth` gains the underlying Microsoft provider and tenant validation used by the new login flow.
Microsoft documents xms_edov as a boolean, but its encoding in tokens is reported inconsistently, and a strict `=== true` would leave an address unverified for a site that added the claim precisely to verify it. The provider now counts true, "true", 1 and "1"; every other value, the false spellings included, still leaves the address unverified. The callback maps id_token_invalid to its own message instead of the generic "Authentication failed", like every other OAuth error code.
There was a problem hiding this comment.
This is the right approach for #1626: it adds Microsoft Entra ID as a built-in OAuth provider, reuses the existing GitHub/Google route machinery, and correctly treats it as OIDC by verifying the ID token locally. The tenant, guest, and domain-ownership logic is carefully modeled.
Re-reviewing after the previous feedback: the two functional issues are fixed. xms_edov now accepts boolean, string ("true", "1"), and number (1) forms, with tests for both truthy and false-ish variants. The callback route now maps id_token_invalid to a user-friendly message instead of falling through to the generic case.
One issue from the previous review is still open: the changeset ends with a paragraph that lists internal @emdash-cms/auth API signatures rather than user-facing behavior. A changeset lands in the CHANGELOG, so implementation mechanics belong in the PR description, not the release notes.
I also reviewed the new docs prose in guides/authentication.mdx, reference/configuration.mdx, and deployment/secrets.mdx against the implementation and found the technical claims accurate.
No other blocking concerns: public routes, virtual-module registration, jose dependency alignment between emdash and @emdash-cms/auth, state-store nonce handling, algorithm restriction to RS256, and SQL safety are all correct.
The functions it named are exported for the sibling packages and are not documented anywhere, so they tell a CHANGELOG reader nothing they can act on. The changeset lists both packages, so the remaining text becomes the @emdash-cms/auth entry too, the way emdash-cms#1868 read there.
There was a problem hiding this comment.
This is the right approach for adding Microsoft Entra ID to EmDash's built-in OAuth providers: it reuses the existing GitHub/Google route machinery, treats Microsoft as OIDC by verifying the ID token locally, and carefully models tenant, guest, and domain-ownership semantics.
Re-reviewing after the previous feedback: the changeset issue is resolved. The final paragraph of internal @emdash-cms/auth API mechanics has been dropped, leaving a user-facing entry that describes the provider, required environment variables, tenant behavior, verification rules, and the emailVerified override.
I also spot-checked the functional fixes from the previous round:
xms_edovnow accepts boolean, string ("true","1"), and number (1) truthy forms, with tests for false-ish variants.- The callback route maps
id_token_invalidto a user-friendly message instead of falling through to the generic case.
I read through the new Microsoft provider implementation, the OAuth consumer hooks (verifyIdToken, requireVerifiedEmailForSignup), the Astro OAuth routes, the virtual provider descriptor, the admin login button, and the docs. I traced the verification flow: nonce generation and storage, ID token verification restricted to RS256, issuer/tenant/nonce checks, domain-ownership and guest detection, and the emailVerified override.
Nothing blocking remains. Public routes, virtual-module registration, jose dependency alignment between emdash and @emdash-cms/auth, state-store nonce handling, algorithm restriction, SQL safety, and the verified-email guards for linking/self-signup/first-admin are all correct. The route tests cover member signup, guest refusal, the override, domain mismatch, and nonce mismatch. Docs claims match the implementation.
LGTM — no new findings.
|
@Rimander tried the pkg.pr.new build on one of their sites and shared the result in the discussion: sign-in works, and signing in with Microsoft found a user who already existed. Thanks! I'm copying it here so it's on the PR. That existing-user part is the bit I couldn't check myself. The tests only cover a link being refused, and on my own tenant I only signed up new users. EmDash links a Microsoft sign-in to an existing user only when it trusts the email address, so I'd like to know what made it trust yours. Could you check these?
There's one more possibility. If your local version already named the provider No need to test guests or users whose email is in another domain. |
…-provider Conflicts: - docs/src/content/docs/deployment/secrets.mdx: keep the Microsoft sign-in row and drop the marketplace token row that emdash-cms#3501 removed. - callback.ts: keep the session-driver import from emdash-cms#3445 next to the Microsoft provider type. The Microsoft route test called the callback without a session, which the emdash-cms#3445 guard now rejects before sign-in; it passes a session stub like the other sign-in route tests.
Vitest 4 takes two arguments for vi.doMock, and core's Database type does not satisfy Kysely<AuthTables> because of its Generated<> columns, so tsc rejected both lines. Core's tests sit outside every tsconfig, so neither pnpm typecheck nor CI reported it. The cast matches src/cleanup.ts; the virtual module mocks without the removed option, as in the middleware tests.
Overlapping PRsThis PR modifies files that are also changed by other open PRs:
This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
What does this PR do?
Adds
microsoft(), a built-in login provider for Microsoft Entra ID, so editors at companies that run on Microsoft 365 can sign in with their work account next to passkeys.Entra sends no
email_verified, and a directory admin can set a member's email to any domain, which Microsoft passes to single-tenant apps unfiltered. The provider therefore reads the profile from the verified ID token and decides itself. With a directory (tenant) ID, an account that signs in through that directory counts as verified when its address is in the domain of its sign-in name, which Entra allows only in the directory's verified domains, or when the optionalxms_edovclaim confirms the domain, whether the claim arrives as a boolean, a string or a number. Guests, accounts from other identity providers and multi-tenant sign-ins do not count as verified, andmicrosoft({ emailVerified })overrides either way. An unverified Microsoft address cannot link to an existing user, take a self-signup through an allowed domain, or create the first admin account. GitHub and Google behave as before.The ID token path is a new optional
verifyIdTokenhook onOAuthProvider; a provider that sets it also gets a nonce in the authorization request, and a token that fails verification shows its own sign-in error instead of the generic one.josejoins@emdash-cms/authat core's version. In@emdash-cms/auth,createAuthorizationUrl()andhandleOAuthCallback()accept"microsoft"with aMicrosoftOAuthConfig,isValidMicrosoftTenant()checks a tenant value, andfindOrCreateOAuthUser()takes arequireVerifiedEmailoption. None of these exports is documented, so the changeset describes only the feature.Implements #1626
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain. (n/a: the button's only text is the brand name "Microsoft", unwrapped like the GitHub and Google buttons)AI-generated code disclosure
Screenshots / test output
The login page of
demos/simplewithgithub(),google()andmicrosoft()enabled, at 1280×800:The same page in Arabic:
microsoft.test.tscovers each ID token check, the guest and domain rules,xms_edovas a boolean, string or number, and the override with locally signed tokens.oauth-microsoft-route.test.tsruns both routes against SQLite with Microsoft's endpoints stubbed: member signup, guest refusal, the override, a member whose email is outside the directory's domains, and a foreign nonce. Breaking each guard in turn failed at least one test.Against a real Entra tenant, a cloud-only member signed up through an allowed domain with the address from
preferred_username. An account homed in a personal Microsoft account was refused untilemailVerified: true.