Skip to content

Single sign-on

SAML 2.0 single sign-on lets people sign in to your app through their organisation’s identity provider, such as Okta, Microsoft Entra ID, Google Workspace or Keycloak. The provider is chosen by the domain of the user’s email address.

The auth server is the SAML service provider. You turn SAML on once in supatype.config.ts. Identity providers are runtime data, usually one per customer, so you register them with supatype sso, in Studio, or through the admin API, never in the config. SAML works on supatype dev and on self-hosted stacks. It is not on Supatype Cloud yet.

Turn SAML on

supatype.config.ts
auth: {
sso: {
saml: { enabled: true },
},
},

Restart the stack to apply it: stop and start supatype dev, or run supatype self-host compose up -d on a server. The section takes these fields:

FieldWhat it does
enabledRequired. true turns SAML on, and the CLI generates the signing key the first time it sees it
allowEncryptedAssertionsAccept encrypted assertions, and publish the key for encryption in the service provider metadata
relayStateValidityHow long, in seconds, a sign-in your app starts may take at the identity provider. The server’s default is 120
rateLimitAssertionHow many assertions the server accepts from one client every 5 minutes. The server’s default is 15
externalUrlThe service provider’s base URL, under which /sso/saml/metadata and /sso/saml/acs are served. The default is your API URL plus /auth/v1

A SUPATYPE_SAML_* value in .env wins over the config’s. While SAML is off, the sign-in routes and the metadata answer 404 with SAML 2.0 is disabled. The admin routes keep working, so you can register providers before you turn it on, and every supatype sso command warns that SAML is off.

The signing key

The service provider signs its requests with an RSA key, and its certificate is made from that key alone. The first supatype dev or supatype self-host compose up with SAML on writes a new key to the project’s .env as SUPATYPE_SAML_PRIVATE_KEY. On a self-hosted server that is the .env in the server’s project directory. The CLI never prints the key, and never replaces one that is there.

The key never goes in supatype.config.ts, which is committed: a privateKey field under auth.sso.saml is refused with the variable to use instead. The generated Compose file names the variable and refuses to start without it, but never holds the value.

What to give the identity provider

An identity provider asks for the service provider’s entity ID and its Assertion Consumer Service (ACS) URL, or for its metadata, which holds both and the certificate:

ValueWhere
Entity ID and metadata URL (the same address)<API URL>/auth/v1/sso/saml/metadata
Metadata as a file<API URL>/auth/v1/sso/saml/metadata?download=true
ACS URL (HTTP-POST binding)<API URL>/auth/v1/sso/saml/acs
NameID formatPersistent is requested; email address is accepted too

supatype sso info prints the entity ID, ACS URL and metadata URL for the local stack, or for the linked deployment. With SAML on, it also reads the served metadata and prints the signing certificate as PEM under Signing certificate (SHA-256 <fingerprint>):, for an identity provider that asks for the certificate rather than the metadata. During a key rotation it prints the next one too, under Next signing certificate, published for the key rotation. While SAML is off it warns once that SAML is off, and that these values are not served until it is on: the metadata URL answers 404. Have the identity provider send an email attribute (email or mail), or use the email address as the NameID, and sign the assertion or the whole response. A stable user ID in the urn:oasis:names:tc:SAML:attribute:subject-id attribute is used in preference to the NameID when it is sent.

Register an identity provider

Terminal window
$ supatype sso add acme --metadata-url https://idp.acme.com/metadata --domains acme.com
$ supatype sso list
$ supatype sso show acme

The name you give add is how every other sso command refers to the provider. It may use letters, digits, ., _ and -, starting with a letter or digit. The commands reach the linked self-hosted deployment when the project is linked, and the local stack otherwise, which must be running. They call the admin API with the service role key from .env, or the link’s token.

Flag (add and update)What it sets
--metadata-url <url>The identity provider’s metadata URL. The server fetches it, and fetches it again when the copy it keeps goes stale
--metadata-file <path>The metadata XML from a file, instead of a URL. Update it by hand when the identity provider changes its certificate
--domains <list>Email domains that sign in through this provider, comma-separated. On update it replaces the list. A domain belongs to one provider only
--attribute-mapping-file <path>JSON that maps assertion attributes to user metadata: { "keys": { "<field>": { "name": "<attribute>" } } }, each entry with name or names, and optional default and array
--name-id-format <format>The NameID format to request: persistent, emailAddress, transient or unspecified

add needs --metadata-url or --metadata-file, never both. update <name> changes only what its flags give, in place, and new metadata must keep the provider’s entity ID. remove <name> asks first, or takes -y.

The server fetches the metadata URL

--metadata-url is fetched by the Supatype server, not by the CLI. Under Docker the server runs in a container, where localhost is the container itself, so the identity provider must be reachable from inside the server’s container. When it is not, add or update fails with a 400 (saml_metadata_fetch_failed) that names the host and the reason:

