notes, String userSessionId, String clientId) {
super(realmId);
this.authMethod = authMethod;
this.redirectUri = redirectUri;
this.timestamp = timestamp;
this.action = action;
this.notes = notes;
- this.id = id;
- }
-
- @Override
- public int hashCode() {
- return id != null ? id.hashCode() : 0;
- }
-
- @Override
- public SessionEntityWrapper mergeRemoteEntityWithLocalEntity(SessionEntityWrapper localEntityWrapper) {
- int timestampRemote = getTimestamp();
-
- SessionEntityWrapper entityWrapper;
- if (localEntityWrapper == null) {
- entityWrapper = new SessionEntityWrapper<>(this);
- } else {
- AuthenticatedClientSessionEntity localClientSession = (AuthenticatedClientSessionEntity) localEntityWrapper.getEntity();
-
- // local timestamp should always contain the bigger
- if (timestampRemote < localClientSession.getTimestamp()) {
- setTimestamp(localClientSession.getTimestamp());
- }
-
- entityWrapper = new SessionEntityWrapper<>(localEntityWrapper.getLocalMetadata(), this);
- }
-
- entityWrapper.putLocalMetadataNoteInt(LAST_TIMESTAMP_REMOTE, timestampRemote);
-
- logger.debugf("Updating client session entity %s. timestamp=%d, timestampRemote=%d", getId(), getTimestamp(), timestampRemote);
-
- return entityWrapper;
+ this.userSessionId = userSessionId;
+ this.clientId = clientId;
}
+ @ProtoField(8)
public String getUserSessionId() {
return userSessionId;
}
@@ -201,8 +172,8 @@ public class AuthenticatedClientSessionEntity extends SessionEntity {
this.userSessionId = userSessionId;
}
- public static AuthenticatedClientSessionEntity create(UUID clientSessionId, RealmModel realm, ClientModel client, UserSessionModel userSession) {
- var entity = new AuthenticatedClientSessionEntity(clientSessionId);
+ public static AuthenticatedClientSessionEntity create(RealmModel realm, ClientModel client, UserSessionModel userSession) {
+ var entity = new AuthenticatedClientSessionEntity();
entity.setRealmId(realm.getId());
entity.setClientId(client.getId());
entity.setTimestamp(Time.currentTime());
@@ -215,7 +186,7 @@ public class AuthenticatedClientSessionEntity extends SessionEntity {
}
public static AuthenticatedClientSessionEntity createFromModel(AuthenticatedClientSessionModel model) {
- var entity = create(UUID.fromString(model.getId()), model.getRealm(), model.getClient(), model.getUserSession());
+ var entity = create(model.getRealm(), model.getClient(), model.getUserSession());
entity.setNotes(model.getNotes() == null ? new ConcurrentHashMap<>() : model.getNotes());
return entity;
}
diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/EmbeddedClientSessionKey.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/EmbeddedClientSessionKey.java
new file mode 100644
index 00000000000..2a2dd499f1a
--- /dev/null
+++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/EmbeddedClientSessionKey.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2024 Red Hat, Inc. and/or its affiliates
+ * and other contributors as indicated by the @author tags.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.keycloak.models.sessions.infinispan.entities;
+
+import org.infinispan.protostream.annotations.Proto;
+import org.infinispan.protostream.annotations.ProtoTypeId;
+import org.keycloak.marshalling.Marshalling;
+
+/**
+ * The key stored in the {@link org.infinispan.Cache} for {@link AuthenticatedClientSessionEntity}.
+ *
+ * Although this class is the same as {@link ClientSessionKey}, we keep them separates so they can evolve independent.
+ */
+@ProtoTypeId(Marshalling.EMBEDDED_CLIENT_SESSION_KEY)
+@Proto
+public record EmbeddedClientSessionKey(String userSessionId, String clientId) {
+
+ public String toId() {
+ return userSessionId + "::" + clientId;
+ }
+
+}
diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/SessionEntity.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/SessionEntity.java
index d2704afb5bc..c56d0c4d215 100755
--- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/SessionEntity.java
+++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/SessionEntity.java
@@ -55,6 +55,8 @@ public abstract class SessionEntity {
this.realmId = realmId;
}
+ @Deprecated(since = "26.4", forRemoval = true)
+ //no longer used
public SessionEntityWrapper mergeRemoteEntityWithLocalEntity(SessionEntityWrapper localEntityWrapper) {
if (localEntityWrapper == null) {
return new SessionEntityWrapper<>(this);
diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/UserSessionEntity.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/UserSessionEntity.java
index 8254081e6ff..28410f96a86 100755
--- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/UserSessionEntity.java
+++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/UserSessionEntity.java
@@ -17,13 +17,15 @@
package org.keycloak.models.sessions.infinispan.entities;
+import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
-import java.util.TreeSet;
+import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;
+import org.infinispan.protostream.annotations.ProtoReserved;
import org.infinispan.protostream.annotations.ProtoTypeId;
import org.jboss.logging.Logger;
import org.keycloak.common.util.Time;
@@ -38,6 +40,10 @@ import org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper;
* @author Stian Thorgersen
*/
@ProtoTypeId(Marshalling.USER_SESSION_ENTITY)
+@ProtoReserved(
+ value = {11},
+ names = {"authenticatedClientSessions"}
+)
public class UserSessionEntity extends SessionEntity {
public static final Logger logger = Logger.getLogger(UserSessionEntity.class);
@@ -66,12 +72,16 @@ public class UserSessionEntity extends SessionEntity {
private UserSessionModel.State state;
+ private final Set clientSessions = ConcurrentHashMap.newKeySet();
+
+ private Map notes = new ConcurrentHashMap<>();
+
public UserSessionEntity(String id) {
this.id = id;
}
@ProtoFactory
- static UserSessionEntity protoFactory(String realmId, String id, String user, String loginUsername, String ipAddress, String authMethod, boolean rememberMe, int started, int lastSessionRefresh, Map notes, AuthenticatedClientSessionStore authenticatedClientSessions, UserSessionModel.State state, String brokerSessionId, String brokerUserId) {
+ static UserSessionEntity protoFactory(String realmId, String id, String user, String loginUsername, String ipAddress, String authMethod, boolean rememberMe, int started, int lastSessionRefresh, Map notes, UserSessionModel.State state, String brokerSessionId, String brokerUserId, Set clientSessions) {
var entity = new UserSessionEntity(id);
entity.setRealmId(realmId);
entity.setUser(user);
@@ -85,7 +95,7 @@ public class UserSessionEntity extends SessionEntity {
entity.setBrokerUserId(brokerUserId);
entity.setState(state);
entity.setNotes(notes);
- entity.setAuthenticatedClientSessions(authenticatedClientSessions);
+ entity.getClientSessions().addAll(clientSessions);
return entity;
}
@@ -94,10 +104,6 @@ public class UserSessionEntity extends SessionEntity {
return id;
}
- private Map notes = new ConcurrentHashMap<>();
-
- private AuthenticatedClientSessionStore authenticatedClientSessions = new AuthenticatedClientSessionStore();
-
@ProtoField(3)
public String getUser() {
return user;
@@ -170,15 +176,6 @@ public class UserSessionEntity extends SessionEntity {
this.notes = notes;
}
- @ProtoField(11)
- public AuthenticatedClientSessionStore getAuthenticatedClientSessions() {
- return authenticatedClientSessions;
- }
-
- public void setAuthenticatedClientSessions(AuthenticatedClientSessionStore authenticatedClientSessions) {
- this.authenticatedClientSessions = authenticatedClientSessions;
- }
-
@ProtoField(value = 12)
public UserSessionModel.State getState() {
return state;
@@ -206,6 +203,11 @@ public class UserSessionEntity extends SessionEntity {
this.brokerUserId = brokerUserId;
}
+ @ProtoField(value = 15, collectionImplementation = HashSet.class)
+ public Set getClientSessions() {
+ return clientSessions;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
@@ -224,7 +226,7 @@ public class UserSessionEntity extends SessionEntity {
@Override
public String toString() {
return String.format("UserSessionEntity [id=%s, realm=%s, lastSessionRefresh=%d, clients=%s]", getId(), getRealmId(), getLastSessionRefresh(),
- new TreeSet(this.authenticatedClientSessions.keySet()));
+ clientSessions);
}
@Override
@@ -283,7 +285,6 @@ public class UserSessionEntity extends SessionEntity {
entity.setBrokerUserId(userSession.getBrokerUserId());
entity.setIpAddress(userSession.getIpAddress());
entity.setNotes(userSession.getNotes() == null ? new ConcurrentHashMap<>() : userSession.getNotes());
- entity.setAuthenticatedClientSessions(new AuthenticatedClientSessionStore());
entity.setRememberMe(userSession.isRememberMe());
entity.setState(userSession.getState());
if (userSession instanceof OfflineUserSessionModel offline) {
diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/remote/RemoteUserSessionProvider.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/remote/RemoteUserSessionProvider.java
index 30ac07cf77e..2b49c62ecfd 100644
--- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/remote/RemoteUserSessionProvider.java
+++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/remote/RemoteUserSessionProvider.java
@@ -95,10 +95,7 @@ public class RemoteUserSessionProvider implements UserSessionProvider {
}
@Override
- public AuthenticatedClientSessionModel getClientSession(UserSessionModel userSession, ClientModel client, String clientSessionId, boolean offline) {
- if (clientSessionId == null) {
- return null;
- }
+ public AuthenticatedClientSessionModel getClientSession(UserSessionModel userSession, ClientModel client, boolean offline) {
var clientTx = getClientSessionTransaction(offline);
var updater = clientTx.get(new ClientSessionKey(userSession.getId(), client.getId()));
if (updater == null) {
@@ -317,7 +314,7 @@ public class RemoteUserSessionProvider implements UserSessionProvider {
userSessionBuffer.add(userSessionModel.getId());
for (var clientSessionModel : userSessionModel.getAuthenticatedClientSessions().values()) {
var clientSessionKey = new ClientSessionKey(userSessionModel.getId(), clientSessionModel.getClient().getId());
- clientSessionBuffer.add(Map.entry(userSessionModel.getId(), clientSessionModel.getId()));
+ clientSessionBuffer.add(Map.entry(clientSessionKey.userSessionId(), clientSessionKey.clientId()));
var clientSessionEntity = RemoteAuthenticatedClientSessionEntity.createFromModel(clientSessionKey, clientSessionModel);
stage.dependsOn(clientSessionCache.putIfAbsentAsync(clientSessionKey, clientSessionEntity));
}
diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/stream/AuthClientSessionSetMapper.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/stream/AuthClientSessionSetMapper.java
index ce72d9bf245..93c1930fc26 100644
--- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/stream/AuthClientSessionSetMapper.java
+++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/stream/AuthClientSessionSetMapper.java
@@ -49,6 +49,6 @@ public class AuthClientSessionSetMapper implements Function apply(Map.Entry> entry) {
- return entry.getValue().getEntity().getAuthenticatedClientSessions().keySet();
+ return entry.getValue().getEntity().getClientSessions();
}
}
diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/stream/UserSessionPredicate.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/stream/UserSessionPredicate.java
index 96bae2e23da..feea6e858bb 100644
--- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/stream/UserSessionPredicate.java
+++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/stream/UserSessionPredicate.java
@@ -130,7 +130,7 @@ public class UserSessionPredicate implements Predicate persistentUserSessions, boolean offline) {}
void close();
diff --git a/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java b/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java
index d05241d52f7..a5e412d7882 100755
--- a/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java
+++ b/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java
@@ -25,7 +25,6 @@ import org.keycloak.cookie.CookieType;
import org.keycloak.http.HttpRequest;
import org.keycloak.OAuth2Constants;
import org.keycloak.TokenVerifier;
-import org.keycloak.TokenVerifier.Predicate;
import org.keycloak.TokenVerifier.TokenTypeCheck;
import org.keycloak.authentication.AuthenticationFlowException;
import org.keycloak.authentication.AuthenticationProcessor;
@@ -109,7 +108,6 @@ import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@@ -271,7 +269,7 @@ public class AuthenticationManager {
ClientConnection connection, HttpHeaders headers,
boolean logoutBroker) {
- return backchannelLogout(session, realm, userSession, uriInfo, connection, headers, logoutBroker, userSession == null ? false : userSession.isOffline());
+ return backchannelLogout(session, realm, userSession, uriInfo, connection, headers, logoutBroker, userSession != null && userSession.isOffline());
}
/**
@@ -1161,7 +1159,7 @@ public class AuthenticationManager {
getClientScopesToApproveOnConsentScreen(grantedConsent, session, authSession);
// Skip grant screen if everything was already approved by this user
- if (clientScopesToApprove.size() > 0) {
+ if (!clientScopesToApprove.isEmpty()) {
String execution = AuthenticatedClientSessionModel.Action.OAUTH_GRANT.name();
ClientSessionCode accessCode =
@@ -1727,7 +1725,7 @@ public class AuthenticationManager {
Map clientSessions = userSession.getAuthenticatedClientSessions();
return clientSessions.values().stream().filter(c -> c.getClient().equals(client))
- .map((c) -> c.getNotes().get(OIDCLoginProtocol.SCOPE_PARAM))
+ .map((c) -> c.getNote(OIDCLoginProtocol.SCOPE_PARAM))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
diff --git a/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionPersisterProviderTest.java b/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionPersisterProviderTest.java
index 78174fc4d5e..96544e7bffc 100644
--- a/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionPersisterProviderTest.java
+++ b/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionPersisterProviderTest.java
@@ -25,10 +25,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
-import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.hamcrest.Matchers;
@@ -58,6 +56,7 @@ import org.keycloak.models.session.UserSessionPersisterProvider;
import org.keycloak.models.sessions.infinispan.PersistentUserSessionProvider;
import org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper;
import org.keycloak.models.sessions.infinispan.entities.AuthenticatedClientSessionEntity;
+import org.keycloak.models.sessions.infinispan.entities.EmbeddedClientSessionKey;
import org.keycloak.models.utils.ResetTimeOffsetEvent;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.protocol.oidc.OIDCLoginProtocolFactory;
@@ -159,7 +158,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
ClientModel testApp = realm.getClientByClientId("test-app");
- session.sessions().getUserSessionsStream(realm, testApp).collect(Collectors.toList())
+ session.sessions().getUserSessionsStream(realm, testApp).toList()
.forEach(userSessionLooper -> persistUserSession(session, userSessionLooper, true));
});
@@ -195,10 +194,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
int started = Time.currentTime();
AtomicReference origSessionsAt = new AtomicReference<>();
- AtomicReference> loadedSessionsAt = new AtomicReference<>();
-
AtomicReference userSessionAt = new AtomicReference<>();
- AtomicReference persistedSessionAt = new AtomicReference<>();
inComittedTransaction(session -> {
// Create some sessions in infinispan
@@ -225,10 +221,8 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
// Load offline session
List loadedSessions = loadPersistedSessionsPaginated(session, true, 10, 1, 1);
- loadedSessionsAt.set(loadedSessions);
UserSessionModel persistedSession = loadedSessions.get(0);
- persistedSessionAt.set(persistedSession);
assertSession(persistedSession, session.users().getUserByUsername(realm, "user1"), "127.0.0.2", started, started, "test-app");
@@ -391,7 +385,6 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
final String username = "my-user";
final String clientId = "my-app";
final AtomicReference userSessionID = new AtomicReference<>();
- final AtomicReference clientSessionId = new AtomicReference<>();
// create user and client
inComittedTransaction(session -> {
@@ -405,8 +398,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
UserSessionModel userSession = session.sessions().createUserSession(null, realm, session.users().getUserByUsername(realm, username), username, "127.0.0.1", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
userSessionID.set(userSession.getId());
- AuthenticatedClientSessionModel clientSession = createClientSession(session, realm.getId(), realm.getClientByClientId(clientId), userSession, "http://redirect", "state");
- clientSessionId.set(clientSession.getId());
+ createClientSession(session, realm.getId(), realm.getClientByClientId(clientId), userSession, "http://redirect", "state");
});
if (InfinispanUtils.isEmbeddedInfinispan()) {
@@ -415,8 +407,9 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
RealmModel realm = session.realms().getRealmByName(realmName);
session.getContext().setRealm(realm);
- Cache> clientSessoinCache = session.getProvider(InfinispanConnectionProvider.class).getCache(CLIENT_SESSION_CACHE_NAME);
- SessionEntityWrapper clientSession = clientSessoinCache.get(UUID.fromString(clientSessionId.get()));
+ var cacheKey = new EmbeddedClientSessionKey(userSessionID.get(), realm.getClientByClientId(clientId).getId());
+ Cache> clientSessoinCache = session.getProvider(InfinispanConnectionProvider.class).getCache(CLIENT_SESSION_CACHE_NAME);
+ SessionEntityWrapper clientSession = clientSessoinCache.get(cacheKey);
assertNotNull(clientSession);
assertNotNull(clientSession.getEntity());
// user session id is not stored in the cache
@@ -440,7 +433,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
.getTimestamp();
}
return session.sessions()
- .getClientSession(userSession, client, clientSessionId.get(), false)
+ .getClientSession(userSession, client, false)
.getTimestamp();
};
@@ -455,7 +448,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
ClientModel client = realm.getClientByClientId(clientId);
UserSessionModel userSession = session.sessions().getUserSession(realm, userSessionID.get());
session.sessions()
- .getClientSession(userSession, client, clientSessionId.get(), false)
+ .getClientSession(userSession, client, false)
.setTimestamp(currentTimestamp + 10);
});
@@ -723,6 +716,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
}
@Test
+ @Deprecated(since = "26.4", forRemoval = true)
public void testMigrateSession() {
Assume.assumeTrue(MultiSiteUtils.isPersistentSessionsEnabled());
Assume.assumeTrue(InfinispanUtils.isEmbeddedInfinispan());
@@ -745,7 +739,6 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
// trigger a migration with the entries that are still in the cache
PersistentUserSessionProvider userSessionProvider = (PersistentUserSessionProvider) session.getProvider(UserSessionProvider.class);
userSessionProvider.migrateNonPersistentSessionsToPersistentSessions();
- JpaUserSessionPersisterProvider sessionPersisterProvider = (JpaUserSessionPersisterProvider) session.getProvider(UserSessionPersisterProvider.class);
// verify that import was complete
Assert.assertEquals(sessions.length, countUserSessionsInRealm(session));
@@ -830,8 +823,6 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
private List loadPersistedSessionsPaginated(KeycloakSession session, boolean offline, int sessionsPerPage, int expectedPageCount, int expectedSessionsCount) {
UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
- int count = persister.getUserSessionsCount(offline);
-
int pageCount = 0;
boolean next = true;
List result = new ArrayList<>();
@@ -840,13 +831,13 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
while (next) {
List sess = persister
.loadUserSessionsStream(0, sessionsPerPage, offline, lastSessionId)
- .collect(Collectors.toList());
+ .toList();
if (sess.size() < sessionsPerPage) {
next = false;
// We had at least some session
- if (sess.size() > 0) {
+ if (!sess.isEmpty()) {
pageCount++;
}
} else {
diff --git a/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionProviderModelTest.java b/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionProviderModelTest.java
index bf18a5e7777..ef644c58bf5 100644
--- a/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionProviderModelTest.java
+++ b/testsuite/model/src/test/java/org/keycloak/testsuite/model/session/UserSessionProviderModelTest.java
@@ -17,7 +17,6 @@
package org.keycloak.testsuite.model.session;
import java.util.Collections;
-import java.util.List;
import java.util.Set;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.ConcurrentHashMap;
@@ -25,8 +24,6 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
-import java.util.concurrent.atomic.AtomicReference;
-import java.util.stream.Collectors;
import org.hamcrest.Matchers;
import org.junit.Assert;
@@ -151,9 +148,6 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
return createSessions(session, realmId);
});
- AtomicReference> clientSessionIds = new AtomicReference<>();
- clientSessionIds.set(origSessions[0].getAuthenticatedClientSessions().values().stream().map(AuthenticatedClientSessionModel::getId).collect(Collectors.toList()));
-
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
@@ -162,7 +156,6 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
Assert.assertEquals(origSessions[0], userSession);
AuthenticatedClientSessionModel clientSession = session.sessions().getClientSession(userSession, realm.getClientByClientId("test-app"),
- origSessions[0].getAuthenticatedClientSessionByClient(realm.getClientByClientId("test-app").getId()).getId(),
false);
Assert.assertEquals(origSessions[0].getAuthenticatedClientSessionByClient(realm.getClientByClientId("test-app").getId()).getId(), clientSession.getId());
@@ -182,10 +175,8 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
Assert.assertEquals(origSessions[0], userSession);
// assert the client sessions are expired
- clientSessionIds.get().forEach(clientSessionId -> {
- Assert.assertNull(session.sessions().getClientSession(userSession, realm.getClientByClientId("test-app"), clientSessionId, false));
- Assert.assertNull(session.sessions().getClientSession(userSession, realm.getClientByClientId("third-party"), clientSessionId, false));
- });
+ Assert.assertNull(session.sessions().getClientSession(userSession, realm.getClientByClientId("test-app"), false));
+ Assert.assertNull(session.sessions().getClientSession(userSession, realm.getClientByClientId("third-party"), false));
});
} finally {
setTimeOffset(0);
@@ -207,10 +198,10 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
UserSessionModel userSession = session.sessions().createUserSession(KeycloakModelUtils.generateId(), realm, session.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", false, null, null, UserSessionModel.SessionPersistenceState.TRANSIENT);
ClientModel testApp = realm.getClientByClientId("test-app");
- AuthenticatedClientSessionModel clientSession = session.sessions().createClientSession(realm, testApp, userSession);
+ session.sessions().createClientSession(realm, testApp, userSession);
// assert the client sessions are present
- assertThat(session.sessions().getClientSession(userSession, testApp, clientSession.getId(), false), notNullValue());
+ assertThat(session.sessions().getClientSession(userSession, testApp, false), notNullValue());
return userSession.getId();
});
@@ -225,27 +216,22 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
@Test
public void testClientSessionIsNotPersistedForTransientUserSession() {
- Object[] transientUserSessionWithClientSessionId = inComittedTransaction(session -> {
+ UserSessionModel userSession = inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
- UserSessionModel userSession = session.sessions().createUserSession(null, realm, session.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", false, null, null, UserSessionModel.SessionPersistenceState.TRANSIENT);
+ UserSessionModel us = session.sessions().createUserSession(null, realm, session.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", false, null, null, UserSessionModel.SessionPersistenceState.TRANSIENT);
ClientModel testApp = realm.getClientByClientId("test-app");
- AuthenticatedClientSessionModel clientSession = session.sessions().createClientSession(realm, testApp, userSession);
+ session.sessions().createClientSession(realm, testApp, us);
// assert the client sessions are present
- assertThat(session.sessions().getClientSession(userSession, testApp, clientSession.getId(), false), notNullValue());
- Object[] result = new Object[2];
- result[0] = userSession;
- result[1] = clientSession.getId();
- return result;
+ assertThat(session.sessions().getClientSession(us, testApp, false), notNullValue());
+ return us;
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
ClientModel testApp = realm.getClientByClientId("test-app");
- UserSessionModel userSession = (UserSessionModel) transientUserSessionWithClientSessionId[0];
- String clientSessionId = (String) transientUserSessionWithClientSessionId[1];
// in new transaction transient session should not be present
- assertThat(session.sessions().getClientSession(userSession, testApp, clientSessionId, false), nullValue());
+ assertThat(session.sessions().getClientSession(userSession, testApp, false), nullValue());
});
}
diff --git a/testsuite/utils/src/main/java/org/keycloak/testsuite/util/cli/AbstractSessionCacheCommand.java b/testsuite/utils/src/main/java/org/keycloak/testsuite/util/cli/AbstractSessionCacheCommand.java
index 708d040f2f8..2342d39568b 100644
--- a/testsuite/utils/src/main/java/org/keycloak/testsuite/util/cli/AbstractSessionCacheCommand.java
+++ b/testsuite/utils/src/main/java/org/keycloak/testsuite/util/cli/AbstractSessionCacheCommand.java
@@ -83,7 +83,7 @@ public abstract class AbstractSessionCacheCommand extends AbstractCommand {
}
protected String toString(UserSessionEntity userSession) {
- int clientSessionsSize = userSession.getAuthenticatedClientSessions()==null ? 0 : userSession.getAuthenticatedClientSessions().size();
+ int clientSessionsSize = userSession.getClientSessions().size();
return "ID: " + userSession.getId() + ", realm: " + userSession.getRealmId()+ ", lastAccessTime: " + Time.toDate(userSession.getLastSessionRefresh()) +
", authenticatedClientSessions: " + clientSessionsSize;
}