Repository navigation
Conversation
…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]>
Contributor
There was a problem hiding this comment.
🟢 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
sidonly when the issued token’s session type isTRANSIENT. - 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 transientcopy of an existing session (
ONLINE_TRANSIENT_CLIENT/OFFLINE_TRANSIENT_CLIENT) is also represented by a transientuser 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
sidand is then reported as
active: falseby 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) isTRANSIENT, 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_accessin the exchange, and the external-to-internal exchangeused for identity-provider federation), so deployments keep using it and hit this regression-like behaviour.
Change
V1TokenExchangeProvider: the condition forsetSessionId(null)becomestokenContext.getSessionType() == AccessTokenContext.SessionType.TRANSIENT(same code as inStandardTokenExchangeProvider).LegacyTokenExchangeTransientClientSessionTestin the new test framework (login, standard token exchangegiving an
ONLINE_TRANSIENT_CLIENTtoken, legacy exchange of that token must keep thesidand the result must beactive at introspection). It fails without the fix ("the legacy exchange removed the session id") and passes with it.
How it was tested
KC_TEST_SERVER=embedded,tests/base): allorg.keycloak.tests.oauth.tokenexchange.*Test,OfflineTokenExchangeTest,LightWeightAccessTokenTest,FineGrainedAdminWithTokenExchangeTest,FineGrainedAdminWithTokenExchangeDisabledTest: 150 tests, 0 failures, 0 errors.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.keycloak-servicesjar replaced in a vanilla26.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.