Terminal window
The server could not fetch SAML metadata from localhost:8080: connection refused. The server fetches metadata_url itself, so the URL must be reachable from the server, not only from your machine

The reason is one of connection refused, the host name could not be resolved, its TLS certificate is not trusted, timed out after 10s, it answered with HTTP status <N> and the response is not SAML metadata XML. Metadata given with --metadata-file that is not SAML metadata is refused with metadata_xml is not SAML metadata XML.

An identity provider such as Keycloak builds its entity ID and sign-in URLs from the host it was reached on. Fetch the metadata through the same name and port the browser uses, or the server keeps endpoints the browser cannot open and an entity ID the provider’s responses do not carry. For an identity provider running on your machine, either attach its container to the stack’s Docker network under a name the browser also resolves to it (a *.localhost name resolves to your machine in browsers), or download the metadata in the browser and register it with --metadata-file.

A metadata URL must be HTTPS. Plain http:// is accepted only on a dev stack (supatype dev), for a local identity provider; a self-hosted stack refuses it with metadata_url is not a HTTPS URL.

Verify a domain

A provider with domains may only sign in email addresses on those domains. Any other address is refused with SAML Assertion email is not on a domain of this identity provider. A domain routes sign-ins as soon as it is added, but it is not verified until you prove you own it, and only a verified domain links a sign-in to an account that already exists.

Terminal window
$ supatype sso verify-domain acme acme.com
acme.com is not verified yet. Add this DNS record where the domain's DNS is managed:
_supatype-challenge.acme.com TXT supatype-domain-verification=...
No TXT record was found at _supatype-challenge.acme.com.
DNS changes can take a while to be seen. Check again with: supatype sso verify-domain acme acme.com

The first run issues the domain’s token and prints the TXT record to add at _supatype-challenge.<domain>. Each run checks DNS, and the command exits 1 until the record holds the token. Once seen, the domain stays verified, and the record may be removed. There is no periodic check after that. sso list and sso show mark each domain verified or not verified.

On a dev stack, a domain ending in .supatype.test counts as verified with no DNS lookup, so you can try linking locally. It still reads as not verified until verify-domain has run for it. A self-hosted stack always checks DNS.

What verification changes

The first time someone signs in through a provider, the server looks for an existing account with the same email: a password, magic link, phone or OAuth account, not another SSO user.

  • No account: a new one is created, verified domain or not.
  • An account, and the email’s domain is verified for this provider: the sign-in is linked to that account. It keeps its ID, its email and every way it already signed in, and gains an sso:<provider ID> identity.
  • An account, and the domain is not verified: the sign-in is refused with This email already has an account. Ask your administrator to verify the domain so they can be linked. No second account is ever made for the same address.
  • Several accounts whose email differs only in letter case: the one whose email is exactly the address the identity provider sent, letter for letter, is the one considered. When none matches exactly, the sign-in is refused with More than one account has this email. Ask your administrator to merge them before signing in with SSO.

Sign users in

A sign-in your app starts asks the server for the identity provider’s URL by the user’s email domain, or by the provider’s ID, and sends the browser there:

const { error } = await supatype.auth.signInWithSSO({
domain: "acme.com",
options: { redirectTo: window.location.href },
})
if (error) console.error(error.message)

Pass { domain } or { providerId }, never both. providerId is the provider’s UUID, the ID line of sso show, not its name. A domain with no provider is refused with No SSO provider assigned for this domain, and a disabled provider with SSO Provider is currently disabled; the page stays where it is. options also takes captchaToken, skipBrowserRedirect to get the URL back without navigating, and flowType. useAuth() from @supatype/react has signInWithSSO too.

The sign-in uses PKCE by default, unlike signInWithOAuth. The browser comes back to redirectTo with ?code= in the address and no tokens, and the client exchanges the code when it starts, with the verifier it saved in this browser when the sign-in began. So redirectTo must be a page of the same app that creates the client, opened in the same browser: usually the page that started the sign-in. It is honoured on SITE_URL’s host, a loopback address, or an entry in SUPATYPE_URI_ALLOW_LIST; without it, or for any other address, the browser lands on SITE_URL. initialize() resolves once the exchange is done, as it does for OAuth.

The pre-built form

src/SignInWithSSO.tsx
import { SupatypeProvider } from "@supatype/react"
import { SSOForm } from "@supatype/react-auth"
import { supatype } from "./supatype"
export function SignInWithSSO() {
return (
<SupatypeProvider client={supatype}>
<SSOForm redirectTo={window.location.href} onError={(error) => console.error(error.message)} />
</SupatypeProvider>
)
}

