How to: use lightweight access tokens with Keycloak
Starting with Keycloak version 24, a token mapper switch "Add to lightweight access token" lets you control which information a lightweight access token carries, separately from the regular "Add to access token" switch. By default, this switch is off for most standard mappers, so a lightweight access token contains only minimal information.
Steps
-
Enable the mappers that must still contribute to lightweight access tokens. By default, most token mappers do not add their claim to lightweight access tokens, which is the intended purpose. If you require specific information to be included (e.g. subject, given name, family name, locale, certain IDs, etc.), enable the "Add to lightweight access token" switch on the mappers that provide this information — these are typically located in the default scope
profile. If the token is going to be processed by the jEAP security library, also enable it on the mapper providing thectxclaim, since the library validates whether an issuer is authorized to authenticate clients in that context. -
Choose how a client gets issued lightweight access tokens, either:
- Always for this client — in the client's configuration, navigate to the "Advanced" tab in the "Advanced Settings" section and set the option "Always use lightweight access token" to "on"; or
- A client profile applied via a client policy — apply a client profile containing the executor "use-lightweight-access-token" to a client via a client policy configured in the "Realm settings" in the "Client policies" tab.
-
For the flexible, opt-in alternative, where a client decides per authentication request whether it needs a lightweight access token, set up a client profile driven by an optional scope:
- Create a new optional scope
lightweight-access-token.- Set the option "Include in token scope" to
on.
- Set the option "Include in token scope" to
- Create a new client profile
lightweight-access-token-profile.- Add the executor
use-lightweight-access-tokento the profile.
- Add the executor
- Create a new client policy
lightweight-access-token-policy.- Add a condition of type
client-scopesto the policy.- Configure the
expected scopesoption with thelightweight-access-tokenscope as an optional scope.
- Configure the
- Add the profile
lightweight-access-token-profileto the policy.
- Add a condition of type
- Add the scope
lightweight-access-tokenas optional scope to the client that should be able to fetch a lightweight access token. - When making an authentication request to Keycloak with the client, include the scope
lightweight-access-tokenin the request'sscopeparameter.
By including or excluding the
lightweight-access-tokenscope in an authentication request, the client can choose whether to request a lightweight access token or not. - Create a new optional scope
With lightweight access tokens, token mappers can be activated on a client for the introspection endpoint but not for access tokens. If the mappers adding a user's authorizations (roles) are not activated on lightweight access tokens for a client, the access tokens issued to that client will contain only limited user information — in particular, they won't contain the user's authorizations. A client using such a token must have the resource query the Keycloak token introspection endpoint to retrieve them; see Token introspection.
Related
- Minimizing access token sizes — why access tokens get large and the other available size-reduction options.
- Token introspection — resolving a lightweight token's authorizations at the authorization server, and caching introspection results.
- How to: enable and debug jEAP token introspection
- How to: use dynamic OAuth2 scopes
- How to: prune roles from access tokens