Keycloak refresh token rotation and token revocation

Learn how refresh token rotation, offline token rules and not-before revocation limit the damage of a stolen Keycloak token, and what each one means for the applications that hold tokens.

On this page

How Keycloak refresh token rotation and revocation limit a stolen token

When a token is stolen, the security team needs to cut off its use quickly without signing out every user and breaking every application. In Keycloak, the controls for this are refresh token rotation, offline token rules, the Revoke Refresh Token setting, session revocation and not-before policies for a realm, client or user. Each one reaches a different part of the token lifecycle, so the right choice depends on which token was taken and how far the damage needs to be contained.

Access tokens are short-lived, so a stolen access token usually expires on its own within a short window. Refresh tokens carry access for much longer, and an attacker holding one can keep minting new access tokens until the refresh token is invalidated. Rotation issues a new refresh token on each use and invalidates the old one, which lets Keycloak detect reuse of a stolen token. Offline tokens go further because they are designed to outlive the user's session, so teams should review which clients receive them and who is allowed to request them. Confirm the current rotation and offline token settings in the Keycloak server administration guide for your release.

These controls only work if the client applications handle them. A single-page app, mobile app or backend service must store the new refresh token after every refresh, stop using the old one, and force a fresh sign-in when a refresh fails. Revocation endpoints and not-before policies cause refresh attempts to fail, but an application that ignores those failures or caches tokens will keep working until its cached token expires. Before relying on revocation during an incident, test each client type against a revoked session and confirm how the application reports the failure to the user.

Which revocation control fits which incident

This table compares the four revocation controls available for a stolen or misused token in Keycloak, showing what each one invalidates and how it affects applications holding tokens. Read each row as a scope decision: start with the narrowest control that contains the incident, and widen only when the compromise spans more users or clients. Note that exact behavior of each control should be confirmed against the Keycloak version you run.

ControlScope and what it invalidatesEffect on applications holding tokensWhen a security team would choose it
Revoke refresh tokenOne refresh token or the session it belongs to, when the realm setting for refresh token revocation is enabledThe application cannot obtain new access tokens with that refresh token and the user must sign in againA single leaked refresh token from one device or one client
Offline token rulesOffline tokens issued for a client, governed by offline session settings and the revocation of offline sessionsLong-lived offline access ends for affected clients, so background jobs or batch services fail until a new consent or loginCompromise of a service account or batch process that holds offline tokens for long periods
Session revocationA user's sessions in a realm or client, including SSO sessions and their attached tokensUsers are signed out across applications sharing the session, and access tokens remain valid until they expire unless checkedA compromised user account or a suspected session hijack affecting several applications
Realm, client or user not-before policyAll tokens issued before a set time for the whole realm, one client or one userEvery token issued before the cut-off is rejected by resource servers that check the policy, which can log out many users at onceBroad incidents such as a leaked signing-related secret or a client compromise where per-token revocation is impractical
Access token lifetime (for context)Bounds how long any issued access token stays usable without a revocation checkResource servers that do not check revocation accept the token until it expiresChoose a short lifetime so revocation controls matter less during an incident

What each control means for applications that hold tokens

Refresh token rotation and revocation change what an application must do when a session ends, not only what the identity server does. The cards below cover the main application types that hold tokens, and each one describes how the app should handle a failed refresh, a revoked session and a forced re-login.

Web applications with a server-side session

A confidential web app keeps tokens on the server and should treat a failed refresh as the end of its own session. Check that a refresh error clears the local session, sends the user to the login flow, and does not retry in a loop. Confirm that the app's session lifetime does not outlast the Keycloak session it depends on. Use the OIDC library recommended in the Keycloak securing applications guidance so token handling is tested rather than hand-written.

Keycloak docs

Single-page applications

A single-page app runs in the browser and cannot keep a client secret, so it depends on public client flows with PKCE. Refresh tokens held in browser storage are a common exposure point, so confirm where the app stores them and how quickly it discards them. When a refresh fails, the app should drop in-memory tokens, clear any cached user data, and redirect to sign-in. Test this path with a session that Keycloak has revoked in another tab.

Mobile applications

Mobile apps often hold long-lived refresh tokens to avoid repeated sign-in, which makes revocation the main control after a lost or stolen device. Store tokens in the platform secure storage and confirm the app handles a rejected refresh by signing the user out locally. Plan for offline use: if the app queues work while offline, decide what happens to that queue when the token is revoked before it syncs.

Backend services and jobs

