Repository navigation
SSO Keycloak
This page sets up a production Keycloak realm for Goodnight Check. The Compose stack's nsc realm is
a development fixture: it has the same shape (groups, the group-membership mapper, demo users) but it
is not a production configuration. For the generic OIDC/BFF behavior, see
Authentication; the environment variables are in
Configuration → Authentication and sessions.
Tested with Keycloak 26.8.
Check is a confidential OIDC client. It needs:
- a browser-visible realm issuer URL and, if the backend reaches Keycloak on another address, an internal discovery URL;
- a confidential client with the standard authorization-code flow and the exact callback URL;
- a claim in the ID token that carries the user's group or role names;
- the three Check roles:
admin,operator,viewer. The IdP group names are yours; theOIDC_*_GROUPvalues map them to these roles.
The default mapping is a groups claim whose values are the IdP group names, mapped by
OIDC_ADMIN_GROUP, OIDC_OPERATOR_GROUP and OIDC_VIEWER_GROUP. This page uses that default.
A realm that puts roles into a roles claim works the same way: set OIDC_ROLES_CLAIM=roles and
use the IdP role names as the OIDC_*_GROUP values.
- Sign in to the Keycloak admin console.
-
Realm settings → Create realm; name it, for example,
goodnight-check. - Set the browser-visible issuer. The backend's
OIDC_ISSUERis the realm URL:https://keycloak.example.com/realms/goodnight-check. Set Keycloak's hostname to the same browser-visible URL; behind a reverse proxy, configure the proxy headers as well. - If the backend reaches Keycloak at a different address (for example
http://keycloak:8080in a container network), keepOIDC_ISSUERas the browser-visible URL and setOIDC_DISCOVERY_URLto the internal realm URL.
Production uses TLS. Set COOKIE_SECURE=true (the default) and serve Check over HTTPS; the callback
and the issuer must use the same browser-visible origin.
Create three groups for the Check roles. Their names are your choice; for example:
<your-admin-group><your-operator-group><your-viewer-group>
Each name must match the corresponding OIDC_ADMIN_GROUP, OIDC_OPERATOR_GROUP or
OIDC_VIEWER_GROUP value exactly. If your directory already has groups for these roles, use those
names. If you use nested groups, either keep the mapper's Full group path off (the claim carries
the last segment) or set the OIDC_*_GROUP values to the full path (for example
/teams/check-admins).
For each person, Users → Add user: username, email, first/last name, email verified as your policy requires. Set a password under Credentials. Then Groups → Join group and add the user to the group you chose for their Check role.
For a first test, create one user per group. A user can be in more than one group; Check stores every
mapped role and the highest one decides access (admin ⊃ operator ⊃ viewer).
Clients → Create client:
- Client ID:
gn-check, or your chosenOIDC_CLIENT_ID. - Client authentication: On (confidential client).
- Standard flow: On. Direct access grants and service accounts: off unless you need them.
- Valid redirect URIs: the exact
OIDC_REDIRECT_URL, for examplehttps://check.example.com/api/auth/callback. - Web origins: the Check origin, for example
https://check.example.com. - Save, then copy the client secret from Credentials.
Check sends the scopes in OIDC_SCOPES, by default openid,profile,email. Add any additional scopes
your realm requires. The group claim is added in the next step.
The claim must be in the ID token, with the group names Check maps. In the client:
-
Client scopes →
<client-id>-dedicated→ Mappers → Add mapper → By configuration → Group Membership. The dedicated scope belongs to the client, so it is always requested. A separate client scope works too if it is assigned as a default scope. - Set:
- Name:
groups - Token Claim Name:
groups - Full group path: Off (so the claim is the group name, not the full path)
- Add to ID token: On
- Add to access token: On (optional; Check reads the ID token)
- Add to userinfo: On (optional)
- Name:
- Save.
The default OIDC_ROLES_CLAIM=groups reads this claim. If your realm already has a groups claim
with full paths, keep Full group path off or set the OIDC_*_GROUP values to those paths.
To inspect the result: Clients → gn-check → Client scopes → Evaluate, select a user, and open
the generated ID token. The groups claim must contain the expected name.
Set these on the backend; the full list is in Configuration.
| Variable | Set to |
|---|---|
APP_BASE_URL |
https://check.example.com |
OIDC_ISSUER |
https://keycloak.example.com/realms/goodnight-check |
OIDC_DISCOVERY_URL |
http://keycloak:8080/realms/goodnight-check if the backend uses an internal address; otherwise omit |
OIDC_CLIENT_ID |
gn-check |
OIDC_CLIENT_SECRET |
the client secret from step 4 |
OIDC_REDIRECT_URL |
https://check.example.com/api/auth/callback (the default is APP_BASE_URL/api/auth/callback) |
OIDC_SCOPES |
openid,profile,email |
OIDC_ROLES_CLAIM |
groups |
OIDC_ADMIN_GROUP |
<your-admin-group> from step 2 |
OIDC_OPERATOR_GROUP |
<your-operator-group> from step 2 |
OIDC_VIEWER_GROUP |
<your-viewer-group> from step 2 |
COOKIE_SECURE |
true (default; keep it for HTTPS) |
SESSION_TTL |
12h default, 15m to 24h |
The built-in defaults for the group values are admin, operator and viewer; set them to your
group names unless your directory uses those names.
Restart the backend after changing the environment. Keep APP_BASE_URL, OIDC_ISSUER and the
registered redirect URI on the same browser-visible origin; localhost and 127.0.0.1 are not
interchangeable.
- Open Check at
APP_BASE_URLand sign in as each test user. - Check the role: signing in as the admin user shows the admin navigation (for example Admin);
an operator can create scans and change configuration; a viewer has read-only access.
GET /api/auth/mereturns the mapped roles for the session. - In Keycloak, the Evaluate view shows the
groupsclaim; if its value does not match theOIDC_*_GROUPvalue (for example/teams/check-adminswhile the env hascheck-admins), fix Full group path or theOIDC_*_GROUPvalues. - A role change is read at the next login. To cut a live session short, an admin can revoke it under Admin → Sessions; see Authentication → Session revocation.
The Compose development realm (deploy/keycloak/realm-nsc.json) uses this same client shape with
the demo group names admin, operator, viewer (an example, not a requirement), the
group-membership mapper with the full path off, and demo users admin/operator/viewer with
the username as password. It is useful for trying the steps; do not use its credentials in
production.
-
Login loop, or the callback fails:
APP_BASE_URL,OIDC_ISSUERand the registered redirect URI must use the same browser-visible origin. UseOIDC_DISCOVERY_URLonly for internal metadata. See Troubleshooting. -
Signed in with no role: the
groupsclaim is missing or the names do not match. Check the client's Group Membership mapper, that the user is in the mapped group, and that the claim value matches theOIDC_*_GROUPvalue exactly (for example,/teams/check-adminsvscheck-admins). -
401/403after login: the session maps roles at login. Check/api/auth/me, the claim, and the role-gated page; a changed group takes effect at the next login. - Token has no groups claim: the mapper is on a client scope that is not assigned as a default scope, or it is not added to the ID token. Use the client's dedicated scope or assign the scope.
-
The user cannot sign in: the client is confidential, the secret matches
OIDC_CLIENT_SECRET, the user is enabled, and the realm's login settings allow the flow.
- Keep Keycloak and Check on TLS; the session cookie is
Secureby default. - The browser-visible issuer is part of token validation. Changing the Keycloak hostname requires
the same change in
OIDC_ISSUER, and users sign in again; live Check sessions last untilSESSION_TTLor revocation. - Use a dedicated realm and client for Check; do not reuse the development realm.
- Assign the least role that works; the Check admin role controls users, sessions, scanner nodes and settings.
- Role membership is snapshotted at login. Treat group removal as a trigger to revoke the user's live Check sessions; see Authentication.
Published from docs/admin/ in the repository. Edit those files in a pull request; this wiki is overwritten on every push to main and on every v* version tag.
Administration
- Home
- Deployment
- Configuration
- Authentication
- First-run bootstrap
- Operations
- Findings and data
- Troubleshooting
Single sign-on
In the repository