Skip to content

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.

  1. Register https://<your api host>/auth/social/microsoft/callback as a web redirect URI of the app registration.
  2. Add the optional claim xms_edov to the ID token of the app registration.
  3. Copy the application (client) id and the client secret to your secret store.
  4. Set social.microsoft with clientId, clientSecret, and tenant.
  5. Link to /auth/social/microsoft/start?next=/home from the sign-in page.
The Microsoft preset in the config
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",
},
},
});
}
The start as a test
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");
});
});
  • tenant is common (every kind of account), organizations (work and school), consumers (personal), or the GUID of one tenant.
  • Each token must name its own tenant: its iss must be the issuer of its tid. A token of another tenant than the GUID of tenant answers oauth_failed.
  • organizations refuses a personal account, and consumers takes only a personal account.
  • The subject is oid, the key that Better Auth keeps. Microsoft gives one oid to each app of a tenant. A Better Auth account row of the provider microsoft links 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 the email claim is not sure to be correct and can change.
  • So without xms_edov, an Entra ID address links no user that exists. The callback answers email_taken_unverified for an address that another user holds. This stops the nOAuth attack.
  • The scope is openid profile email.
  • An address with no xms_edov can 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_edov reaches the token.
  • oauth_failed: the token named another tenant, the token call failed, or the claims are bad. Check tenant, 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 no xms_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.