Backend services using the client credentials flow usually hold no refresh token, so revocation works through client disablement or secret rotation rather than session end. Services that use a stored user refresh token, such as a sync job that acts for a user, need explicit handling. Confirm the job stops, alerts an operator, and does not fall back to a cached access token when the refresh fails. Record which service accounts act for users.

APIs that validate tokens

A resource server usually validates access tokens locally against cached signing keys and does not see refresh events. A revoked session can therefore leave an access token valid until it expires, so the API's exposure depends on the access token lifetime. Confirm whether the API calls token introspection for sensitive operations, and document the maximum delay between revocation and enforcement. Check that the API rejects tokens whose audience does not match the API.

Applications with offline access

Applications that request the offline_access scope hold offline tokens that do not expire with the user session in the same way. Each such app needs a defined process for the admin or user to revoke its offline access, and the app should detect a revoked offline token at its next use. Inventory which clients request offline access, since a broad default scope can grant it without the app team noticing.

How to set up rotation and revocation in a production realm

This sequence moves a Keycloak realm from default token behavior to a deliberate setup where refresh tokens rotate, offline access is limited to the clients that need it, and revocation has been tested before anyone depends on it during an incident.

  1. Inventory clients that hold refresh and offline tokens

    Before changing any setting, list every client in the realm and record whether it uses the standard flow with refresh tokens, whether it requests the offline_access scope, and which users depend on it. Pull this from the client list in the admin console and from each application team's configuration, since the realm view alone does not show how an application uses tokens. The step is done when you have a written table of clients, token types, owners and the expected behavior of each application when its tokens are revoked. Confirm the list with each owner, because an application that silently fails on a refresh error will look like a Keycloak outage.

  2. Enable revoke refresh token and set reuse behavior

    In the realm token settings, turn on Revoke Refresh Token so that a refresh token is replaced when it is used, and set the reuse behavior the applications can tolerate. Review the setting against the inventory from the previous step, because clients that retry a refresh request after a network failure may need a different reuse setting than clients that never retry. Apply the change in a non-production realm first, if your environment has one, and watch login and refresh traffic. The step is complete when a refresh call returns a new refresh token and the old one is rejected on a second use in the test realm.

  3. Define offline token rules per client

    Offline tokens outlive normal sessions, so grant offline_access only to clients with a documented need, such as a batch job or a mobile client that must work offline. Remove the scope from clients that do not need it, and where the scope is required, record the owner and the reason in your client register. Check the consent screen and the client scopes of each client to confirm that offline access appears only where you expect. The step is done when every client that can issue offline tokens has a recorded justification and no unexplained client still requests the scope.

  4. Test revocation with a non-production user

    Create or pick a test user in a non-production realm, sign in to a test application, and capture its refresh token. Revoke the session or the refresh token from the admin console, then attempt to refresh with the captured token. Repeat the test for a user with an offline session so the offline behavior is also observed. Record which applications surface a clear error and which show a generic failure, because that output tells the support team what users will see. The step is done when the revoked token is rejected and you have notes on the observed client behavior.

  5. Add not-before policies to the incident runbook

    Write a runbook entry that explains the difference between revoking one session, revoking a refresh token, and setting a not-before policy for a realm, a client or a user. For each scope, state who may trigger it, the expected effect on active tokens, and the user communication that follows. Note that a not-before policy affects tokens issued before the set time, so the runbook must say what happens to valid tokens and to users who sign in again immediately afterward. The step is complete when the security operations team has reviewed the runbook and confirmed who holds the admin rights to run each action.

  6. Verify refresh behavior after each revocation

    Run the verification again after the production change, using a dedicated test account in production or a staging copy of the realm, and do not use a real employee account. Revoke a session, attempt a refresh with the old token, and confirm the rejection, then sign in again and confirm a new session works. Repeat for one client that holds offline tokens. Keep the results with the change record, along with the admin event entries that show who made each change. The step is done when each revocation path in the runbook has a dated test result and every failure has an owner.

How a revoked refresh token flows through Keycloak and the application

The flow starts when the application sends a refresh token to the Keycloak token endpoint. Keycloak checks the refresh token against the user session and revocation state, then applies any not-before policy that covers the realm, client or user. If every check passes, Keycloak returns new tokens. If a check fails, it returns an error, and the application must send the user through a fresh sign-in. Teams should confirm that their client library treats that error as a signal to re-authenticate rather than retrying.

Refresh request checked against session and revocation state before tokens are reissued.

