Sign in with Microsoft
For: an app developer whose users have a Microsoft work, school, or personal account.
What it does: social.microsoft adds a “Continue with Microsoft” button on the v2.0 endpoints of Entra ID. Core finds or makes the user by the oid of the token.
Before you rely on this preset, sign in once with a real account.
Before you start
Section titled “Before you start”- A working social sign-in from Sign in with Google and Apple.
- An app registration in Entra ID with a client secret, for
social.microsoft.
- Register
https://<your api host>/auth/social/microsoft/callbackas a web redirect URI of the app registration. - Add the optional claim
xms_edovto the ID token of the app registration. - Copy the application (client) id and the client secret to your secret store.
- Set
social.microsoftwithclientId,clientSecret, andtenant. - Link to
/auth/social/microsoft/start?next=/homefrom the sign-in page.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createMicrosoftAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, social: { // Read the secret from your secret store. The placeholders only build the config. microsoft: { clientId: "00000000-0000-4000-8000-000000000001", clientSecret: "microsoft-client-secret", // "common", "organizations", "consumers", or the GUID of your tenant. tenant: "organizations", }, }, });}import { describe, expect, it } from "vitest";import { call, harness } from "../support/harness.ts";import { createMicrosoftAuth } from "./microsoft.ts";
describe("sign in with Microsoft Entra ID", () => { it("sends the person to the provider with the scope and a PKCE challenge", async () => { const auth = createMicrosoftAuth(harness()); const res = await call(auth, "GET", "/auth/social/microsoft/start?next=/home"); expect(res.status).toBe(302); const to = new URL(res.headers.get("location") ?? ""); expect(to.origin + to.pathname).toBe( "https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize", ); expect(to.searchParams.get("scope")).toBe("openid profile email"); expect(to.searchParams.get("redirect_uri")).toBe( "https://api.example.com/auth/social/microsoft/callback", ); expect(to.searchParams.get("code_challenge_method")).toBe("S256"); expect(to.searchParams.get("client_id")).toBe("00000000-0000-4000-8000-000000000001"); });});How it behaves
Section titled “How it behaves”tenantiscommon(every kind of account),organizations(work and school),consumers(personal), or the GUID of one tenant.- Each token must name its own tenant: its
issmust be the issuer of itstid. A token of another tenant than the GUID oftenantanswersoauth_failed. organizationsrefuses a personal account, andconsumerstakes only a personal account.- The subject is
oid, the key that Better Auth keeps. Microsoft gives oneoidto each app of a tenant. A Better Authaccountrow of the providermicrosoftlinks with no change. - The preset uses the global cloud of Entra ID,
login.microsoftonline.com. It serves no sovereign cloud, such as the US Government cloud or the China cloud. - An address is verified only with
xms_edov: true. Microsoft says theemailclaim is not sure to be correct and can change. - So without
xms_edov, an Entra ID address links no user that exists. The callback answersemail_taken_unverifiedfor an address that another user holds. This stops the nOAuth attack. - The scope is
openid profile email.
Residuals
Section titled “Residuals”- An address with no
xms_edovcan fill a new user, unverified. That user then proves the address by mail before it can do what a verified address allows. - Before you go live, sign in once with a work account of one tenant and once with a personal account. Check that
xms_edovreaches the token.
If it fails
Section titled “If it fails”oauth_failed: the token named another tenant, the token call failed, or the claims are bad. Checktenant, the client id, and the secret.oauth_denied: the person refused at Microsoft, or the tenant needs the consent of an admin.email_taken_unverified: another user holds the address, and the token has noxms_edov: true.invalid_token: the state is spent or old, or the callback came to another browser. Start again.sign_up_closed: the sign-up rule refused a new user.