# SSO login (SAML)

**Last updated:** 2026-11-21T09:54:10-03:00 (UTC-3)

Optional [federated MFA policy](/docs/features/federated-mfa/) requests an exact authentication context and checks the returned class and optional authentication freshness inside the selected signed assertion. This is distinct from assertion age and session lifetime; the existing SAML trust DTO remains unchanged.

Console users sign in with a corporate SAML 2.0 identity provider (Keycloak, Microsoft Entra ID, Okta, ADFS, or any IdP that signs its responses). The gateway never sees the user's password. Flags: `saml_login` (the login itself) and `saml_management` (editing the trust from the API and console).

SAML does not grant a role or team access. By default, sign-in resolves an existing local gateway user, with the role and teams an administrator assigned. Optional [JIT provisioning](/docs/features/jit-provisioning/) can create an allowlisted identity pending approval; only that profile reads its configured verified e-mail and group attributes. IdP role attributes never grant local roles.

Prefer [OIDC](/docs/features/oidc/) when the IdP offers both: it is simpler to operate. Use SAML when the company requires it.

## How to turn it on

```yaml
features:
  ui: true
  console_users: true
  saml_login: true
  saml_management: true    # optional: edit the trust from the API/console
admin:
  saml:
    label: Corporate SAML
    entity_id: https://gateway.example.com/saml
    acs_url: https://gateway.example.com/api/v1/admin/saml/callback
    idp_entity_id: https://identity.example.com/realms/company
    idp_sso_url: https://identity.example.com/realms/company/protocol/saml
    idp_signing_certificates_pem:
      - |
        -----BEGIN CERTIFICATE-----
        ...
        -----END CERTIFICATE-----
    subject_users:
      persistent-name-id-from-the-idp: usr_0123456789abcdef0123456789abcdef
    session_epoch: 2026-11-rollout-1
```

- Requires PostgreSQL and the console session key. Local login and the admin bootstrap token keep working, for recovery.
- `acs_url` is HTTPS and its path is exactly `/api/v1/admin/saml/callback`. `idp_sso_url` is HTTPS. The gateway itself listens on HTTP behind your TLS ingress, as in any production profile.
- `idp_signing_certificates_pem` pins up to eight IdP signing certificates (RSA, 2048 bits or more). A certificate in the response never adds trust. List the next certificate before the IdP rotates its key, then remove the old one. Responses with or without a KeyInfo certificate work during rotation; if the response and the assertion are signed by two different keys, the IdP must include KeyInfo.
- `subject_users` binds the IdP's **persistent** NameID to an existing local user (the `usr_…` ID shown in **Console users**). Unmapped subjects are refused. Transient, email and unspecified NameID formats are refused.
- `session_epoch` is a label you choose; changing it ends every SAML session.
- The console shows the SAML button on the login screen with the configured `label`.

With `saml_management`, **Settings & sources → SAML single sign-on** edits the same fields, and so does the API. Only a global administrator can read or change it. The console value overrides the YAML until you choose **Revert SAML to YAML**. Saving rejects a binding to a missing or disabled user.

| Parameter | Default | Range | Meaning |
| --- | --- | --- | --- |
| `label` | `Corporate SAML` | up to 80 characters | Login button text |
| `entity_id` | required | an absolute URI | The gateway's own SAML entity ID: the audience the assertion must name, and the Client ID at the IdP |
| `acs_url` | required | HTTPS, path `/api/v1/admin/saml/callback` | Where the IdP posts its response |
| `idp_entity_id` | required | an absolute URI | The IdP's entity ID, compared exactly with the response and assertion issuer |
| `idp_sso_url` | required | HTTPS | The IdP's single sign-on address the login redirects to |
| `request_ttl` | `5m` | 1m–10m | How long a started login waits for the IdP |
| `assertion_max_age` | `5m` | 1m–10m | Oldest accepted response and assertion |
| `clock_skew` | `30s` | 0s–2m | Tolerance for the IdP clock |
| `max_response_bytes` | `262144` | 16 KiB–1 MiB | Largest decoded response |
| `max_xml_tokens` | `20000` | 1000–50000 | Largest XML document, in tokens |
| `max_concurrent_callbacks` | `4` | 1–64 | Responses validated at once per process; the excess gets 503 |