Security and operational risks to plan for

Revocation in Keycloak reaches the server-side session and refresh path quickly, but several gaps remain for tokens already in circulation. Each card below names one gap, the failure it causes, and the check to run before go-live.

Access tokens outlive revocation

Revoking a refresh token or session does not invalidate access tokens already issued. Resource servers that validate tokens locally keep accepting them until their exp claim passes. Check the access token lifetime for each client, and confirm which APIs call introspection and which trust the signature alone. The lifetime decision is covered in the token and session lifetime topic.

Keycloak docs

Offline tokens for departed users

Offline tokens do not expire with the user's SSO session, so a token issued to an employee can keep refreshing after they leave. Query the offline sessions for disabled or removed accounts, and set a review or cleanup step in the offboarding runbook. Revoke offline sessions explicitly as part of user deactivation, not only when the account is deleted.

Not-before policies over-revoke

A not-before policy on a realm, client or user invalidates every token issued before that time, including tokens belonging to users who did nothing wrong. Before using it during an incident, decide who will be signed out and whether applications will show a clear re-login path. Test the change in a non-production realm first.

Revocation events left unlogged

Without retained admin and user event records, you cannot show who revoked which session, when, or whether the revocation reached the affected tokens. Enable admin events and login events with the details you need, and forward them to your log platform. Confirm retention meets your incident response and audit requirements.

Clients ignoring failed refresh

When a refresh fails with an invalid grant, some applications retry in a loop, cache the dead token, or fall back to a stored password flow. This hides the revocation from the user and can hammer the token endpoint. Test each client's behavior after you revoke its session, and require it to send the user to a fresh sign-in.

Admins revoking without oversight

Anyone with the manage-users or manage-clients role can revoke sessions for the whole realm, and a compromised administrator account can sign out or hold tokens indefinitely. Limit revocation rights to a small group, use dedicated admin accounts with MFA, and alert on bulk session actions. Rotate any client secrets the affected applications use, as covered in the client secret rotation topic.

Questions teams ask before changing token revocation

How much effort does a refresh token rotation rollout take?

The effort depends mostly on how many clients hold refresh tokens and how each one stores and renews them. A rollout touches every client that uses refresh tokens, so the count of clients is the first driver. Single-page apps, server-side web apps and mobile apps each need a different change, and the team must confirm how each one handles a rotated token that is no longer valid. Custom code that calls the token endpoint directly adds work because it must be found and changed as well. Ask any partner to list the clients, their flows and their token storage before quoting, since the written scope is what sets the effort.

Which parts of a rotation project does the internal team need to handle?

Your team has to supply knowledge that only the organization holds. That includes an inventory of applications and owners, the business tolerance for users being signed out, the release windows for each application, and access to the code that refreshes tokens. Someone must also decide the revocation policy, such as how long a session may stay valid after a suspected theft. Application owners need to test their own login and renewal paths, because they know which user journeys matter. Plan for these people to be available during testing, not only at kickoff.

Can our own team roll out refresh token rotation without a partner?

Yes, if your team already runs Keycloak and maintains the applications involved. The realm and client settings are documented in the Keycloak server administration guide, and the changes themselves are configuration, not new software. The harder work is in the applications, so the question is whether your developers can change refresh logic and test it across client types. Teams that lack Keycloak administration experience, or that have many legacy clients, often use a partner for the review and testing. Decide early which part you will own and which part needs outside help.

How long does testing take as the number of clients grows?

Testing time grows with the number of distinct client types and flows, more than with the raw client count alone. Clients that share one integration pattern can often be tested once and then checked lightly. Each separate pattern, such as a mobile app with offline access, a backend service or a single-page app, needs its own sign-in, renewal, logout and failure tests. Include a test for what happens when a token is reused after rotation. Ask for a test plan that lists each client group and its cases, because that list shows the effort more clearly than any total.

Should a partner review how our applications use offline tokens?

A review is worth requesting when offline tokens are in use, because they keep working for a long time and are harder to revoke than standard refresh tokens. Offline access usually supports background jobs or mobile apps that must work without a user present. The review should confirm which clients request the offline scope, who approved that access, how the tokens are stored, and how they are revoked when a user leaves or a device is lost. Your security team should decide whether the business need justifies the exposure. An outside reviewer adds an independent check, but the decision on risk stays with you.

How does token revocation behave during a Keycloak upgrade?