SSOForm asks for a work email and starts the sign-in for its domain. It takes redirectTo, onSuccess (called with the identity provider’s URL), onError, className, classNames and labels, like the other auth forms. A refusal from the server, on the way out or the way back, shows above the field and goes to onError.

Sign-ins the identity provider starts

When a user opens your app from the identity provider’s dashboard, there is no sign-in in your app to come back to. The server sends the browser to SITE_URL, or to a RelayState URL the provider sends when it is allowed, with the tokens in the fragment (#access_token=...), since no browser holds a PKCE verifier for it. The client accepts this whatever its flowType: it stores the session and clears the fragment. So the page at SITE_URL must create the client.

A sign-in the server refuses on the way back, from either side, lands on SITE_URL with error, error_code and error_description in the query, and initialize() resolves with that error.

Rotate the signing key

Rotation runs two keys side by side, so identity providers can take the new certificate before the old one stops working.

1. Create the next key

Terminal window
$ supatype sso rotate-key

This writes SUPATYPE_SAML_NEXT_PRIVATE_KEY to .env, or keeps the one already there.

2. Restart the stack

Restart supatype dev, or run supatype self-host compose up -d, so the server loads the next key. Until then nothing has changed. From the restart, the metadata lists two certificates, the current one and the next. Requests are still signed with the current key, and encrypted assertions are decrypted with either.

3. Give every identity provider the new metadata

Have each provider fetch the metadata again, upload the new file, or take the next certificate that supatype sso info prints during the rotation. Providers that check request signatures must take the new certificate before the next step.

4. Promote the next key

Terminal window
$ supatype sso rotate-key --promote

It asks first, or takes -y; with no terminal it needs -y. The next key replaces SUPATYPE_SAML_PRIVATE_KEY, and SUPATYPE_SAML_NEXT_PRIVATE_KEY is removed.

5. Restart again

After this restart the server signs with the new key and the metadata lists one certificate. A provider that never took the new certificate now fails its signature check.

On a project linked to a self-hosted deployment, the key is in the server’s environment, not your .env, so rotate-key prints these steps for you to run in the server’s project directory instead.

In Studio

Authentication > SSO lists the providers with their entity IDs, domains and status, and, while SAML is on, the entity ID and ACS URL to give an identity provider. There you can add a provider from a metadata URL or XML, edit it, disable and enable it, remove it, and verify a domain: the Verify dialog shows the TXT record and checks it. The page says when SAML is off.

Only owners and admins can change providers and verify domains; on a self-hosted project that means admins. The server enforces it, not just the page. Developers and viewers see the page read-only, with Only owners and admins can change SSO providers. Editors have no SSO page. See Studio roles. The Users list marks each user who signed in through SAML with sso: and their provider’s name, for example sso:acme. A provider that has been removed, or was added without a name, shows the first block of its ID instead.

While SAML is on, Studio’s own sign-in page offers Sign in with SSO: an admin enters their work email and goes to the identity provider for its domain, then comes back to Studio, never to the app’s site URL. Studio still lets in only people with a Studio role. With auth.email.enabled: false, the setting for a project that signs in only through SSO, Studio’s email and password form is hidden, leaving single sign-on and any OAuth providers. The first SSO sign-in to an existing account removes its password, so admins of an SSO project sign in to Studio this way too.

The admin API

An app that lets its own customers set up SSO calls the auth server’s admin API from its server, with the service role key. Never call it from the browser: the service role key passes every access rule.

Terminal window
$ curl -X POST https://api.example.com/auth/v1/admin/sso/providers \
-H "Authorization: Bearer $SERVICE_ROLE_KEY" \
-H "Content-Type: application/json" \
-d '{"type":"saml","resource_id":"acme","metadata_url":"https://idp.acme.com/metadata","domains":["acme.com"]}'
Route under /auth/v1What it does
GET /admin/sso/providersLists providers as { "items": [...] }, without their metadata XML. Filter with ?resource_id= or ?resource_id_prefix=
POST /admin/sso/providersCreates one. type: "saml", and metadata_url or metadata_xml; optional domains, attribute_mapping, name_id_format, resource_id and disabled
GET /admin/sso/providers/{id}One provider, with its domains and whether each is verified
PUT /admin/sso/providers/{id}Changes the fields given. domains replaces the list, new metadata must keep the entity ID, and disabled: true turns the provider off
DELETE /admin/sso/providers/{id}Removes the provider
POST /admin/sso/providers/{id}/domains/{domain}/verifyIssues the domain’s token the first time, checks DNS, and answers domain, verified, verified_at, the challenge record (name and value) and what DNS returned in found

{id} is the provider’s UUID, or resource_ followed by its resource_id: resource_acme. resource_id is the name supatype sso uses, so give each customer’s provider a stable one and change the provider with PUT rather than deleting and creating it again. The admin routes work while SAML is off.