Keycloak - Open Source Red Hat SSO¶
Backend class¶
For Django, add this class path to AUTHENTICATION_BACKENDS. For other
integrations, use the same class path in the framework-specific backend
setting.
Backend name |
Class path |
|---|---|
|
|
Keycloak is an open source IAM and SSO system.
With an email or domain allowlist, the Keycloak response must contain the
boolean email_verified claim set to true. Enable email verification and
ensure the client receives the claim from the same UserInfo response or ID
token as the selected email. If a different email claim is configured, set
SOCIAL_AUTH_KEYCLOAK_EMAIL_VERIFIED_KEY to its matching verification claim.
IdP Setup¶
To configure Keycloak:
Log into your Keycloak Admin Console and select your Realm
Navigate to Clients > Create
Configure the client:
Client ID: Choose a meaningful name (e.g.,
django-app)Client Protocol:
openid-connectAccess Type:
confidentialValid Redirect URIs:
https://your-domain.com/complete/keycloak/
Save and go to the Credentials tab to get the Client Secret
Under Fine Grain OpenID Connect Configuration (found in the client’s Settings or Advanced Settings tab; location may vary depending on Keycloak version), set:
User Info Signed Response Algorithm:
RS256Request Object Signature Algorithm:
RS256
Get the active public key from Realm Settings > Keys > RS256
Create an Audience Mapper for the client to ensure its Client ID is included in the access token’s
audclaim. In recent Keycloak versions, navigate to Client scopes ><client-id>-dedicated> Add mapper > Audience, select the client under Included Client Audience, and enable Add to access token.Note the Authorization URL and Token URL from the Realm OpenID Endpoint Configuration
Application Configuration¶
Add Keycloak to your AUTHENTICATION_BACKENDS:
AUTHENTICATION_BACKENDS = (
...
'social_core.backends.keycloak.KeycloakOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
Configure with values from your Keycloak client:
SOCIAL_AUTH_KEYCLOAK_KEY = 'test-django-oidc'
SOCIAL_AUTH_KEYCLOAK_SECRET = 'a7a41-245e-...'
SOCIAL_AUTH_KEYCLOAK_PUBLIC_KEY = \
'MIIBIjANBxxxdSD'
SOCIAL_AUTH_KEYCLOAK_AUTHORIZATION_URL = \
'https://iam.example.com/auth/realms/voxcloud-staff/protocol/openid-connect/auth'
SOCIAL_AUTH_KEYCLOAK_ACCESS_TOKEN_URL = \
'https://iam.example.com/auth/realms/voxcloud-staff/protocol/openid-connect/token'
Audience validation¶
SOCIAL_AUTH_KEYCLOAK_KEY is the OAuth Client ID. The backend also uses
this value as the expected audience when validating the access token. Therefore,
the exact value configured in SOCIAL_AUTH_KEYCLOAK_KEY must be present in the
token’s aud claim.
The azp (authorized party) claim does not satisfy audience validation. For
example, with SOCIAL_AUTH_KEYCLOAK_KEY = 'test-django-oidc', the access token
should contain claims similar to:
{
"azp": "test-django-oidc",
"aud": ["test-django-oidc"]
}
If the Client ID is missing from aud, configure the Audience Mapper described
above. Otherwise, authentication fails with an audience validation error even
when azp identifies the correct client.
Signing key rotation¶
The Keycloak backend verifies access tokens with the single static key configured
in SOCIAL_AUTH_KEYCLOAK_PUBLIC_KEY. It does not use the JWT header’s kid
(key ID) to select a key and does not fetch keys from Keycloak’s JWKS endpoint.
The configured key must therefore be the active RS256 signing key from the same
realm that issues the token. After a realm signing-key rotation, update
SOCIAL_AUTH_KEYCLOAK_PUBLIC_KEY; otherwise, authentication fails with a
signature verification error. The token’s kid can be compared with the key
ID shown under Realm Settings > Keys to diagnose a mismatch.
For automatic signing-key discovery and rotation, consider using the generic
OIDC (OpenID Connect) backend instead. It uses OpenID Connect discovery, selects keys from
the provider’s JWKS by kid, and refreshes the JWKS when a new key appears.
Proof Key for Code Exchange (PKCE)¶
PKCE is disabled by default. Enable it with:
SOCIAL_AUTH_KEYCLOAK_USE_PKCE = True
The backend then saves a random verifier in the session for each login
transaction, sends its challenge at authorization, and sends the matching
verifier when redeeming the code. The challenge method is S256 unless
SOCIAL_AUTH_KEYCLOAK_PKCE_CODE_CHALLENGE_METHOD says otherwise; keep it at
S256 when Keycloak requires that method. This binds authorization codes to
the initiating login transaction. PKCE complements client authentication; keep
configuring the client secret.
To make Keycloak reject authorization requests without PKCE, turn on Require
PKCE and set PKCE Method to S256 under the client’s Capability
config. Older Keycloak versions have this as Proof Key for Code Exchange
Code Challenge Method in the client’s Advanced tab. Enable the setting
above first, and restart any login attempts that were already in progress when
it changed.
User ID Configuration¶
The default behavior is to associate users via the sub (subject) field from the
JWT token. However, you can configure which field to use as the unique user identifier
by setting:
SOCIAL_AUTH_KEYCLOAK_ID_KEY = 'email'
This can be useful if you want to use email, username, or another field as the unique
identifier instead of the sub field.
With the old default, associations created by older social-core releases used
the normalized preferred_username value and migrate to sub on the next
successful authentication.
Deployments that selected SOCIAL_AUTH_KEYCLOAK_ID_KEY = "sub" before
social-core 6.0 already stored subjects in their older associations. Keep that
setting until those associations have been keyed correctly. A username equal
to another account’s subject must not be used to migrate its association;
see Identifier migration for the evidence requirement
and Django backfill guidance.
Warning
Usernames and email addresses can change or be reassigned. Selecting one as
ID_KEY can allow a different provider account to match a stale local
association.
See the Configurable User ID Key documentation for more information about this feature.
External memberships¶
See External groups for opt-in extraction, group-based login restrictions, and local group synchronization. No separate extraction pipeline step is needed.
Changing account identifier configuration¶
For explicit identifier changes, configure previous LEGACY_ID_KEYS and
ensure the new stable identifier was stored before changing ID_KEY.
See Identifier migration. Merely recording the old key
or presenting a matching username does not prove ownership of the association.