NokiaTube | New address | Mode Desktop | On high quality
Skip to content

Keep the session id in legacy token exchange of transient client session tokens - #55620

Open
primax79 wants to merge 1 commit into
keycloak:mainfrom
primax79:fix/55617-v1-exchange-sid
Open

primax79 wants to merge 1 commit into
keycloak:mainfrom
primax79:fix/55617-v1-exchange-sid

Conversation

@primax79

Copy link
Copy Markdown

Closes #55617

The legacy (V1) token exchange removes the session id from the issued access token when the target user session is
TRANSIENT. That check looks at the persistence state of the user session object, but a token bound to a transient
copy of an existing session (ONLINE_TRANSIENT_CLIENT / OFFLINE_TRANSIENT_CLIENT) is also represented by a transient
user session, even though it still refers to a real session. Typical inputs are an RPT obtained with the UMA grant, or a
token issued by the standard token exchange to a client without a client session. The exchanged token loses its sid
and is then reported as active: false by the introspection endpoint.

#42565 already fixed the same condition for the standard token exchange (V2): the session id is removed only when the
token session type (AccessTokenContext.SessionType) is TRANSIENT, i.e. the token is not linked to any session.
This PR applies the same check to V1TokenExchangeProvider.

Why touch V1 although it is deprecated: it is still the only option for several cases that the standard token exchange
does not cover (public clients as requester, offline_access in the exchange, and the external-to-internal exchange
used for identity-provider federation), so deployments keep using it and hit this regression-like behaviour.

Change

  • V1TokenExchangeProvider: the condition for setSessionId(null) becomes
    tokenContext.getSessionType() == AccessTokenContext.SessionType.TRANSIENT (same code as in
    StandardTokenExchangeProvider).
  • New test LegacyTokenExchangeTransientClientSessionTest in the new test framework (login, standard token exchange
    giving an ONLINE_TRANSIENT_CLIENT token, legacy exchange of that token must keep the sid and the result must be
    active at introspection). It fails without the fix ("the legacy exchange removed the session id") and passes with it.

How it was tested

  • New test above, embedded server: fails before the fix, passes after.
  • Regression, new test framework (KC_TEST_SERVER=embedded, tests/base): all org.keycloak.tests.oauth.tokenexchange.*Test,
    OfflineTokenExchangeTest, LightWeightAccessTokenTest, FineGrainedAdminWithTokenExchangeTest,
    FineGrainedAdminWithTokenExchangeDisabledTest: 150 tests, 0 failures, 0 errors.
  • Legacy Arquillian tests (undertow, testsuite/integration-arquillian/tests/base): ClientTokenExchangeSAML2Test 7/7, StandardTokenExchangeV2WithLegacyTokenExchangeTest 33/33, SubjectImpersonationTokenExchangeV1Test 8/8 pass. StandardTokenExchangeV1Test (10 of 15 pass) and KcOidcBrokerTokenExchangeTest (0 of 12 pass) show errors in my local setup (feature flags of the legacy testsuite not applied, fine-grained permissions v2 used instead of v1); the same errors occur identically without the fix, so they are environmental and I could not verify those two classes locally. CI results for them are needed.
  • Released version check: the same one-line change applied on tag 26.8.2, keycloak-services jar replaced in a vanilla
    26.8.2 container. With a small script (realm with a client, UMA, V1 exchange of the RPT, introspection of the result):
    before: 2b token exchange of the RPT (V1, app -> app) sid=NO active=False;
    after: 2b token exchange of the RPT (V1, app -> app) sid=yes active=True.

AI assistance disclosure

Generative AI tools (Claude Code) were used to assist with this contribution: investigation of the root cause,
the code change and the test were drafted with the tool from a prompt, then reviewed, run and verified by me. I
understand the change and can explain and revise it.

…ion tokens

The legacy (V1) token exchange removed the session id from the issued access
token whenever the target user session was TRANSIENT. A token bound to a
transient copy of an existing session (ONLINE_TRANSIENT_CLIENT or
OFFLINE_TRANSIENT_CLIENT, for example an RPT or a token issued by the standard
token exchange) still refers to a real user session, so the exchanged token was
rejected at introspection.

Use the same check as StandardTokenExchangeProvider (keycloak#42565): remove the session
id only when the token session type is TRANSIENT.

Closes keycloak#55617

Signed-off-by: Alfredo Oliviero <[email protected]>
@primax79
primax79 requested a review from a team as a code owner November 27, 2026 12:41
Copilot AI balanced review requested due to automatic review settings November 27, 2026 12:41
@stianst stianst added the kind/bug Categorizes a PR related to a bug label Nov 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the legacy token-exchange session-ID preservation change.

No open issues 🎉

What changed in this PR

Aligns legacy (V1) token exchange with V2 session-ID handling, addressing #55617 so tokens tied to existing sessions retain their sid and remain active at introspection.

Changes:

  • Clear sid only when the issued token’s session type is TRANSIENT.
  • Add a regression test covering standard-to-legacy exchange, session-ID preservation, and active introspection.
File Description
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​oauth/​tokenexchange/​LegacyTokenExchangeTransientClientSessionTest.java Tests legacy exchange of an online transient-client token.
services/​src/​main/​java/​org/​keycloak/​protocol/​oidc/​tokenexchange/​V1TokenExchangeProvider.java Uses token session type to decide whether to clear sid.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes a PR related to a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Legacy token exchange (V1) removes sid for ONLINE_TRANSIENT_CLIENT sessions, the exchanged token is inactive (same condition fixed in V2 by #42565)

3 participants