Handle already existing user session in the store

Closes #40374

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz
2025-09-11 17:10:06 +02:00
committed by GitHub
parent d531377dd8
commit 63a5aee682
3 changed files with 656 additions and 605 deletions

View File

@@ -45,7 +45,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -55,10 +54,6 @@ import static org.junit.Assert.assertTrue;
*/
public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTest {
private static KeycloakSession currentSession;
private static RealmModel realm;
private static UserSessionManager sessionManager;
@Before
public void before() {
testingClient.server("test").run(session ->
@@ -89,28 +84,25 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
public void testOfflineSessionsCrud(KeycloakSession session) {
Map<String, Set<String>> offlineSessions = new HashMap<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), currentSession -> {
// Create some online sessions in infinispan
reloadState(sessionCrud);
createSessions(sessionCrud);
reloadState(currentSession);
createSessions(currentSession);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud2) -> {
currentSession = sessionCrud2;
realm = currentSession.realms().getRealmByName("test");
sessionManager = new UserSessionManager(currentSession);
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), currentSession -> {
RealmModel realm = currentSession.realms().getRealmByName("test");
// Key is userSession ID, values are client UUIDS
// Persist 3 created userSessions and clientSessions as offline
ClientModel testApp = realm.getClientByClientId("test-app");
currentSession.sessions().getUserSessionsStream(realm, testApp).collect(Collectors.toList())
currentSession.sessions().getUserSessionsStream(realm, testApp).toList()
.forEach(userSession -> offlineSessions.put(userSession.getId(), createOfflineSessionIncludeClientSessions(currentSession, userSession)));
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud3) -> {
currentSession = sessionCrud3;
realm = currentSession.realms().getRealmByName("test");
sessionManager = new UserSessionManager(currentSession);
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), currentSession -> {
RealmModel realm = currentSession.realms().getRealmByName("test");
UserSessionManager sessionManager = new UserSessionManager(currentSession);
// Assert all previously saved offline sessions found
for (Map.Entry<String, Set<String>> entry : offlineSessions.entrySet()) {
@@ -123,7 +115,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
UserModel user1 = currentSession.users().getUserByUsername(realm, "user1");
Set<ClientModel> clients = sessionManager.findClientsWithOfflineToken(realm, user1);
Assert.assertEquals(clients.size(), 2);
Assert.assertEquals(2, clients.size());
for (ClientModel client : clients) {
Assert.assertTrue(client.getClientId().equals("test-app") || client.getClientId().equals("third-party"));
}
@@ -131,7 +123,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
UserModel user2 = currentSession.users().getUserByUsername(realm, "user2");
clients = sessionManager.findClientsWithOfflineToken(realm, user2);
Assert.assertEquals(clients.size(), 1);
Assert.assertEquals(1, clients.size());
Assert.assertEquals("test-app", clients.iterator().next().getClientId());
// Test count
@@ -143,16 +135,15 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
sessionManager.revokeOfflineToken(user1, testApp);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud4) -> {
currentSession = sessionCrud4;
realm = currentSession.realms().getRealmByName("test");
sessionManager = new UserSessionManager(currentSession);
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), currentSession -> {
RealmModel realm = currentSession.realms().getRealmByName("test");
UserSessionManager sessionManager = new UserSessionManager(currentSession);
// Assert userSession revoked
ClientModel thirdparty = realm.getClientByClientId("third-party");
List<UserSessionModel> thirdpartySessions = currentSession.sessions().getOfflineUserSessionsStream(realm, thirdparty, 0, 10)
.collect(Collectors.toList());
.toList();
Assert.assertEquals(1, thirdpartySessions.size());
Assert.assertEquals("127.0.0.1", thirdpartySessions.get(0).getIpAddress());
Assert.assertEquals("user1", thirdpartySessions.get(0).getUser().getUsername());
@@ -172,10 +163,9 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud5) -> {
currentSession = sessionCrud5;
realm = currentSession.realms().getRealmByName("test");
sessionManager = new UserSessionManager(currentSession);
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), currentSession -> {
RealmModel realm = currentSession.realms().getRealmByName("test");
UserSessionManager sessionManager = new UserSessionManager(currentSession);
ClientModel testApp = realm.getClientByClientId("test-app");
ClientModel thirdparty = realm.getClientByClientId("third-party");
@@ -185,7 +175,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
Assert.assertEquals(0, currentSession.sessions().getOfflineSessionsCount(realm, thirdparty));
List<UserSessionModel> testAppSessions = currentSession.sessions().getOfflineUserSessionsStream(realm, testApp, 0, 10)
.collect(Collectors.toList());
.toList();
Assert.assertEquals(1, testAppSessions.size());
Assert.assertEquals("127.0.0.3", testAppSessions.get(0).getIpAddress());
@@ -203,8 +193,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
public void testOnRealmRemoved(KeycloakSession session) {
AtomicReference<String> userSessionID = new AtomicReference<>();
String realmId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), (KeycloakSession sessionRR1) -> {
currentSession = sessionRR1;
String realmId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), currentSession -> {
RealmModel fooRealm = currentSession.realms().createRealm("foo");
currentSession.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
@@ -223,9 +212,8 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
return fooRealm.getId();
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRR2) -> {
currentSession = sessionRR2;
sessionManager = new UserSessionManager(currentSession);
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
UserSessionManager sessionManager = new UserSessionManager(currentSession);
// Persist offline session
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
@@ -234,18 +222,20 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
createOfflineSessionIncludeClientSessions(currentSession, userSession);
UserSessionModel offlineUserSession = sessionManager.findOfflineUserSession(fooRealm, userSession.getId());
Assert.assertEquals(offlineUserSession.getAuthenticatedClientSessions().size(), 1);
Assert.assertEquals(1, offlineUserSession.getAuthenticatedClientSessions().size());
AuthenticatedClientSessionModel offlineClientSession = offlineUserSession.getAuthenticatedClientSessions().values().iterator().next();
Assert.assertEquals("foo-app", offlineClientSession.getClient().getClientId());
Assert.assertEquals("user3", offlineClientSession.getUserSession().getUser().getUsername());
});
// Remove realm
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
RealmManager realmMgr = new RealmManager(currentSession);
realmMgr.removeRealm(realmMgr.getRealm(realmId));
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRR3) -> {
currentSession = sessionRR3;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
RealmModel fooRealm = currentSession.realms().createRealm(realmId, "foo");
currentSession.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
@@ -254,8 +244,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
currentSession.users().addUser(fooRealm, "user3");
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRR4) -> {
currentSession = sessionRR4;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
Assert.assertEquals(0, currentSession.sessions().getOfflineSessionsCount(fooRealm, fooRealm.getClientByClientId("foo-app")));
@@ -270,9 +259,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
@ModelTest
public void testOnClientRemoved(KeycloakSession session) {
AtomicReference<String> userSessionID = new AtomicReference<>();
String realmId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), (KeycloakSession sessionCR1) -> {
currentSession = sessionCR1;
sessionManager = new UserSessionManager(currentSession);
String realmId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), currentSession -> {
RealmModel fooRealm = currentSession.realms().createRealm("foo");
currentSession.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
@@ -297,8 +284,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
try {
int started = Time.currentTime();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCR2) -> {
currentSession = sessionCR2;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
// Create offline currentSession
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
@@ -306,8 +292,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
createOfflineSessionIncludeClientSessions(currentSession, userSession);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCR3) -> {
currentSession = sessionCR3;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
RealmManager realmMgr = new RealmManager(currentSession);
ClientManager clientMgr = new ClientManager(realmMgr);
RealmModel fooRealm = realmMgr.getRealm(realmId);
@@ -315,15 +300,14 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
// Assert currentSession was persisted with both clientSessions
UserSessionModel offlineSession = currentSession.sessions().getOfflineUserSession(fooRealm, userSessionID.get());
assertSession(offlineSession, currentSession.users().getUserByUsername(fooRealm, "user3"), "127.0.0.1", started, started, "foo-app", "bar-app");
assertSession(offlineSession, currentSession.users().getUserByUsername(fooRealm, "user3"), "127.0.0.1", started, started);
// Remove foo-app client
ClientModel client = fooRealm.getClientByClientId("foo-app");
clientMgr.removeClient(fooRealm, client);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCR4) -> {
currentSession = sessionCR4;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
RealmManager realmMgr = new RealmManager(currentSession);
ClientManager clientMgr = new ClientManager(realmMgr);
RealmModel fooRealm = realmMgr.getRealm(realmId);
@@ -339,8 +323,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
clientMgr.removeClient(fooRealm, client);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCR5) -> {
currentSession = sessionCR5;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
// Assert nothing loaded - userSession was removed as well because it was last userSession
RealmManager realmMgr = new RealmManager(currentSession);
RealmModel fooRealm = realmMgr.getRealm(realmId);
@@ -352,8 +335,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionTearDown) -> {
currentSession = sessionTearDown;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
RealmManager realmMgr = new RealmManager(currentSession);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
@@ -373,8 +355,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
@ModelTest
public void testOnUserRemoved(KeycloakSession session) {
AtomicReference<String> userSessionID = new AtomicReference<>();
String realmId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), (KeycloakSession sessionUR1) -> {
currentSession = sessionUR1;
String realmId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), currentSession -> {
RealmModel fooRealm = currentSession.realms().createRealm("foo");
currentSession.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
@@ -396,9 +377,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
int started = Time.currentTime();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUR2) -> {
currentSession = sessionUR2;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
// Create offline session
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
@@ -406,9 +385,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
createOfflineSessionIncludeClientSessions(currentSession, userSession);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUR3) -> {
currentSession = sessionUR3;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
RealmManager realmMgr = new RealmManager(currentSession);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
@@ -416,15 +393,13 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
// Assert session was persisted with both clientSessions
UserSessionModel offlineSession = currentSession.sessions().getOfflineUserSession(fooRealm, userSessionID.get());
assertSession(offlineSession, user3, "127.0.0.1", started, started, "foo-app");
assertSession(offlineSession, user3, "127.0.0.1", started, started);
});
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionTearDown) -> {
currentSession = sessionTearDown;
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), currentSession -> {
RealmManager realmMgr = new RealmManager(currentSession);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
@@ -453,7 +428,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
}
public static void assertSession(UserSessionModel session, UserModel user, String ipAddress, int started,
int lastRefresh, String... clients) {
int lastRefresh) {
assertEquals(user.getId(), session.getUser().getId());
assertEquals(ipAddress, session.getIpAddress());
assertEquals(user.getUsername(), session.getLoginUsername());
@@ -462,14 +437,10 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
assertTrue((session.getStarted() >= started - 1) && (session.getStarted() <= started + 1));
assertTrue((session.getLastSessionRefresh() >= lastRefresh - 1) && (session.getLastSessionRefresh() <= lastRefresh + 1));
String[] actualClients = new String[session.getAuthenticatedClientSessions().size()];
int i = 0;
for (Map.Entry<String, AuthenticatedClientSessionModel> entry : session.getAuthenticatedClientSessions().entrySet()) {
String clientUUID = entry.getKey();
AuthenticatedClientSessionModel clientSession = entry.getValue();
Assert.assertEquals(clientUUID, clientSession.getClient().getId());
actualClients[i] = clientSession.getClient().getClientId();
i++;
}
}
@@ -481,26 +452,19 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
return clientSession;
}
private static UserSessionModel[] createSessions(KeycloakSession session) {
private static UserSessionModel[] createSessions(KeycloakSession currentSession) {
UserSessionModel[] sessions = new UserSessionModel[3];
sessions[0] = session.sessions().createUserSession(null, realm, currentSession.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
RealmModel realm = currentSession.getContext().getRealm();
sessions[0] = currentSession.sessions().createUserSession(null, realm, currentSession.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
Set<String> roles = new HashSet<String>();
roles.add("one");
roles.add("two");
createClientSession(currentSession, realm.getClientByClientId("test-app"), sessions[0], "http://redirect", "state");
createClientSession(currentSession, realm.getClientByClientId("third-party"), sessions[0], "http://redirect", "state");
Set<String> protocolMappers = new HashSet<String>();
protocolMappers.add("mapper-one");
protocolMappers.add("mapper-two");
sessions[1] = currentSession.sessions().createUserSession(null, realm, currentSession.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.2", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
createClientSession(currentSession, realm.getClientByClientId("test-app"), sessions[1], "http://redirect", "state");
createClientSession(session, realm.getClientByClientId("test-app"), sessions[0], "http://redirect", "state");
createClientSession(session, realm.getClientByClientId("third-party"), sessions[0], "http://redirect", "state");
sessions[1] = session.sessions().createUserSession(null, realm, session.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.2", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
createClientSession(session, realm.getClientByClientId("test-app"), sessions[1], "http://redirect", "state");
sessions[2] = session.sessions().createUserSession(null, realm, session.users().getUserByUsername(realm, "user2"), "user2", "127.0.0.3", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
createClientSession(session, realm.getClientByClientId("test-app"), sessions[2], "http://redirect", "state");
sessions[2] = currentSession.sessions().createUserSession(null, realm, currentSession.users().getUserByUsername(realm, "user2"), "user2", "127.0.0.3", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
createClientSession(currentSession, realm.getClientByClientId("test-app"), sessions[2], "http://redirect", "state");
return sessions;
}
@@ -509,14 +473,12 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
reloadState(session, false);
}
public static void reloadState(KeycloakSession session, Boolean initialConfig) {
currentSession = session;
realm = currentSession.realms().getRealmByName("test");
public static void reloadState(KeycloakSession currentSession, Boolean initialConfig) {
RealmModel realm = currentSession.realms().getRealmByName("test");
if (initialConfig) {
currentSession.users().addUser(realm, "user1").setEmail("user1@localhost");
currentSession.users().addUser(realm, "user2").setEmail("user2@localhost");
}
sessionManager = new UserSessionManager(currentSession);
}
@Override

View File

@@ -553,6 +553,10 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
setTimeOffset(i);
UserSessionModel userSession = session.sessions().createUserSession(null, realm, user, "user1", "127.0.0.1", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
if (userSessionsInner.contains(userSession.getId())) {
Assert.fail("Duplicate session id generated: " + userSession.getId());
}
createClientSession(session, realmId, realm.getClientByClientId("test-app"), userSession, "http://redirect", "state");
userSessionsInner.add(userSession.getId());
}
@@ -585,6 +589,48 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
}
@Test
public void testConcurrentSessionCreation() {
String userSessionId = withRealm(realmId, (session, realm) -> {
UserModel user = session.users().getUserByUsername(realm, "user1");
UserSessionModel userSession = session.sessions().createUserSession(null, realm, user, "user1", "127.0.0.1", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
userSession.setNote("ITERATION1", "true");
return userSession.getId();
});
// Simulate a concurrently created session
withRealm(realmId, (session, realm) -> {
UserModel user = session.users().getUserByUsername(realm, "user1");
UserSessionModel userSession = session.sessions().createUserSession(userSessionId, realm, user, "user1", "127.0.0.1", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
userSession.setNote("ITERATION2", "true");
return null;
});
withRealm(realmId, (session, realm) -> {
UserSessionModel userSession = session.sessions().getUserSession(realm, userSessionId);
assertThat(userSession.getNote("ITERATION1"), Matchers.equalTo("true"));
assertThat(userSession.getNote("ITERATION2"), Matchers.equalTo("true"));
return null;
});
if (MultiSiteUtils.isPersistentSessionsEnabled()) {
try {
// Simulate a concurrently created session with a different user
withRealm(realmId, (session, realm) -> {
UserModel user = session.users().getUserByUsername(realm, "user2");
UserSessionModel userSession = session.sessions().createUserSession(userSessionId, realm, user, "user1", "127.0.0.1", "form", true, null, null, UserSessionModel.SessionPersistenceState.PERSISTENT);
userSession.setNote("ITERATION2", "true");
return null;
});
Assert.fail("Exception expected");
} catch (RuntimeException e) {
assertThat(e.getMessage(), Matchers.containsString("unable to complete the session updates"));
assertThat(e.getSuppressed()[0].getMessage(), Matchers.containsString("User ID of the session does not match"));
}
}
}
@Test
public void testExpiredSessions() {
int started = Time.currentTime();