### Keycloak

Create a SAML client whose **Client ID** is `entity_id`, then set:

- **Valid redirect URIs** and **Assertion Consumer Service POST Binding URL**: the exact `acs_url`.
- **Name ID format** `persistent`, with **Force name ID format** on.
- **Sign documents** and **Sign assertions** on, algorithm `RSA_SHA256`, canonicalization *Exclusive*.
- **Client signature required** off (the gateway does not sign its AuthnRequest). **Encrypt assertions** off, unless you turn on [encrypted assertions](#encrypted-assertions).
- The signing certificate is in the realm descriptor, `https://<keycloak>/realms/<realm>/protocol/saml/descriptor`. `idp_entity_id` is `https://<keycloak>/realms/<realm>` and `idp_sso_url` is `…/protocol/saml`.

Keycloak generates the persistent NameID on the user's first login and keeps it as a user attribute. Read it from the user's attributes in Keycloak and add it to `subject_users`.

### Other identity providers

Use the gateway's metadata (`GET /api/v1/admin/saml/metadata`) to register it. The IdP must sign both the response and the assertion with RSA-SHA256 and exclusive canonicalization, release a persistent NameID, and post to the ACS URL. Microsoft Entra ID, Okta and ADFS can do this; they were not connected in testing.

## How it works

- **Flow:** SP-initiated, HTTP-Redirect to the IdP and HTTP-POST back. A login started at the IdP (IdP-initiated) is refused.
- **Binding to the browser:** the start sets a short `__Host-` cookie, sealed with AES-GCM, that holds the request ID and a hash of a random RelayState. Nothing is stored on the server for a started login, so abandoned or anonymous starts cannot fill anything. The callback needs that cookie and the exact RelayState; the request ID is then consumed once.
- **Validation:** both signatures by a pinned certificate, exact issuer, destination, recipient, audience and `InResponseTo`, bearer confirmation, time windows within `assertion_max_age` and `clock_skew`, one assertion, no DTD. An encrypted assertion is refused unless [encrypted assertions](#encrypted-assertions) are on. The request, response and assertion IDs are remembered in PostgreSQL only after both signatures verify, so a replay is refused even on another replica.
- **Session:** eight hours, or less if the IdP sets `SessionNotOnOrAfter`. The user's role, teams and status are reread on every request. Disabling the user ends the session on the next request.
- **Configuration change:** any change to the SAML trust, `saml_login` or the YAML baseline ends every SAML session and every started login, on all replicas.
- **Failure:** the callback answers a generic 401 or 403. Logs and JIT creation audit keep the callback HTTP `request_id`; `saml_request_id` separately identifies the SAML AuthnRequest, and successful login also links `initiating_request_id`. Trace/span IDs follow the request context. Logs never include the response, NameID or user attributes.

| Endpoint | What it does |
| --- | --- |
| `GET /api/v1/admin/auth/providers` | Public list of login buttons; includes SAML when it is on and configured |
| `GET /api/v1/admin/saml/start` | Starts the login and redirects to the IdP |
| `POST /api/v1/admin/saml/callback` | Assertion Consumer Service; creates the session and returns to `/ui` |
| `GET /api/v1/admin/saml/metadata` | Service provider metadata for the IdP; with the T168 flags, the SP certificates (encryption, signing) and the SingleLogoutService |
| `GET`, `POST /api/v1/admin/saml/slo` | The IdP's LogoutResponse (`saml_single_logout`) |
| `GET`, `PUT`, `DELETE /api/v1/admin/saml/settings` | Read, save or revert the trust (`saml_management`, administrator, CSRF and `version`) |

## Encrypted assertions

Flag `saml_encrypted_assertions` (off by default, requires `saml_login`). With it on, the IdP may encrypt the assertion to the gateway's certificate. Nothing about signatures changes: the response and the assertion are both signed by a pinned IdP certificate, exactly as for a plain assertion.

```yaml
features:
  saml_encrypted_assertions: true
admin:
  saml_service_provider:
    key_env: GATEWAY_SAML_SP_KEY                 # PEM RSA private key, 2048 bits or more
    certificate_env: GATEWAY_SAML_SP_CERTIFICATE # PEM certificate of that key
    previous_key_env: GATEWAY_SAML_SP_PREVIOUS_KEY                 # optional, during rotation
    previous_certificate_env: GATEWAY_SAML_SP_PREVIOUS_CERTIFICATE # set together with previous_key_env
  saml:
    # ...the trust above...
    require_encrypted_assertions: false # true refuses plain assertions
```

| Key | Default | What it does |
| --- | --- | --- |
| `saml_service_provider.key_env` | required with the flag | Variable holding the current PEM RSA private key (2048 bits or more); it decrypts assertions and signs the LogoutRequest |
| `saml_service_provider.certificate_env` | required with the flag | Variable holding the PEM certificate of that key, published in the metadata |
| `saml_service_provider.previous_key_env` | empty | Variable holding the key being retired during a rotation; it only decrypts |
| `saml_service_provider.previous_certificate_env` | empty | Variable holding the certificate of the previous key; set together with `previous_key_env` |

- The key pair is a boot reference to secrets: environment variables, or `<NAME>_FILE` files with `secret_files` (a Kubernetes Secret mounted as files). It is never stored in YAML values, the database or the API, and is read when used, so replacing a mounted file takes effect without a restart. A key that cannot be read refuses encrypted responses only; startup with the flag on refuses a missing, mismatched or expired pair.
- **What is accepted:** one `EncryptedAssertion` with its `EncryptedKey` inside `EncryptedData/KeyInfo` or beside `EncryptedData`. Content AES-128-GCM, AES-256-GCM, AES-128-CBC or AES-256-CBC; key transport RSA-OAEP (`xmlenc#rsa-oaep-mgf1p`, or `xmlenc11#rsa-oaep` with MGF1 SHA-1 or SHA-256), digest SHA-1 or SHA-256. RSA 1.5 key transport, Triple DES, AES-192, OAEP labels, `CipherReference`, `EncryptedID`, `EncryptedAttribute` and any other algorithm are refused before the key is used.
- **Order of checks:** the response signature, which covers the ciphertext, is verified first. Only then is the assertion decrypted, with the current key and then the previous one. The decrypted assertion must be signed by a pinned certificate and pass every check of a plain assertion. Every failure is the same generic 401, so a changed ciphertext can never be used to probe the key (no padding or Bleichenbacher oracle).
- **Rotation:** publish the new pair as current and keep the old one as previous. Metadata lists both certificates for encryption until you remove the previous pair, after the IdP moved to the new certificate. A previous pair that expires or cannot be read is dropped (no longer decrypts or appears in metadata); the current pair keeps working. An expired or unreadable current pair refuses encrypted responses and Single Logout.
- XML Encryption identifiers (`Id` on `EncryptedData` and `EncryptedKey`, as OpenSAML and Shibboleth send) are accepted; every identifier in the document must still be unique.
- `require_encrypted_assertions` refuses a plain assertion; it is ignored while the flag is off.
- **Keycloak:** in the client's **Keys** tab, turn on **Encrypt assertions** and import the gateway's certificate (from the metadata, or `certificate_env`). Keycloak's defaults (AES-256-GCM, RSA-OAEP with SHA-256) are accepted.

## Single Logout

Flag `saml_single_logout` (off by default, requires `saml_login`). Signing out of the console then also ends the session at the IdP, started from the gateway (SP-initiated).

```yaml
features:
  saml_single_logout: true
admin:
  saml_service_provider: # the same key pair signs the LogoutRequest
    key_env: GATEWAY_SAML_SP_KEY
    certificate_env: GATEWAY_SAML_SP_CERTIFICATE
  saml:
    # ...the trust above...
    idp_slo_url: https://identity.example.com/realms/company/protocol/saml
    slo_url: https://gateway.example.com/api/v1/admin/saml/slo
```

| Parameter | Default | Rule |
| --- | --- | --- |
| `idp_slo_url` | none | HTTPS, no query: the IdP's SingleLogoutService for the HTTP-Redirect binding |
| `slo_url` | none | HTTPS, the ACS origin, path exactly `/api/v1/admin/saml/slo` |
| `require_encrypted_assertions` | `false` | Refuse plain assertions (with `saml_encrypted_assertions`) |

Set both URLs or neither; with neither, sign-out stays local. The console edits them in **Settings & sources → SAML single sign-on → Encryption and Single Logout**; the key stays in YAML.

1. At sign-in the gateway keeps the NameID and `SessionIndex` in a sealed `__Host-` cookie (AES-GCM, HttpOnly, SameSite=Strict) bound to that session, written only once the sign-in succeeds; the signed session cookie is unchanged.
2. **Sign out** clears the local session first. Then the gateway answers with a signed `LogoutRequest` on the HTTP-Redirect binding (RSA-SHA256 over the query, current key) and the console follows it to the IdP. A sealed `__Host-` cookie binds the request ID and a random RelayState to this browser.
3. The IdP answers at `slo_url` with a `LogoutResponse`, on the Redirect binding (query signature) or the POST binding (enveloped signature). The gateway checks the signature with a pinned certificate, the issuer, `Destination` = `slo_url`, `InResponseTo`, the issue time and the browser cookie, spends the request and response IDs once across replicas and redirects to `/ui`.

The local session ends even if the IdP is down, refuses the request or never answers: the browser is already signed out of the console before it leaves. An IdP failure status is logged (`saml_logout_completed`, `outcome: idp_refused`); a forged or replayed response gets a generic 401. A session signed in before the flag was turned on, or without its logout cookie, signs out locally only.

**IdP-initiated logout is not adopted.** A `LogoutRequest` sent by the IdP to `slo_url` is refused with 400: ending sessions that way needs a server-side index of every console session by NameID, which this profile does not keep (sessions are signed cookies). Sign-out started elsewhere at the IdP does not end a console session; it ends at its own expiry, at most eight hours or `SessionNotOnOrAfter`.

**Keycloak:** in the client, set **Logout Service Redirect Binding URL** (or POST) to `slo_url`, turn on **Front channel logout** and import the gateway's signing certificate in **Keys**. Leave **Client signature required** off: the AuthnRequest is unsigned, so Keycloak would refuse logins, and it then accepts the signed LogoutRequest without checking it. `idp_slo_url` is the realm's `…/protocol/saml`.

## Limits

- Each process validates at most `max_concurrent_callbacks` signed responses at once; reading the request body does not take a slot.
- No IdP-initiated login or logout, signed AuthnRequests or metadata download from the IdP. Without `saml_single_logout`, signing out of the console ends only the local session.
- Encrypted assertions and Single Logout were tested against a local fake IdP (signature wrapping, ciphertext tampering, algorithm downgrade, replay and forged logout responses), not against a real IdP.
- No role from attributes. First-login user creation is opt-in through [JIT provisioning](/docs/features/jit-provisioning/), which reads the e-mail and groups attributes of the signed assertion; [SCIM](/docs/features/scim/) also provisions users.
- Tested against real Keycloak 26.4 through a TLS front end, with Chromium.

## See also

- [OIDC](/docs/features/oidc/) · [Access control](/docs/features/access-control/) · [Security](/docs/security/security/)
- References: [SAML 2.0 core](https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf), [SAML 2.0 profiles](https://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf), [Keycloak SAML clients](https://www.keycloak.org/docs/latest/server_admin/#_client-saml-configuration).
