Skip to main content

OIDC with Keycloak

This page sets up OIDC sign-in with Keycloak. It was validated with Keycloak 26.8: sign-in for each role, no access without a role, both sign-out modes, the CLI, a disabled user losing access at the next re-check, a Keycloak outage, refresh-token rotation, break-glass, and Keycloak behind a private certificate authority.

Keycloak is also the supported way to sign in with Active Directory accounts, through LDAP user federation (not yet tested against AD): see Active Directory.

You need the console's address, for example https://achilles.example.com. The redirect URI is that address plus /api/auth/oidc/callback.

1. Realm​

Use an existing realm or create one for the console. The issuer is the realm's address:

https://keycloak.example.org/realms/<realm>

Keycloak must be served over https. The console accepts http only for an issuer on localhost.

2. Confidential client​

In the realm, open Clients and create a client:

  • Client type: OpenID Connect.
  • Client ID: for example achilles (this is <client-id>).
  • Client authentication: On. The console is a confidential client and authenticates with a client secret.
  • Authentication flow: Standard flow (authorization code). The console needs no other flow.
  • Valid redirect URIs: https://achilles.example.com/api/auth/oidc/callback.
  • Valid post logout redirect URIs: https://achilles.example.com/signed-out (used when OIDC_LOGOUT=provider).

Save, then copy the Client secret from the client's Credentials tab. Put it straight into the installer or deploy.config.env; never paste it into chat, a ticket or mail.

3. Roles​

On the client's Roles tab, create four client roles: admin, operator, analyst, explorer. These names equal the console's default role map, so OIDC_ROLE_MAP can stay unset. If you use other names, set the role map to match (see Roles and the role map).

Give each person one of these roles (Users → the user → Role mapping → Assign role, filtered by clients), or give them to a group. The first console administrator is whoever holds admin.

4. The roles mapper​

The console reads roles from the ID token, in a top-level claim named roles (OIDC_ROLE_CLAIM). Keycloak does not put client roles there by default, so add a mapper:

  1. On the client's Client scopes tab, open <client-id>-dedicated.
  2. Add mapper → By configuration → User Client Role.
  3. Set:
    • Client ID: the console's client (achilles);
    • Token Claim Name: roles;
    • Multivalued: On;
    • Add to ID token: On.
  4. Save.

The claim then holds the person's roles of this client only, for example "roles": ["operator"].

The console can also read a nested claim, such as realm_access.roles, by setting OIDC_ROLE_CLAIM to the dotted path. Whatever you choose, the claim must be in the ID token: the console does not read the access token.

5. Refresh tokens and the re-check​

The console re-checks every signed-in person with Keycloak every OIDC_REVALIDATE_MINUTES (default 60) by using their refresh token. Two things keep that working:

  • Keep offline_access in OIDC_SCOPES (it is in the default). With it, Keycloak issues an offline token, which Keycloak's SSO session idle timeout (30 minutes by default) does not end. Without it, the refresh token ends with the Keycloak session, and the first re-check after that is refused: the console marks the person inactive and ends their sessions, and they must sign in again. The backend warns at start when offline_access is missing.
  • Every person needs the offline_access realm role. People get it through the realm's default roles (default-roles-<realm>); users created in the admin console have it. Users that were imported or come from user federation may not. Without it, sign-in fails: the console shows "Sign-in did not complete" and the backend logs error="not_allowed" ("Offline tokens not allowed for the user or client"). Give such users the default roles, or the offline_access role.

Refresh-token rotation works. With the realm's Revoke Refresh Token on and Refresh Token Max Reuse at 0 (each refresh token works once), the console stored each new token and kept people signed in through every re-check.

Disabling a user ends their access at their next request after the re-check interval: Keycloak refuses the refresh token (invalid_grant), and the console marks the user inactive and ends their console sessions and CLI sign-ins. See When access ends.

6. A private certificate authority​

A LAN Keycloak often has a certificate from the organization's own certificate authority. The console trusts only the public certificate authorities, plus the one CA file you give it in OIDC_CA_CERT_FILE. That file is used for requests to the identity provider only.

Never turn certificate checks off

Without the CA file, sign-in fails with "Your organization's sign-in service could not be reached", and the backend log line ends with a hint from Node.js to run it with --use-system-ca. Do not follow that hint, and do not use NODE_EXTRA_CA_CERTS either: both change what the whole backend trusts. Use OIDC_CA_CERT_FILE. There is no option to skip the check.

  1. Get the CA's certificate in PEM form (a file with one or more -----BEGIN CERTIFICATE----- blocks). The file must hold certificates only: one with a private key in it is refused.
  2. Give it to the console:
    • in the installer wizard, as Private CA file. When Keycloak's certificate is not trusted, the wizard says so and puts you on that field;
    • on an existing install: sudo /opt/projectachilles/achilles-installer auth set --ca-file /root/keycloak-ca.pem (a relative path is read from the repository root).
  3. The deploy script checks the file, copies it to deploy/oidc/ca.pem in the repository, and gives the backend that copy, read-only. Backups made with achilles-installer backup include it, and restore puts it back.

Note that this server trusting the CA is not enough: the console runs in a container and does not use the server's certificate store. The installer says so when it finds that case.

Browsers must trust the CA too, or people get a certificate warning on Keycloak's sign-in page. Distribute the CA's certificate to their machines as you do for other internal sites (on an achilles-ca install, the same way as the ProjectAchilles root).

7. Configure the console​

In the installer wizard, choose Your identity provider (OIDC) on the Login screen and fill in the provider name, the issuer, the client ID, the client secret, the role map and, if needed, the private CA file. Unattended, write a full deploy.config.env (see Server Installer: unattended install), with these lines in place of the three Clerk lines (CLERK_PUBLISHABLE_KEY, CLERK_SECRET_KEY, ADMIN_EMAIL):

AUTH_MODE='oidc'
OIDC_ISSUER='https://keycloak.example.org/realms/<realm>'
OIDC_CLIENT_ID='achilles'
OIDC_CLIENT_SECRET='<client-secret>'
OIDC_PROVIDER_NAME='Keycloak'
OIDC_CA_CERT_FILE='/root/keycloak-ca.pem'

To switch an install that signs in with Clerk today, see Switching an existing install.

With OIDC_LOGOUT=provider, Keycloak may show its own "Do you want to log out?" page, because the console sends no ID-token hint. One click returns to the console's "You are signed out" page, and the next sign-in asks for the password.

Troubleshooting​

For the log command and the general table, see OIDC sign-in: Troubleshooting.

SymptomIn the backend logFix
"Sign-in did not complete" for some users onlyerror="not_allowed"Give the user the default roles or the offline_access realm role (step 5).
"Your account has no role in this console."refused (no-access): the claim "roles" holds no value mapped to an Achilles roleAssign a client role (step 3); check the mapper adds roles to the ID token (step 4).
"Your organization's sign-in service could not be reached"beginning the sign-in failed (provider_unreachable) with UNABLE_TO_VERIFY_LEAF_SIGNATURESet OIDC_CA_CERT_FILE (step 6).
People still allowed in Keycloak are signed out at a re-checkrefreshing a sign-in failed (refused), then was marked inactive: the provider refused the refresh tokenPut offline_access back in OIDC_SCOPES (step 5).