Can I set the token expiry on a user or role or client level, or use a
mix of tokens and Basic auth?
Currently, Keycloak does not offer (out-of-the-box) user- or role-based token expiration. IMO no one in this thread has yet covered how the SSO-related fields influence the access token lifespan. It is also missing references to ID and refresh tokens. I will try to expand on this.
Refresh Token lifespan
Currently, the refresh token lifespan cannot be explicitly set. Nonetheless, one can implicitly set the refresh token by tuning the values SSO Session Idle
, Client Session Idle
, SSO Session Max
, and Client Session Max
. One can read more about it on this SO answer.
ID and Access tokens lifespan
TL;DR One can infer that the ID
and access token lifespan will be equal to the smallest value among (Access Token Lifespan
, SSO Session Max
, and Client Session Max
).
In Keycloak (KC), one can explicitly set the access token lifespan (ATL) at the realm and client levels. Those options are not available for the ID token lifespan, but KC will make it the same as the ATL. Therefore, when one changes the ATL, one indirectly sets the ID token.
By default, the ATL is 5 minutes (except for implicit flow
which is 15 minutes). One can change those values at: Realm > Realm Settings > Tokens
. How it looks on the:


Even though the fields are labeled as 'Access Token', KC uses (as previously mentioned) their values to also set the ID Token
lifespan as well.
Those values can be overridden on a per-client basis in Ones Realm > Clients > ones Client
, then on the:
- old KC UI, one goes to the section
Advanced Settings

- new KC UI, one switches to the tab
Advanced
and goes to the section Advanced Settings

So far so good, but now things will get a bit more interesting.
Interplay with SSO-related fields
As with the refresh token lifespan, the ATL also depends on other values, namely:
SSO Session Max
- and
Client Session Max
.
Let us consider for now only the realm settings SSO Session Max
and Access Token Lifespan
, when:
SSO Session Max
> Access Token Lifespan
, then ATL = Access Token Lifespan
SSO Session Max
<= Access Token Lifespan
, then ATL = SSO Session Max
So, one can infer that the ATL is bound to the smallest value between the Access Token Lifespan
and the SSO Session Max
, which makes sense.
SSO Session Max
is related to Single Sign-ON; we still need to consider the value of Client Session Max
in the realm settings, which when unset, is the same as SSO Session Max
.
If Client Session Max
is set, in the context of the ATL, it will override the value from SSO Session Max
, BUT only if that value is lower than the value from SSO Session Max
.
Let us see the following examples: Access Token Lifespan
(at realm settings) = 30 minutes, SSO Session Max
= 10 hours and:
Client Session Max
= 1 hour, then ATL is equal to the Access Token Lifespan
Client Session Max
= 60 seconds, then ATL is equal to the Client Session Max
So in short one can infer that the ATL will be equal to the smallest value amongst the fields Access Token Lifespan
, SSO Session Max
, and Client Session Max
.
Finally, the fields Access Token Lifespan
and Client Session Max
from the realm settings can be overwritten respectively by the Access Token Lifespan
and Client Session Max
in the clients themselves, which will affect the ATL for that client in particular.
The same logic applies but instead of considering the values Access Token Lifespan
and Client Session Max
from the realm settings one needs to consider those from the client advance settings.