Fix cascade removal of client session on user session removal for CHM

Closes #12146
This commit is contained in:
Michal Hajas 2022-05-24 09:43:03 +02:00 committed by Hynek Mlnařík
parent cf386efa40
commit 1a98765fb7
3 changed files with 31 additions and 2 deletions

View file

@ -129,7 +129,12 @@ public class ConcurrentHashMapStorage<K, V extends AbstractEntity & UpdatableEnt
@SuppressWarnings("unchecked")
public MapKeycloakTransaction<V, M> createTransaction(KeycloakSession session) {
MapKeycloakTransaction<V, M> sessionTransaction = session.getAttribute("map-transaction-" + hashCode(), MapKeycloakTransaction.class);
return sessionTransaction == null ? new ConcurrentHashMapKeycloakTransaction<>(this, keyConverter, cloner, fieldPredicates) : sessionTransaction;
if (sessionTransaction == null) {
sessionTransaction = new ConcurrentHashMapKeycloakTransaction<>(this, keyConverter, cloner, fieldPredicates);
session.setAttribute("map-transaction-" + hashCode(), sessionTransaction);
}
return sessionTransaction;
}
public MapModelCriteriaBuilder<K, V, M> createCriteriaBuilder() {

View file

@ -92,6 +92,11 @@ public class UserSessionConcurrentHashMapStorage<K> extends ConcurrentHashMapSto
@SuppressWarnings("unchecked")
public MapKeycloakTransaction<MapUserSessionEntity, UserSessionModel> createTransaction(KeycloakSession session) {
MapKeycloakTransaction<MapUserSessionEntity, UserSessionModel> sessionTransaction = session.getAttribute("map-transaction-" + hashCode(), MapKeycloakTransaction.class);
return sessionTransaction == null ? new Transaction(clientSessionStore.createTransaction(session), clientSessionStore.getKeyConverter(), cloner, fieldPredicates) : sessionTransaction;
if (sessionTransaction == null) {
sessionTransaction = new Transaction(clientSessionStore.createTransaction(session), clientSessionStore.getKeyConverter(), cloner, fieldPredicates);
session.setAttribute("map-transaction-" + hashCode(), sessionTransaction);
}
return sessionTransaction;
}
}

View file

@ -20,6 +20,7 @@ import org.junit.Assert;
import org.junit.Test;
import org.keycloak.common.util.Time;
import org.keycloak.models.AuthenticatedClientSessionModel;
import org.keycloak.models.ClientModel;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
@ -42,6 +43,8 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.keycloak.testsuite.model.session.UserSessionPersisterProviderTest.createClients;
import static org.keycloak.testsuite.model.session.UserSessionPersisterProviderTest.createSessions;
@ -202,4 +205,20 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
}
}
}
@Test
public void testCascadeRemovalOfClientSessionOnUserSessionRemoval() {
UserSessionModel[] origSessions = inComittedTransaction(session -> { return createSessions(session, realmId); });
String testAppClientSessionId = withRealm(realmId, (session, realm) -> {
ClientModel testApp = realm.getClientByClientId("test-app");
UserSessionModel userSessionToBeRemoved = session.sessions().getUserSession(realm, origSessions[0].getId());
String returnValue = userSessionToBeRemoved.getAuthenticatedClientSessions().get(testApp.getId()).getId();
session.sessions().removeUserSession(realm, userSessionToBeRemoved);
return returnValue;
});
assertThat(withRealm(realmId, (session, realm) -> session.sessions().getClientSession(origSessions[0], realm.getClientByClientId("test-app"), testAppClientSessionId, false)), nullValue());
}
}