Revocation settings and existing sessions are stored in the database, so an upgrade must keep them intact. Keycloak migrates its database schema automatically at startup by default, and the upgrading guide suggests manual migration for large deployments. Before upgrading, confirm the release notes for any change to token or session behavior, since minor releases can introduce breaking changes only as opt-in. Test a revocation, a not-before change and an offline token refresh on a staging copy of the production database. Keep the revocation runbook with the upgrade plan so that a fast revoke remains available during and after the change.

Will changing revocation settings sign users out?

Some changes will, and others will not, so check each setting before applying it. A not-before revocation for a realm, client or user invalidates tokens issued before that point, which signs affected users out at their next request. Changing refresh token rotation affects the next renewal, so active sessions may need a fresh sign-in once the old token is no longer accepted. Plan a notice to users and choose a low-traffic window for changes that affect every session. Test with a small group of users first, and confirm the exact behavior for your Keycloak release in its documentation.

Limits to confirm in your own environment

Revocation and refresh token rotation only protect you as far as your Keycloak release, your client libraries, your token storage and your resource servers allow. The items below are the points where the documented design and your real deployment can diverge, so each one needs a test in your own environment before you rely on it.

Behavior in your release line

Revocation and rotation behavior can change between minor releases, and the project says breaking changes in a minor release are opt-in. Confirm how your deployed line handles Revoke Refresh Token, offline token rules and not-before revocation by testing against the exact version you run, currently 26.8.1 per the project, and read the upgrade notes for each step you plan to take.

Keycloak upgrading guide

Client library rotation support

Many OIDC client libraries cache the refresh token and do not replace it when the server issues a new one. If a library keeps the old token after rotation, the next refresh fails and users are signed out unexpectedly. Test each library your applications use with rotation enabled, including the failure path when a reused refresh token is rejected.

Mobile app token storage

Mobile and desktop clients often hold refresh tokens in local storage, a keychain or a plain file, and a stolen device can expose them. Confirm where each client stores tokens, whether the platform keystore protects them, and how the app behaves when a revoked token is returned. Revocation only reduces risk if the client also drops the token it holds.

Reach of revocation to APIs

Revoking a refresh token stops new access tokens, but an access token already issued stays valid until it expires unless your resource servers check revocation or use short lifetimes. Confirm whether each API validates tokens locally from signed JWTs or calls introspection, and record the maximum window in which a stolen access token remains usable.

Revocation event logging retention

Investigating a suspected token theft depends on knowing when a session was revoked, by whom and from which client. Confirm which events Keycloak writes for revocation, logout and token refresh, where they go, and how long your log platform keeps them. Check that the retention period covers your incident response and audit needs before you tune any revocation setting.

Upstream brokered sessions

When users sign in through an upstream identity provider, a revocation in Keycloak may not end the upstream session, and a later sign-in can silently re-establish access. Confirm how logout and revocation propagate through brokered providers, whether backchannel logout is configured on both sides, and how the upstream provider reports session end.

How DeployOpen scopes and delivers token revocation work

DeployOpen starts token revocation work with a written scope agreed before any change is made to your Keycloak. The scope names the realms, clients and applications in play, the token types each one holds (access, refresh and offline tokens), and the revocation controls the work will cover, such as refresh token rotation, the refresh token revocation behavior, not-before policies for a realm, client or user, and session revocation. Deliverables, the environments touched and the acceptance checks are listed in the same document, and a quote follows from that scope. DeployOpen does not publish a price list, so the quote is the place where your team sees each component priced and can ask what was left out.

Delivery begins with an inventory of clients and token use, because the right revocation settings depend on which applications hold refresh tokens, whether any rely on offline tokens for background jobs, and how each one behaves when a token is rejected. DeployOpen then makes the configuration changes inside your own Keycloak, in your own environments, and records each change so your administrators can reproduce it. Verification is part of the scope: test sign-ins, token refresh, forced logout and revocation of a sample session are run against each client, and the results show which applications recover cleanly and which need code changes on their side. Ask whether the scope includes a retest after any remediation.

DeployOpen does not issue audit reports, attestations or certifications. Evidence it produces, such as configuration exports and test records, supports your own internal review or an independent auditor's work, and the auditor decides what it proves. Ongoing review of token lifetimes, revocation behavior and Keycloak upgrades can be covered by a retainer, which recurs and is priced separately from the one-time implementation. Before you sign, confirm who owns the Keycloak administrator accounts used during the work, how changes are handed over, and what the retainer covers between Keycloak releases.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.