Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public void executeOnEvent(ClientPolicyContext context) throws ClientPolicyExcep
case SERVICE_ACCOUNT_TOKEN_REQUEST:
case BACKCHANNEL_TOKEN_REQUEST:
case DEVICE_TOKEN_REQUEST:
case TOKEN_EXCHANGE_REQUEST:
case JWT_AUTHORIZATION_GRANT:
session.setAttribute(Constants.USE_LIGHTWEIGHT_ACCESS_TOKEN_ENABLED, true);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import jakarta.ws.rs.core.Response;

import org.keycloak.OAuth2Constants;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.ClientScopeResource;
import org.keycloak.admin.client.resource.ProtocolMappersResource;
import org.keycloak.admin.client.resource.RealmResource;
Expand Down Expand Up @@ -1117,4 +1118,42 @@ public void testUserInfoWithLightweightTokenServerWideOverridesClient() throws E
alwaysUseLightWeightAccessToken(false);
}
}

@Test
public void testPolicyLightWeightTokenExchangeTest() throws Exception {
setUseLightweightAccessTokenExecutor();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Using setUseLightweightAccessTokenExecutor() is the established pattern across LightWeightAccessTokenTest for validating lightweight token behavior. The test explicitly asserts that the token exchange response returns a lightweight token context (TokenType.LIGHTWEIGHT), successfully covering the new event mapping in UseLightweightAccessTokenExecutor while keeping the diff within testsuite limits


ProtocolMappersResource protocolMappers = setProtocolMappers(false, true, true, false);
try {
ClientManager.realm(adminClient.realm(REALM_NAME)).clientId(TEST_CLIENT).alwaysUseLightweightAccessToken(false);
ClientResource clientResource = AdminApiUtil.findClientResourceByClientId(adminClient.realm(REALM_NAME), TEST_CLIENT);
ClientRepresentation clientRep = clientResource.toRepresentation();
if (clientRep.getAttributes() == null) clientRep.setAttributes(new java.util.HashMap<>());
clientRep.getAttributes().put("standard.token.exchange.enabled", "true");
clientResource.update(clientRep);
oauth.scope("address");
oauth.client(TEST_CLIENT, TEST_CLIENT_SECRET);
AccessTokenResponse response = browserLogin(TEST_USER_NAME, TEST_USER_PASSWORD).tokenResponse;
assertAccessToken(oauth.verifyToken(response.getAccessToken()), true, true, true);
AccessTokenResponse exchangeResponse = oauth.tokenExchangeRequest(response.getAccessToken()).send();
String exchangedTokenString = exchangeResponse.getAccessToken();
org.junit.Assert.assertNotNull("Token exchange response should contain access token", exchangedTokenString);
AccessTokenContext ctx = runOnServer.fetch(RunHelpers.getTokenContext(oauth.verifyToken(exchangedTokenString).getId()));
Assertions.assertEquals(AccessTokenContext.TokenType.LIGHTWEIGHT, ctx.getTokenType());
oauth.client(RESOURCE_SERVER_CLIENT_ID, RESOURCE_SERVER_CLIENT_PASSWORD);
String tokenResponse = oauth.doIntrospectionAccessTokenRequest(exchangedTokenString).getRaw();
assertTokenIntrospectionResponse(JsonSerialization.readValue(tokenResponse, AccessToken.class), true, true, false);
} finally {
try {
ClientResource cr = AdminApiUtil.findClientResourceByClientId(adminClient.realm(REALM_NAME), TEST_CLIENT);
ClientRepresentation crRep = cr.toRepresentation();
if (crRep.getAttributes() != null) {
crRep.getAttributes().remove("standard.token.exchange.enabled");
cr.update(crRep);
}
} catch (Exception ignored) {
}
deleteProtocolMappers(protocolMappers);
}
}
}
Loading