Catalogue / keycloak-realm-access-design
keycloak-realm-access-design
Older skill: source not recordedSignedPlans and troubleshoots Keycloak identity setups: realms, client types, role models, browser and MFA flows, token and session lifetimes, LDAP federation, themes, OIDC endpoints, and container diagnostics. Use when the user mentions "configure a Keycloak realm", "set up SSO or OIDC for my app", "add…--- name: keycloak-realm-access-design description: >- Plans and troubleshoots Keycloak identity setups: realms, client types, role models, browser and MFA flows, token and session lifetimes, LDAP federation, themes, OIDC endpoints, and container diagnostics. Use when the user mentions "configure a Keycloak realm", "set up SSO or OIDC for my app", "add MFA or OTP to login", "federate LDAP or Active Directory", or asks why a Keycloak login or token is failing. Not for obtaining credentials, editing identities or realm data without the user's confirmation, or non-Keycloak identity products. ---
Keycloak Realm and Access Design
Before proposing changes, learn the deployment: installed Keycloak version, which realm is in play, existing clients and roles, where users come from, and how the server runs (Compose, Kubernetes, bare binary). Port numbers, health paths, and kc.sh export/import flags differ between versions, so read the installed instance rather than assuming.
Realm roles versus client roles
| Layer | Use it for | Example | |---|---|---| | Realm roles | coarse standing in the organisation | platform-admin, member, visitor | | Client roles | scoped permissions inside one resource API | reports:read, reports:write, reports:delete, reports:admin |
Holding a role in a token does not by itself authorise every operation — spell out which role unlocks which action, and check enforcement in the resource, not only in the token.
Realm-level policy decisions to make explicitly: self-registration on or off, email as username, verified-email requirement, brute-force detection (a common starting point is a lockout counter of 5 failed attempts with a gradually growing wait), and TLS requirements (external behind a TLS-terminating proxy, all for direct production traffic).
Two client shapes
Confidential backend client (machine or server side):
{
"clientId": "reports-api",
"enabled": true,
"publicClient": false,
"clientAuthenticatorType": "client-secret",
"serviceAccountsEnabled": true,
"protocol": "openid-connect"
}Direct access grants (the resource-owner password grant) are off by default in modern advice; enable only when a reviewed requirement demands it.
Public browser client (SPA):
{
"clientId": "reports-web",
"enabled": true,
"publicClient": true,
"standardFlowEnabled": true,
"implicitFlowEnabled": false,
"directAccessGrantsEnabled": false,
"redirectUris": ["http://localhost:5173/callback"],
"webOrigins": ["+"],
"protocol": "openid-connect",
"attributes": { "pkce.code.challenge.method": "S256" }
}Rules of thumb: public clients never hold a secret and always use the authorisation-code flow with PKCE (S256); keep implicit flow and password grants disabled; list exact redirect URIs and origins instead of wildcards.
Lifetimes worth reviewing
| Setting | Typical starting value | |---|---| | Access token lifespan | 3,600 s | | Implicit token (if ever enabled) | 900 s | | SSO session idle | 1,800 s | | SSO session max | 36,000 s | | Offline session idle | 2,592,000 s (30 days) |
Treat these as a baseline to debate, not a policy: shorter tokens cut exposure but cost user experience. Turn on audit logging for sign-ins, role changes and admin actions, and keep secrets out of the log lines.
Authentication flows
The stock browser flow is: cookie authentication (alternative) → identity- provider redirector (alternative) → username/password form (required) → conditional OTP. To require MFA for privileged users, clone the flow rather than editing the built-in one, then add a condition on the role:
portal-mfa-flow 1. Username Password Form REQUIRED 2. Conditional OTP CONDITIONAL - condition: user role = platform-admin - OTP Form REQUIRED
WebAuthn/passwordless and recovery-code requirements depend on the installed version's flow capabilities — inspect what the instance offers before promising them. Test the flow with both an admin and a regular account before rollout.
User federation
For LDAP or Active Directory, pin down: vendor, an LDAPS connection URL, the users DN, the username LDAP attribute (for Active Directory commonly sAMAccountName), edit mode (read-only is the safe default), and sync mode (import versus periodic). Verify the real directory schema against a test bind before switching production users over. Third-party brokers — SAML or OIDC providers, social logins — are configured as identity providers with reviewed mappers and access rules.
Endpoints and admin API
Every realm publishes a discovery document; prefer it over hard-coded URLs:
/realms/<realm>/.well-known/openid-configuration
| Purpose | Path under /realms/<realm>/protocol/openid-connect | |---|---| | Authorisation | auth | | Token | token | | User info | userinfo | | Logout | logout | | JWKS keys | certs |
Admin REST lives under /admin/realms/<realm>/ — for example GET .../users, GET .../roles, POST .../clients. Introspection (token/introspect) is an authenticated call: the caller authenticates as a client; never paste bearer tokens into unauthenticated commands or logs. Let the user supply the admin login themselves; do not fetch or echo credentials.
Themes
A theme splits by page type, each with its own properties, stylesheets and message bundles:
themes/<name>/ ├── theme.properties # parent, import, locales ├── login/ │ ├── theme.properties │ ├── resources/css/login.css │ └── messages/messages_en.properties ├── account/ └── email/
parent=keycloak import=common/keycloak styles=css/login.css locales=en
Confirm the deployment's theme directory and caching setup before editing; custom messages override the parent's localised strings per locale.
Container lifecycle and diagnosis
- Start (dev): bring up the Keycloak service together with its database via
the project's Compose file — ask before starting containers.
- Export/import:
kc.sh export --dir <dir> --users realm_fileand
kc.sh import --dir <dir>, run inside the container. Exports contain real users; agree on the destination and its protection first. Flags vary by version, so check the installed kc.sh help.
- Logs: follow the container's recent output when login fails.
- Readiness: hit the deployment's health endpoint (path varies by version)
and the discovery document; a green health page says nothing about authorisation correctness.
Test coverage for any change: login, MFA prompt, role-based access, PKCE handshake on the SPA, federation sign-in, logout, and session timeout. Report anything you could not verify.
Never
- Use, print, or bake in default admin credentials, or write commands that
carry a client secret or bearer token in plain sight — have the user supply and unlock secrets.
- Enable wildcard redirect URIs, implicit flow, or password grants because an
example did; each needs a stated reason and the user's approval.
- Change clients, roles, flows, federation, themes, or import/export realm
data without the user's explicit go-ahead and a stated rollback.
- Treat a passing health check or a valid token as proof that access control
works.
| File | Size | SHA-256 |
|---|---|---|
| SKILL.md | 7.4 kB | fc561ab6056e |
| examples.json | 1.9 kB | 58b504fc4ecb |
Published verbatim from the pinned commit. Nothing is rewritten.
| Version | Pinned commit | Signed | |
|---|---|---|---|
| 1.0.1 Current | Not recorded | 6 Oct 2026 | |
| 1.0.0 | Not recorded | 2 Oct 2026 |
Locking a skill to one version needs Pro.