Single sign-on
Sign in to the engine and to medhook.dev with your organization's identity provider over OIDC or SAML 2.0, with roles from its groups.
On this page
People can sign in with your organization's identity provider (IdP) instead of an engine password: Microsoft Entra ID, Okta, Ping, AD FS, Keycloak or any other OpenID Connect or SAML 2.0 provider. The IdP's groups decide each person's role, and the IdP's own policy (MFA, conditional access, offboarding) applies to every sign-in. The engine and medhook.dev each support both protocols.
sequenceDiagram
participant P as Person's browser
participant E as MedHook engine
participant I as Identity provider
P->>E: Continue with single sign-on
E-->>P: Redirect (OIDC: state, nonce, PKCE / SAML: AuthnRequest)
P->>I: Sign in, MFA
I-->>P: Code (OIDC) or signed assertion (SAML)
P->>E: Callback or POST to the ACS
E->>E: Verify, map groups to a role, create or update the person
E-->>P: Session (60 min idle, 12 h at most)How it works
OIDC uses the authorization code flow with PKCE (S256), a random state and a nonce. The engine reads the IdP's discovery document, redeems the code with the client secret, and checks the id_token: the signature against the IdP's published keys, the issuer, the audience, azp whenever it is present (and required with several audiences), the expiry and the nonce. Only RSA and EC signatures are accepted. Discovery, the key set and the token request go through the engine's outbound URL check and never follow a redirect; an IdP on a private network needs its host in SSRF_ALLOWLIST.
SAML is SP-initiated: the engine sends an AuthnRequest and the IdP posts a Response back to the Assertion Consumer Service (ACS). The assertion must be signed by a certificate you configured; an unsigned assertion is refused even inside a signed Response. Only SHA-256 or stronger signatures and digests are accepted (no SHA-1). The engine also checks the Audience, the validity window (60 seconds of clock skew), the Issuer, the Response Destination, and, inside the signed assertion, a bearer SubjectConfirmationData whose InResponseTo is the request this browser started and whose Recipient is this ACS. Each assertion and each request is accepted once, and an assertion whose signature holds is used up even when a later check refuses it. A Response with more than one assertion, a DOCTYPE, or a signature that doesn't cover the assertion it is attached to is refused, which stops signature-wrapping attacks. IdP-initiated sign-in is not accepted.
Roles from groups
Each IdP has its own group-to-role map: group=role entries separated by ; or new lines. A group can be a name, an object id or an AD distinguished name. A person gets the highest role among their matched groups.
| Setting | Engine roles | medhook.dev roles |
|---|---|---|
| Group map | viewer, operator, admin | USER, ADMIN |
| Default role | unset: refuse anyone whose groups match no entry | same |
| Re-sync | at every sign-in, the role follows the IdP's groups, except for accounts with a password | same |
The first sign-in creates the person (just-in-time provisioning) when the IdP verified their email: OIDC needs email_verified, and a signed SAML assertion counts. The *_TRUST_EMAIL setting lets an unverified email create a new person, and nothing else. An existing account with the same email is linked automatically only when the IdP verified the email and the account has no password and isn't an admin. An account with a password (break-glass) or the admin role is never linked by email: its owner signs in as usual and chooses Settings → Single sign-on → Other identity providers → Link my account. Linking asks for the account's password again, and the authenticator code when one is enrolled (the check is good for 5 minutes and one link), and is refused unless the identity provider account's verified email is the account's email. A link is audited with the IdP's issuer and subject (auth.sso_account_linked). Unlink my account removes your own link, and an admin can remove anyone's (DELETE /api/auth/sso/link?userId=, audited as auth.sso_account_unlinked). A linked account with a password also stops signing in by SSO if the IdP's verified email for it changes. Emails are compared exactly after trimming and lowercasing ASCII letters; an address with non-ASCII characters is never matched, linked or used to create a person, and dots and + tags are part of the address. Wrong passwords or codes when confirming a link count toward the account's sign-in lockout. A linked account with a password keeps the role an admin gave it; the IdP's groups only decide whether it may sign in. On the engine, a new person who isn't an admin joins the default workspace at their role; an admin adds them to other workspaces. Every refusal, account creation, link and role change is in the audit log (auth.signin_failed, auth.sso_user_provisioned, auth.sso_account_linked, auth.sso_role_changed on the engine; auth.signin.refused, auth.sso.user_provisioned, auth.sso.role_changed on medhook.dev).
MFA
The IdP enforces MFA. A person who enrolled an authenticator in MedHook is asked for its code after SSO, as after Google sign-in, unless you turn on Accept its MFA in place of an enrolled authenticator for that IdP (*_TRUST_IDP_MFA=true, off by default): then an IdP-reported multi-factor sign-in counts as the second factor. Turn on Require MFA at the identity provider (*_REQUIRE_MFA=true) to refuse any sign-in it doesn't report as multi-factor.
What counts as multi-factor: an OIDC amr claim with mfa, or a possession or inherence method (hwk, swk, sc, otp, fpt, face, iris, retina, vbm) together with a knowledge method (pwd, pin, kba). One method alone, two knowledge methods (pwd and kba) or fed do not count. For SAML, Entra's multipleauthn (as an authentication context or an authnmethodsreferences value), AD FS's phone factor, the mobile two-factor classes, or the REFEDS MFA profile.
Break-glass access
Email and password sign-in keeps working next to SSO. Keep at least one engine admin with a password and an authenticator (create-engine-user admin@example.test --role admin) for when the IdP is down or misconfigured.
Set up the engine
You can configure IdPs in two places:
- Environment variables: one OIDC IdP (
ENGINE_OIDC_*, sign-in idoidc) and one SAML IdP (ENGINE_SAML_*, idsaml). See Environment variables. Keep the client secret in your secrets manager. - Settings → Single sign-on → Other identity providers (admins): add any number of IdPs with Add identity provider. The OIDC client secret is stored encrypted with the engine's master key and is never shown again. The same API is
PUT /api/settings/sso.
The engine shows the URLs to register at the IdP for each provider. With ENGINE_PUBLIC_URL=https://engine.example.test and the provider id corp:
| Protocol | Register at the IdP |
|---|---|
| OIDC | Redirect URI https://engine.example.test/api/auth/sso/corp/callback |
| SAML | ACS URL https://engine.example.test/api/auth/sso/corp/acs |
| SAML | Entity ID and metadata https://engine.example.test/api/auth/sso/corp/metadata |
SAML needs https: the flow cookie must survive the IdP's cross-site POST. On https the flow cookies are __Host- prefixed (Secure, Path=/, no Domain).
Set up medhook.dev
medhook.dev reads one OIDC and one SAML IdP from its environment, and only when NEXTAUTH_URL is set (the ids are built from it, never from Host). Linking an existing account to SSO is not offered on medhook.dev: ADMINs and accounts with a password keep their own sign-in. The OIDC provider is a NextAuth provider with PKCE, state and nonce checks. Register https://medhook.example.test/api/auth/callback/oidc (OIDC) or https://medhook.example.test/api/auth/saml/acs and the entity ID https://medhook.example.test/api/auth/saml/metadata (SAML), with your site's address in place of the example.
| Variable | What it does |
|---|---|
SSO_OIDC_ISSUER, SSO_OIDC_CLIENT_ID, SSO_OIDC_CLIENT_SECRET | The OIDC IdP. Off unless all three are set. |
SSO_OIDC_LABEL, SSO_OIDC_GROUPS_CLAIM | Button text (default Single sign-on) and groups claim (groups). |
SSO_SAML_IDP_ENTITY_ID, SSO_SAML_IDP_SSO_URL, SSO_SAML_IDP_CERT | The SAML IdP. Off unless all three are set. |
SSO_SAML_LABEL, SSO_SAML_GROUPS_ATTRIBUTE, SSO_SAML_SP_ENTITY_ID, SSO_SAML_REQUIRE_SIGNED_RESPONSE | Optional SAML settings. |
SSO_<OIDC or SAML>_GROUP_ROLES | group=USER or group=ADMIN entries. |
SSO_<OIDC or SAML>_DEFAULT_ROLE | USER or ADMIN for unmapped people; unset refuses them. |
SSO_<OIDC or SAML>_TRUST_IDP_MFA | IdP-reported MFA replaces the TOTP step. Off by default. |
SSO_<OIDC or SAML>_ALLOWED_DOMAINS, _REQUIRE_MFA, _TRUST_EMAIL | Email domain allowlist, required IdP MFA, letting an unverified email create a new person. |
SSO_ON_PREVIEW | Single sign-on is off on Vercel preview deployments, which may share the production database, unless this is true. |
Checklist
- The IdP app has the redirect URI or ACS URL above, and sends a groups claim or attribute.
- The group map gives every team the role it needs; the default role is unset (refuse) or deliberate.
- One admin can still sign in with a password and an authenticator.
- A test person in each mapped group signs in and gets the expected role.
- A person in no mapped group is refused, and the refusal shows in the audit log.
Troubleshooting
When the engine refuses a single sign-on, the person lands back on the sign-in page with a short message. The message says no more than that, so nobody learns about an account from it. The real reason is in the audit log: open Sign-ins in Activity, click the "Sign-in failed" row (auth.signin_failed) and read Context in its detail (reason: …). The event's metadata, in Export and GET /api/audit-log, also holds method (sso-oidc or sso-saml) and provider.
What the sign-in page says
| The sign-in page says | reason in the audit log | Cause | Fix |
|---|---|---|---|
| "Google Workspace sign-in isn't set up on this engine. Use your email and password." | none | The provider id in the URL is unknown or turned off, or ENGINE_PUBLIC_URL is unset, so there is no redirect URI. The page names Google for every provider. | Set ENGINE_PUBLIC_URL. Check that the provider is saved under Settings → Single sign-on → Other identity providers with the id the IdP redirects to. |
| "Sign-in was cancelled." | none | The IdP sent the person back with an OIDC error: they cancelled, or the IdP refused them, for example because they are not assigned to the app. | Read the IdP's own sign-in log. Assign the person or their group to the app. |
| "That sign-in link expired. Start again." | state_mismatch (SAML only; OIDC is not audited) | The browser came back without the flow cookie, more than 10 minutes after it left, or with a state that is not the one it took. SAML over http loses the cookie on the IdP's POST. | Start again from the sign-in page, in the same browser. Use https for SAML. Make sure nothing strips cookies between the browser and the engine. |
| "Too many attempts. Wait a few minutes and try again." | none | More than 20 callbacks or ACS posts in a minute from one IP address. | Wait a minute. Everyone behind one proxy shares that limit. |
| "That account isn't in an allowed domain for this engine." | domain_not_allowed | The email's domain is not in the provider's allowed domains. emailDomain in the event shows the domain the IdP sent. | Add the domain, or have the IdP send the person's work email. |
| "This engine needs a multi-factor sign-in at your identity provider. Sign in again with MFA." | mfa_required | Require MFA at the identity provider is on, and the IdP did not report a multi-factor sign-in. See MFA for what counts. | Make the IdP require MFA for the app, and send mfa in amr (OIDC) or a multi-factor authentication context (SAML). |
| "Your account isn't in a group that has access to this engine. Ask an admin." | no_role | None of the person's groups is in the group map, and there is no default role. groupCount in the event is the number of groups the IdP sent; 0 means it sent none. | Add their group to the map, or set a default role. With 0 groups, make the IdP send the groups claim or attribute. |
| "This account signs in with a password. Sign in that way, then link single sign-on under Settings → Users & roles." | link_required | An engine account with this email has a password or the admin role. Those are never linked by email. | The owner signs in with their password and chooses Link my account. |
| "That identity provider account's verified email isn't this account's email, so it wasn't linked." | email_mismatch | On Link my account, the IdP's verified email is not the account's email. A linked account with a password also stops signing in by SSO when the IdP's email for it changes. | Link from an IdP account with the same email, or change the engine account's email first. |
| "The link request expired. Confirm your password in Settings and try again." | link_expired, link_session | The 5-minute link window passed, or the person signed out or into another account before the IdP sent them back. | Start Link my account again and finish it within 5 minutes, in the same session. |
| "That account can't sign in to this engine. Ask an admin." | disabled, conflict, email_unverified, no_email | The account is disabled; the email is already linked to another IdP or subject; the IdP did not verify the email; or the IdP sent no usable email (none, or one with non-ASCII letters). | Enable the account. Unlink the old identity. Have the IdP send email_verified (OIDC). For a new person whose IdP can't verify emails, turn on *_TRUST_EMAIL. Use an ASCII email. |
| "Single sign-on failed. Try again." | a protocol code, see below | The IdP's answer failed a check. When it fails before the person reaches the IdP, for example on discovery, the engine logs "SSO sign-in could not start" and writes no audit event. | Look up the code in the next table. |
| "Your sign-in timed out. Start again." | none | The person took more than 10 minutes on the authenticator code step after SSO, or opened that step directly. | Start again. |
Protocol codes
These come with "Single sign-on failed. Try again." on the sign-in page.
reason | Protocol | Cause | Fix |
|---|---|---|---|
discovery_failed | OIDC | The issuer is not https, its discovery document could not be fetched, or it is for another issuer or lists unsafe endpoints. | Check the issuer URL exactly, trailing slash included. An IdP on a private network needs its host in SSRF_ALLOWLIST. |
token_exchange_failed | OIDC | The token request failed, or the IdP refused the code. | Check the client id and secret, and that the IdP has the redirect URI the engine shows. |
invalid_token | OIDC | The id_token's signature, issuer, audience or expiry is wrong, or its azp is another client. | Check the client id and the issuer. Check the engine host's clock. |
nonce_mismatch | OIDC | The id_token's nonce is not the one this browser sent. | Start again. If it repeats, look for something that replays old callbacks. |
missing_claim | Both | The id_token has no sub, or the assertion has no NameID. | Make the IdP send a stable subject or NameID. |
invalid_response | SAML | The SAMLResponse is missing, too large, not SAML 2.0, has a DOCTYPE or no assertion, or the provider has no IdP certificate. | Check that the IdP posts a SAML 2.0 Response to the ACS URL, and that the provider has the IdP's signing certificate. |
invalid_signature | SAML | The assertion is unsigned, signed by another certificate, or signed with SHA-1. | Sign the assertion, not only the Response, with SHA-256. Paste the IdP's current signing certificate again after it rotates. |
issuer | SAML | The Response or the assertion comes from another IdP entity ID. | Set the IdP entity ID to the IdP's exact value. |
audience | SAML | The assertion's Audience is not this engine's entity ID. | Register the entity ID the engine shows, from /metadata. |
destination | SAML | The Response Destination or the assertion's Recipient is not this ACS URL. | Register the ACS URL the engine shows. Check that ENGINE_PUBLIC_URL is the address people use. |
expired | SAML | The assertion is outside its validity window (60 seconds of clock skew), or has no valid subject confirmation. | Sync the IdP's and the engine host's clocks. |
in_response_to | SAML | The Response does not answer the AuthnRequest this browser started. IdP-initiated sign-in ends here. | Start from the engine's sign-in page, not from the IdP's app portal. |
replay | SAML | The AuthnRequest or the assertion was already used. | Start again. A repeat points to a browser that resubmits the form, or to a replay. |
sso_error | Both | An unexpected error. | Read the engine log at the event's time. |
On medhook.dev, a refusal is audited as auth.signin.refused with the same reason codes. Its sign-in page shows one message for every policy refusal, and "Single sign-on failed. Try again." for a SAML protocol error.