8000 Remove authentication session during logout if no valid user session is found by pedroigor · Pull Request #40352 · keycloak/keycloak · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove authentication session during logout if no valid user session is found #40352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
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 @@ -39,6 +39,7 @@
import org.keycloak.models.AuthenticatedClientSessionModel;
import org.keycloak.models.ClientModel;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakContext;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
Expand Down Expand Up @@ -413,6 +414,13 @@ private Response doBrowserLogout(AuthenticationSessionModel logoutSession) {
if (userSession == null) {
event.event(EventType.LOGOUT);
event.error(Errors.SESSION_EXPIRED);
KeycloakContext context = session.getContext();
AuthenticationSessionModel authSession = context.getAuthenticationSession();

if (authSession != null) {
// no valid session, make sure the current root auth session is also deleted and restart cookies
new AuthenticationSessionManager(session).removeAuthenticationSession(authSession.getRealm(), authSession, true);
}
} else {
Integer idTokenIssuedAt = Integer.parseInt(idTokenIssuedAtStr);
checkTokenIssuedAt(idTokenIssuedAt, userSession);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.ClientsResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.common.util.Retry;
import org.keycloak.common.util.Time;
import org.keycloak.constants.AdapterConstants;
Expand All @@ -56,9 +57,11 @@
import org.keycloak.testsuite.util.ProtocolMapperUtil;
import org.keycloak.testsuite.util.RealmBuilder;
import org.keycloak.testsuite.util.TokenSignatureUtil;
import org.keycloak.testsuite.util.UserBuilder;
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;
import org.keycloak.testsuite.util.oauth.LogoutResponse;

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

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

for (int i = 0; i < 2; i++) {
realm.users().create(UserBuilder.create()
.username("user-0")
.password("password")
.email("user-0@keycloak")
.firstName("first")
.lastName("last")
.enabled(true)
.build()).close();
UserRepresentation user = ApiUtil.findUserByUsername(realm, "user-0");
Assert.assertNotNull(user);

loginPage.open();
loginPage.login("user-0", "password");

String code = oauth.parseLoginResponse().getCode();
AccessTokenResponse tokenResponse = oauth.accessTokenRequest(code).param(AdapterConstants.CLIENT_SESSION_STATE, "client-session").send();
String idTokenString = tokenResponse.getIdToken();
realm.users().get(user.getId()).remove();
oauth.logoutForm()
.withRedirect()
.idTokenHint(idTokenString)
.postLogoutRedirectUri(oauth.APP_AUTH_ROOT)
.open();

realm.users().create(UserBuilder.create()
.username("user-1")
.password("password")
.email("user-1@keycloak")
.firstName("first")
.lastName("last")
.enabled(true)
.build()).close();

loginPage.open();
loginPage.login("user-1", "password");
code = oauth.parseLoginResponse().getCode();
tokenResponse = oauth.accessTokenRequest(code).param(AdapterConstants.CLIENT_SESSION_STATE, "client-session").send();
idTokenString = tokenResponse.getIdToken();
oauth.logoutForm()
.idTokenHint(idTokenString)
.postLogoutRedirectUri(oauth.APP_AUTH_ROOT)
.open();
user = ApiUtil.findUserByUsername(realm, "user-1");
Assert.assertNotNull(user);
realm.users().get(user.getId()).remove();
}
}

@Test
public void logoutUserByAdmin() {
loginPage.open();
Expand Down
Loading
0