Skip to content

feat(auth): let editors sign in with a Microsoft work account - #3254

Merged
ascorbic merged 5 commits into
emdash-cms:mainfrom
danielmlr:feat/microsoft-auth-provider
Sep 29, 2026
Merged

ascorbic merged 5 commits into
emdash-cms:mainfrom
danielmlr:feat/microsoft-auth-provider

Conversation

@danielmlr

@danielmlr danielmlr commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

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.

import { microsoft } from "emdash/auth/providers/microsoft";

emdash({ authProviders: [microsoft()] });

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 optional xms_edov claim 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, and microsoft({ 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 verifyIdToken hook on OAuthProvider; 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. jose joins @emdash-cms/auth at core's version. In @emdash-cms/auth, createAuthorizationUrl() and handleOAuthCallback() accept "microsoft" with a MicrosoftOAuthConfig, isValidMicrosoftTenant() checks a tenant value, and findOrCreateOAuthUser() takes a requireVerifiedEmail option. None of these exports is documented, so the changeset describes only the feature.

Implements #1626

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main. (n/a: the button's only text is the brand name "Microsoft", unwrapped like the GitHub and Google buttons)
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: OIDC login with Azure (Entra ID) — enterprise #1626
  • I have included screenshots below if this PR changes the UI

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5, Claude Opus 5.5

Screenshots / test output

The login page of demos/simple with github(), google() and microsoft() enabled, at 1280×800:

The EmDash login page with a Microsoft button below the GitHub and Google buttons

The same page in Arabic:

The EmDash login page in Arabic, right to left, with the Microsoft button mirrored like the GitHub and Google buttons

microsoft.test.ts covers each ID token check, the guest and domain rules, xms_edov as a boolean, string or number, and the override with locally signed tokens. oauth-microsoft-route.test.ts runs 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 until emailVerified: true.

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-bot

changeset-bot Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2fe1e35

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
emdash Minor
@emdash-cms/auth Minor
@emdash-cms/cloudflare Minor
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/admin Minor
@emdash-cms/blocks Minor
create-emdash Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
@emdash-cms/plugin-embeds Patch

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

@github-actions

Copy link
Copy Markdown
Contributor

Scope check

This 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.

@pkg-pr-new

pkg-pr-new Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@3254

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@3254

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@3254

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@3254

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@3254

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@3254

emdash

npm i https://pkg.pr.new/emdash@3254

create-emdash

npm i https://pkg.pr.new/create-emdash@3254

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@3254

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@3254

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3254

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@3254

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@3254

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@3254

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3254

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@3254

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@3254

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@3254

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@3254

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@3254

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@3254

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@3254

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@3254

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@3254

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@3254

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@3254

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@3254

commit: 2fe1e35

@danielmlr danielmlr added the ci:run Approve and re-run CI on the PR's latest commit label Sep 21, 2026
@github-actions github-actions Bot removed the ci:run Approve and re-run CI on the PR's latest commit label Sep 21, 2026
@danielmlr
danielmlr marked this pull request as ready for review September 21, 2026 20:20
@github-actions github-actions Bot added the review/needs-review No maintainer or bot review yet label Sep 21, 2026
@danielmlr danielmlr added the bot:review Trigger an emdashbot code review on this PR label Sep 21, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_edov is currently accepted only as the boolean true, 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_invalid code 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-141

    The domain-ownership check only treats xms_edov === true as 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-288

    The new id_token_invalid error code introduced by the Microsoft provider falls through to the generic default case, 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:16

    The 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.

@emdashbot emdashbot Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed bot:review Trigger an emdashbot code review on this PR review/needs-review No maintainer or bot review yet labels Sep 21, 2026
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.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Sep 21, 2026
@danielmlr danielmlr added the ci:run Approve and re-run CI on the PR's latest commit label Sep 21, 2026
@github-actions github-actions Bot removed the ci:run Approve and re-run CI on the PR's latest commit label Sep 21, 2026
@danielmlr danielmlr added the ci:run Approve and re-run CI on the PR's latest commit label Sep 21, 2026
@github-actions github-actions Bot removed the ci:run Approve and re-run CI on the PR's latest commit label Sep 21, 2026
@danielmlr danielmlr added the bot:review Trigger an emdashbot code review on this PR label Sep 21, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread .changeset/microsoft-login-provider.md Resolved
@emdashbot emdashbot Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed bot:review Trigger an emdashbot code review on this PR review/needs-rereview New commits since the last review labels Sep 21, 2026
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.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Sep 22, 2026
@danielmlr danielmlr added the bot:review Trigger an emdashbot code review on this PR label Sep 22, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_edov now accepts boolean, string ("true", "1"), and number (1) truthy forms, with tests for false-ish variants.
  • The callback route maps id_token_invalid to 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.

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed bot:review Trigger an emdashbot code review on this PR review/needs-rereview New commits since the last review labels Sep 22, 2026
@danielmlr

Copy link
Copy Markdown
Member Author

@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?

  • Is your tenant ID your actual directory ID, not common or organizations?
  • Is the user's email in the same domain as the name they sign in with?
  • Did you set emailVerified in microsoft(), or add the xms_edov claim to the app registration?

There's one more possibility. If your local version already named the provider microsoft, that user might have been linked back then, and nothing new happened this time. If so, just let me know.

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.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review overlap and removed review/approved Approved; no new commits since needs-rebase labels Sep 27, 2026
@github-actions github-actions Bot mentioned this pull request Sep 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Sep 29, 2026
@ascorbic
ascorbic merged commit 5de3bdc into emdash-cms:main Sep 29, 2026
45 checks passed
@emdashbot emdashbot Bot mentioned this pull request Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants