Skip to content

Client sessions attached to offline user sessions via token exchange are created with OFFLINE_FLAG=0, causing orphaned OFFLINE_CLIENT_SESSION rows #52139

Closed

Description

@ahus1

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?

  1. Log in a user with a client that has the offline_access scope, obtain an offline refresh token, and refresh it to get an access token bound to the offline user session.
  2. Use that access token as subject_token in a V1 token exchange request (grant_type=urn:ietf:params:oauth:grant-type:token-exchange) targeting a different client.
  3. Inspect the database: a new row appears in OFFLINE_CLIENT_SESSION with OFFLINE_FLAG=0, linked via USER_SESSION_ID to a row in OFFLINE_USER_SESSION with OFFLINE_FLAG=1.
  4. 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: calls attachAuthenticationSession with 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)

Activity

  1. added
    kind/bug
    on Aug 27, 2026
  2. added
    area/token-exchange
    on Aug 27, 2026
  3. added theissue type on Aug 27, 2026
  4. self-assigned this
    on Aug 27, 2026
  5. moved this from Backlog to In progress in Keycloak Roadmapon Sep 10, 2026
  6. 1 remaining item

  7. ahus1 commented on Oct 2, 2026

    @ahus1
    MemberAuthor

    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.

  8. added a commit that references this issue on Oct 2, 2026
    b94f8c7
  9. added a commit that references this issue on Oct 2, 2026
    0682684
  10. added a commit that references this issue on Oct 2, 2026
    ea2533d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Fields

No fields configured for bug.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions