Keycloak
Keycloak provides identity and access management for applications, including standards-based sign-in and federation. Start by defining the realm and client boundaries, then decide how applications will validate identity and enforce their own resource permissions.
A working login page is the beginning of the integration. You also need correct redirect handling, token validation, role mapping, logout expectations, administration, database recovery, and upgrade evidence. Identity infrastructure deserves a runbook before it deserves a victory screenshot.
TL;DR
- Use realms for deliberate identity-policy boundaries and clients for application integrations.
- Choose supported protocol libraries and validate issuer, audience, signature, and time constraints.
- Keep resource authorization in the relevant application or explicitly designed policy service.
- Test federation, account linking, authentication flows, logout, and failure behavior.
- Review proxy, hostname, TLS, database, health, and upgrade settings for the deployed version.
Quick Example
This complete offline Python fixture checks a small subset of fictional discovery configuration consistency. Reserved .example URLs are never fetched. It is not a token validator, OIDC implementation, or complete metadata security check.
Keycloak exposes realm-specific discovery at /realms/{realm-name}/.well-known/openid-configuration. Actual clients should use supported protocol implementations with a configured trusted issuer; this example neither discovers nor trusts a remote server. Keycloak OIDC endpoints
Core Concepts
Realms and clients
A realm manages users, credentials, groups, and roles. Clients represent applications or services that integrate with the realm. Realm isolation concerns the identity configuration; it does not by itself isolate shared database administration, host resources, or every application record. Choose a realm boundary because the policy and operational requirements justify it. Keycloak concepts
Authentication and authorization
Authentication establishes identity through a flow. Authorization decides what that identity may do to a resource. A valid token for one client or realm is not permission to read another tenant’s document. Define the required audience and claims, and test object-level permission enforcement where the data is served.
Federation and brokering
User federation connects an external user store; identity brokering connects an external identity provider. Their account lifecycle and trust semantics differ. Review where credentials live, which attributes are authoritative, how accounts link, and what happens when the external source disables or changes a user. Treat linking rules as security decisions.
Design the application integration
Use the client type and flow appropriate to the application. Browser applications cannot keep a distributed client secret confidential. Server-side applications need protected credentials and callback/session handling. For interactive authorization-code integration, configure exact permitted redirects and the appropriate PKCE requirements; do not broaden redirects to make a failing callback disappear.
The following is an integration review matrix. Each outcome needs a real supported client library and an authorized test environment; the offline fixture above does not exercise these behaviors.
Keep protocol checks and business rules separate in the evidence. A backend should use established validation libraries rather than decoding a JWT and trusting the resulting JSON. Include signing-key rotation and clock handling in the test plan. If a key cannot be obtained or validation fails, the application needs a deliberate failure response, not a fallback that accepts unverified claims.
Role design also needs restraint. Map roles to meaningful application capabilities, then check resource ownership and context. Avoid placing every business fact into a long-lived token. Decide how quickly an access change must take effect and how the deployed session/token design meets that requirement.
Operate and upgrade the identity service
For production, review the configured public hostname, TLS termination, trusted proxy behavior, supported database, and health routing. Keycloak’s production guide distinguishes startup/liveness from readiness during initialization; route traffic only to ready instances under the documented deployment model. Production configuration
Limit administration to the people and automation that need it, retain administrative event evidence, and test the emergency recovery process through the approved access path. Do not store administrator credentials in sample realm exports, screenshots, or source control. A realm export is not automatically a complete, restorable identity-service backup.
Before upgrading, read the migration notes across the exact source and target versions. Rehearse with representative configuration, extensions, themes, federation, database state, and client journeys. Keycloak’s upgrade guide describes version-specific changes; a generic container-image rollback is not proof that a migrated database can be downgraded. Upgrade guide
Record the backup, restore procedure, compatibility checks, maintenance decision, and recovery acceptance tests. Test a fresh login as well as existing sessions and downstream application access. A healthy server process can still issue claims that an application interprets incorrectly.
Best Practices
Establish an issuer allowlist
Configure expected issuers in application deployment settings under change review. Do not let a request choose an arbitrary issuer or key endpoint. Confirm both valid and invalid token paths with the chosen library.
Review custom flows as code
Keep a versioned description of flow order, required actions, recovery paths, and extension dependencies. Exercise bypass and failure cases in the authorized environment after changes, rather than checking only the ordinary login path.
Make identity dependencies observable
Measure failed logins, token validation failures, readiness, database health, and external-provider errors without collecting credentials or full tokens. Connect alerts to an owner who understands the application impact.
Common Mistakes
Bad: Treating successful login as complete authorization.
Correct: Test permissions at the API and object boundary for both allowed and denied identities.
Bad: Using wildcard redirects to silence callback errors.
Correct: Resolve the configured origin/path mismatch and permit only the intended callbacks.
Bad: Rolling an image back after a database migration without a tested plan.
Correct: Follow version-specific guidance and rehearse a compatible restore or forward recovery path.
FAQ
Should every application get a separate realm?
Choose realms for actual identity-policy and administrative boundaries. Separate clients may be sufficient within one shared policy domain; test the resulting access design.
Does the Python example validate a token?
No. It checks fictional endpoint consistency only. Actual cryptographic and protocol validation requires an appropriate maintained library and integration tests.
What is the first production acceptance test?
Choose a complete representative journey, then include wrong issuer, wrong audience, denied resource, external-provider failure, and recovery cases. No single login test establishes readiness.
Related Topics
- Identity & Access Management — Define identities and access boundaries.
- Authentication vs Authorization — Separate sign-in from permission decisions.
- Application Security — Review operational and application controls.