8000 Set client in the session context for logout token encode by rmartinc · Pull Request #41004 · keycloak/keycloak · GitHub
[go: up one dir, main page]

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 @@ -193,12 +193,17 @@ protected Response sendBackChannelLogoutRequestToClientUri(ClientModel resource,
AuthenticatedClientSessionModel clientSessionModel, String managementUrl) {
UserModel user = clientSessionModel.getUserSession().getUser();

LogoutToken logoutToken = session.tokens().initLogoutToken(resource, user, clientSessionModel);
String token = session.tokens().encode(logoutToken);
if (logger.isDebugEnabled())
logger.debugv("logout resource {0} url: {1} sessionIds: ", resource.getClientId(), managementUrl);
HttpPost post = null;
ClientModel previousClient = session.getContext().getClient();
try {
session.getContext().setClient(resource);

LogoutToken logoutToken = session.tokens().initLogoutToken(resource, user, clientSessionModel);
String token = session.tokens().encode(logoutToken);
if (logger.isDebugEnabled()) {
logger.debugv("logout resource {0} url: {1} sessionIds: ", resource.getClientId(), managementUrl);
}

post = new HttpPost(managementUrl);
List<NameValuePair> parameters = new LinkedList<>();
if (logoutToken != null) {
Expand All @@ -223,6 +228,7 @@ protected Response sendBackChannelLogoutRequestToClientUri(ClientModel resource,
ServicesLogger.LOGGER.logoutFailed(e, resource.getClientId());
return Response.serverError().build();
} finally {
session.getContext().setClient(previousClient);
if (post != null) {
post.reset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.keycloak.common.util.Retry;
import org.keycloak.common.util.Time;
import org.keycloak.constants.AdapterConstants;
import org.keycloak.crypto.Algorithm;
import org.keycloak.events.Details;
import org.keycloak.jose.jws.JWSHeader;
import org.keycloak.jose.jws.JWSInput;
Expand All @@ -51,6 +52,7 @@
import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.admin.ApiUtil;
import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.updaters.ClientAttributeUpdater;
import org.keycloak.testsuite.updaters.RealmAttributeUpdater;
import org.keycloak.testsuite.util.ClientManager;
import org.keycloak.testsuite.util.Matchers;
Expand All @@ -60,6 +62,7 @@
import org.keycloak.testsuite.util.UserBuilder;
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;
import org.keycloak.testsuite.util.oauth.LogoutResponse;
import org.keycloak.testsuite.util.oauth.OAuthClient;

import java.io.IOException;
import java.util.LinkedList;
Expand Down Expand Up @@ -181,6 +184,46 @@ public void postLogoutFailWithCredentialsOfDifferentClient() throws Exception {
oauth.client("test-app", "password");
}

@Test
public void logoutBackchannelTwoClientsSpecificConfigurationIsUsed() throws Exception {
final String defaultSignatureAlgorithm = adminClient.realm(oauth.getRealm()).toRepresentation().getDefaultSignatureAlgorithm();
final String differentAlg = Algorithm.RS256.equals(defaultSignatureAlgorithm) ? Algorithm.RS512 : Algorithm.RS256;
try (ClientAttributeUpdater updater = ClientAttributeUpdater.forClient(adminClient, oauth.getRealm(), oauth.getClientId())
.setAttribute(OIDCConfigAttributes.ID_TOKEN_SIGNED_RESPONSE_ALG, differentAlg)
.setAttribute(OIDCConfigAttributes.BACKCHANNEL_LOGOUT_URL, OAuthClient.APP_ROOT + "/admin/backchannelLogout")
.update()) {

// login with test-app
oauth.doLogin("test-user@localhost", "password");
AccessTokenResponse tokenResponse = oauth.accessTokenRequest(oauth.parseLoginResponse().getCode())
.param(AdapterConstants.CLIENT_SESSION_STATE, "client-session").send();
Assert.assertNull(tokenResponse.getError());

// login with test-app-scope
oauth.client("test-app-scope", "password");
oauth.openLoginForm();
AccessTokenResponse tokenResponse2 = oauth.accessTokenRequest(oauth.parseLoginResponse().getCode())
.param(AdapterConstants.CLIENT_SESSION_STATE, "client-session").send();
Assert.assertNull(tokenResponse2.getError());
AccessToken accessToken = new JWSInput(tokenResponse2.getAccessToken()).readJsonContent(AccessToken.class);

// logout from test-app-scope
oauth.logoutForm().idTokenHint(tokenResponse2.getIdToken()).open();

// check test-app backchannel is received
String rawLogoutToken = testingClient.testApp().getBackChannelRawLogoutToken();

// check the logout token is OK and using correct signature algorithm
JWSInput jwsInput = new JWSInput(rawLogoutToken);
assertEquals(differentAlg, jwsInput.getHeader().getRawAlgorithm());
LogoutToken logoutToken = jwsInput.readJsonContent(LogoutToken.class);
validateLogoutToken(logoutToken);
JWSHeader logoutTokenHeader = jwsInput.getHeader();
assertEquals("logout+jwt", logoutTokenHeader.getType());
assertEquals(accessToken.getSubject(), logoutToken.getSubject());
}
}

@Test
public void testRemoveAuthSessionWhenUserSessionFromIdTokenIsInvalid() throws IOException {
RealmResource realm = adminClient.realm("test");
Expand Down
0