Single sign-on (platform)
Prepare Keycloak for organization single sign-on, understand how Tokamak binds each organization's identity provider to its verified domains, and recover an organization locked out by a broken provider.
Organizations connect their own OIDC or SAML identity provider for the email domains they have verified by DNS, and may require it. The organization's own guide is Verified domains and single sign-on. This page covers what a platform administrator configures and needs to know.
How it works
| Piece | Where it lives |
|---|---|
| Domain claims and DNS verification | Core (organization_domains), managed at Organization › Settings by the organization's org.admin |
| The connection and the sign-in rules | Auth (sso_connections, sso_connection_domains, and the domain history sso_alias_domains) |
| The brokered identity provider | Your Keycloak realm, as alias tokamak-org-<organization id>, hidden from Keycloak's own login page |
When an organization saves a connection, auth fetches the provider's discovery document or SAML metadata, uploads it to Keycloak's importer, and writes the brokered provider. Sign-in goes through the same Keycloak authorize and code exchange as Google and Microsoft, with kc_idp_hint=tokamak-org-<id>.
Auth applies two rules to every session credential before it creates, rebinds or resolves an account:
- Domain binding. A login through an organization's provider must carry an email on one of that connection's domains. Otherwise it is refused with
403 sso_domain_mismatch. Keycloak still creates its own account for a foreign address before the token reaches Tokamak. So each provider stamps the accounts it creates with atokamak_sso_aliasattribute, and auth keeps every domain a connection has ever covered. When auth meets a realm account for the first time, or its email changed, it holds the account to those domains for every organization provider that created it or that it is linked to, whatever method it signs in with. Deleting or replacing the connection, or unlinking the account, does not lift this. - Enforcement. For a domain whose connection is required, every other sign-in method is refused with
403 sso_required, including sessions issued earlier.
Removing a verified domain, or the daily re-check revoking it, removes it from the connection first. A connection left with no domains is disabled in auth and in Keycloak. Saves, deletes, removal and revocation take a per-organization lock, so none of them acts on a domain set another is changing.
Prepare Keycloak
The auth service's Keycloak service client (TOKAMAK_AUTH_KEYCLOAK_SERVICE_CLIENT_ID) needs these realm-management roles, in addition to manage-users and view-users for password sign-in:
| Role | Why |
|---|---|
manage-identity-providers, view-identity-providers | Create, update and delete each organization's brokered provider |
view-clients, query-clients | Find the login client to check its token mapper |
The login client (KEYCLOAK_CLIENT_ID) must put the broker alias in its access tokens: a User Session Note protocol mapper with session note identity_provider, claim name identity_provider, type String, added to the access and ID tokens. Auth adds the mapper on the first save if the service client also holds manage-clients; otherwise add it yourself. Without this claim, brokered logins cannot be bound to their domains.
The realm's user profile (Realm settings › User profile) must declare the attribute tokamak_sso_alias, with view and edit permission for admin only. Keycloak silently drops an attribute the profile does not declare, so auth refuses to save a connection until it is declared.
The local realm infra/docker/tokamak/keycloak/realm-jan.local.json carries all three. compose.keycloak.yml also imports acme-idp, a stand-in corporate identity provider for trying OIDC and SAML locally.
Outbound requests
The metadata URL is supplied by a tenant, so auth fetches it rather than letting Keycloak do it. The fetch:
- uses https only;
- connects only to globally routable addresses, checked on the address actually dialed (no loopback, private, link-local, CGNAT or cloud metadata ranges);
- checks every redirect again, at most three.
The provider's token, userinfo and JWKS URLs from the document must pass the same check. The other server-side calls a provider can request are switched off: SAML metadata re-fetch, back-channel logout and SAML artifact binding.
To allow identity providers on a private network, list their host names in TOKAMAK_AUTH_SSO_ALLOWED_PRIVATE_HOSTS (comma-separated). Keycloak resolves the back-channel host names again at login, so a name re-pointed to a private address after saving is not caught here. Keep Keycloak's own egress restricted.
Recover a locked-out organization
If an organization requires single sign-on and its identity provider breaks, its people, admins included, cannot sign in. To restore access:
- turn the connection off from the database (
UPDATE tokamak_auth.sso_connections SET enforced = false WHERE organization_id = …), so other sign-in methods work again; or - delete the provider alias
tokamak-org-<id>in Keycloak and the row insso_connections.
Record what you changed and tell the organization's owner. A platform console page for this is not available yet.
Settings
| Variable | Default | Description |
|---|---|---|
TOKAMAK_AUTH_SSO_ALLOWED_PRIVATE_HOSTS | — | Host names whose metadata and back-channel URLs may be private or http. The local compose sets keycloak |
TOKAMAK_CORE_ORG_DOMAIN_DNS_SERVER | system resolver | host:port of one DNS server for every domain verification lookup |
TOKAMAK_CORE_ORG_DOMAIN_RECHECK_INTERVAL | 24h | How often a verified domain is checked again; 0 turns re-checks off |
TOKAMAK_CORE_ORG_DOMAIN_RECHECK_GRACE | 72h | How long a record may stay missing before the domain returns to pending |
TOKAMAK_CORE_ORG_DOMAIN_RECHECK_BATCH_SIZE | 50 | Domains per re-check pass |
Upgrading
Core migration 000147 carries every organization's existing allow-list over as verified (method legacy) with auto-join on, so nobody loses auto-join on deploy. If two organizations listed the same domain, the lowest id keeps it and the others get a pending claim they must prove. Legacy domains are not re-checked; an organization can prove one by DNS to make it a normal verified domain. Auth migration 000045 adds the connection tables.
Not covered
- One connection per organization.
- No identity-provider-initiated SAML, SCIM provisioning or group-to-team mapping.
- Requiring single sign-on does not revoke API keys members already issued. Pair it with directory lifecycle sync so disabling someone in the directory stops their keys.
- A foreign address a provider asserted keeps its Keycloak account (unable to use Tokamak) until you delete it in the realm.