Repository navigation
Description
Before reporting an issue
- I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.
Area
token-exchange
Describe the bug
When exchanging a token whose underlying UserSessionModel is an offline session (e.g. because the subject_token was obtained from an offline token), the legacy internal-internal token exchange (V1TokenExchangeProvider.exchangeClientToOIDCClient) attaches a new client session to that user session via TokenManager.attachAuthenticationSession(), which in turn calls UserSessionProvider.createClientSession(realm, client, userSession).
The root cause is that createClientSession is the wrong method for offline sessions. The correct path is createOfflineClientSession, which in InfinispanUserSessionProvider both writes to the Infinispan offline cache and explicitly calls UserSessionPersisterProvider.createClientSession(clientSession, true) to persist the row in the JPA layer. createClientSession only writes to the Infinispan online cache and never calls the persister, so an offline client session created this way is incomplete: it is missing from persistent storage and its rows end up with the wrong flag.
Since PERSISTENT_USER_SESSIONS is a Type.DEFAULT feature, both OFFLINE_USER_SESSION and OFFLINE_CLIENT_SESSION tables store both online and offline sessions, distinguished only by the OFFLINE_FLAG column. Client sessions created via the buggy code path end up persisted with OFFLINE_FLAG=0 while their parent user session row has OFFLINE_FLAG=1.
Maintenance/cleanup code joins client and user sessions on (USER_SESSION_ID, OFFLINE_FLAG), assuming the flags always match. As a result, these mismatched client session rows are never cleaned up and accumulate indefinitely as orphans in OFFLINE_CLIENT_SESSION (reported: >1,000,000 orphaned rows over time in a production deployment).
The V2 standard token exchange (StandardTokenExchangeProvider) already handles this correctly: when the target session is offline it wraps it in a transient user session before calling attachAuthenticationSession, so createClientSession is never called with a live offline session.
Version
26.0.0 and later, including current main
Regression
Not applicable / not checked — this is present since the introduction of PERSISTENT_USER_SESSIONS as Type.DEFAULT in 26.0.0.
Expected behavior
When a client session is attached to an offline UserSessionModel during token exchange, it should be created/persisted as an offline client session (OFFLINE_FLAG=1), so that cleanup jobs can correctly remove it when the parent user session expires or is removed.
Actual behavior
The client session is created via the wrong code path, resulting in an OFFLINE_FLAG=0 row in OFFLINE_CLIENT_SESSION linked to an OFFLINE_FLAG=1 row in OFFLINE_USER_SESSION. Cleanup jobs never match/remove these rows, causing orphaned rows to grow continuously.
How to Reproduce?
- Log in a user with a client that has the
offline_accessscope, obtain an offline refresh token, and refresh it to get an access token bound to the offline user session. - Use that access token as
subject_tokenin a V1 token exchange request (grant_type=urn:ietf:params:oauth:grant-type:token-exchange) targeting a different client. - Inspect the database: a new row appears in
OFFLINE_CLIENT_SESSIONwithOFFLINE_FLAG=0, linked viaUSER_SESSION_IDto a row inOFFLINE_USER_SESSIONwithOFFLINE_FLAG=1. - Revoke/expire the offline user session — the mismatched client session row is not removed and remains orphaned.
Anything else?
Relevant code:
services/src/main/java/org/keycloak/protocol/oidc/tokenexchange/V1TokenExchangeProvider.java—exchangeClientToOIDCClient()(root cause: callsattachAuthenticationSessionwith a live offline session)services/src/main/java/org/keycloak/protocol/oidc/TokenManager.java—attachAuthenticationSession()model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java—createOfflineClientSession()(correct path for offline sessions, also calls the JPA persister)
Metadata
Metadata
Assignees
Labels
Type
Fields
Projects
- StatusShow more project fieldsDone
Activity
1 remaining item
ahus1 commentedon Oct 2, 2026
NOTE: This fix is planned to be backported to 26.8, but not to any earlier version. Token Exchange V1 will be removed in Keycloak 27, so everyone using it today should migrate away from it.
Avoid orphaned offline client sessions for V1 token exchange
Avoid orphaned offline client sessions for V1 token exchange
Avoid orphaned offline client sessions for V1 token exchange