use of org.keycloak.models.UserSessionModel in project keycloak by keycloak.
the class UserSessionInitializerTest method testUserSessionPropagationBetweenSites.
@Test
@RequireProvider(value = UserSessionProvider.class, only = InfinispanUserSessionProviderFactory.PROVIDER_ID)
public void testUserSessionPropagationBetweenSites() throws InterruptedException {
AtomicInteger index = new AtomicInteger();
AtomicReference<String> userSessionId = new AtomicReference<>();
AtomicReference<List<Boolean>> containsSession = new AtomicReference<>(new LinkedList<>());
Object lock = new Object();
Optional<HotRodServerRule> hotRodServer = getParameters(HotRodServerRule.class).findFirst();
inIndependentFactories(4, 300, () -> {
synchronized (lock) {
if (index.incrementAndGet() == 1) {
// create a user session in the first node
UserSessionModel userSessionModel = withRealm(realmId, (session, realm) -> {
final UserModel user = session.users().getUserByUsername(realm, "user1");
return session.sessions().createUserSession(realm, user, "un1", "ip1", "auth", false, null, null);
});
userSessionId.set(userSessionModel.getId());
} else {
// try to get the user session at other nodes and also at different sites
inComittedTransaction(session -> {
InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class);
Cache<String, Object> localSessions = provider.getCache(USER_SESSION_CACHE_NAME);
containsSession.get().add(localSessions.containsKey(userSessionId.get()));
if (hotRodServer.isPresent()) {
RemoteCache<String, Object> remoteSessions = provider.getRemoteCache(USER_SESSION_CACHE_NAME);
containsSession.get().add(remoteSessions.containsKey(userSessionId.get()));
}
});
}
}
});
assertThat(containsSession.get(), everyItem(is(true)));
// 3 nodes (first node just creates the session), with Hot Rod server we have local + remote cache, without just local cache
int size = hotRodServer.isPresent() ? 6 : 3;
assertThat(containsSession.get().size(), is(size));
}
use of org.keycloak.models.UserSessionModel in project keycloak by keycloak.
the class UserSessionInitializerTest method testUserSessionInitializerWithDeletingClient.
@Test
public void testUserSessionInitializerWithDeletingClient() {
UserSessionModel[] origSessionIds = createSessionsInPersisterOnly();
int started = origSessionIds[0].getStarted();
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
// Delete one of the clients now
ClientModel testApp = realm.getClientByClientId("test-app");
realm.removeClient(testApp.getId());
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
// Assert sessions are in
ClientModel thirdparty = realm.getClientByClientId("third-party");
assertThat("Count of offline sesions for client 'third-party'", session.sessions().getOfflineSessionsCount(realm, thirdparty), is((long) 1));
List<UserSessionModel> loadedSessions = session.sessions().getOfflineUserSessionsStream(realm, thirdparty, 0, 10).collect(Collectors.toList());
assertThat("Size of loaded Sessions", loadedSessions.size(), is(1));
assertSessionLoaded(loadedSessions, origSessionIds[0].getId(), session.users().getUserByUsername(realm, "user1"), "127.0.0.1", started, started, "third-party");
// Revert client
realm.addClient("test-app");
});
}
use of org.keycloak.models.UserSessionModel in project keycloak by keycloak.
the class UserSessionProviderModelTest method testMultipleSessionsRemovalInOneTransaction.
@Test
public void testMultipleSessionsRemovalInOneTransaction() {
UserSessionModel[] origSessions = inComittedTransaction(session -> {
return createSessions(session, realmId);
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
UserSessionModel userSession = session.sessions().getUserSession(realm, origSessions[0].getId());
Assert.assertEquals(origSessions[0], userSession);
userSession = session.sessions().getUserSession(realm, origSessions[1].getId());
Assert.assertEquals(origSessions[1], userSession);
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.sessions().removeUserSession(realm, origSessions[0]);
session.sessions().removeUserSession(realm, origSessions[1]);
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
UserSessionModel userSession = session.sessions().getUserSession(realm, origSessions[0].getId());
Assert.assertNull(userSession);
userSession = session.sessions().getUserSession(realm, origSessions[1].getId());
Assert.assertNull(userSession);
});
}
use of org.keycloak.models.UserSessionModel in project keycloak by keycloak.
the class UserSessionPersisterProviderTest method testMoreSessions.
@Test
public void testMoreSessions() {
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
// Create 10 userSessions - each having 1 clientSession
List<String> userSessionsInner = new LinkedList<>();
UserModel user = session.users().getUserByUsername(realm, "user1");
for (int i = 0; i < USER_SESSION_COUNT; i++) {
// Having different offsets for each session (to ensure that lastSessionRefresh is also different)
Time.setOffset(i);
UserSessionModel userSession = session.sessions().createUserSession(realm, user, "user1", "127.0.0.1", "form", true, null, null);
createClientSession(session, realmId, realm.getClientByClientId("test-app"), userSession, "http://redirect", "state");
userSessionsInner.add(userSession.getId());
}
for (String userSessionId : userSessionsInner) {
UserSessionModel userSession2 = session.sessions().getUserSession(realm, userSessionId);
persistUserSession(session, userSession2, true);
}
return null;
});
withRealm(realmId, (session, realm) -> {
final int sessionsPerPage = 3;
List<UserSessionModel> loadedSessions = loadPersistedSessionsPaginated(session, true, sessionsPerPage, USER_SESSION_COUNT / sessionsPerPage + (USER_SESSION_COUNT / sessionsPerPage == 0 ? 0 : 1), USER_SESSION_COUNT);
UserModel user = session.users().getUserByUsername(realm, "user1");
ClientModel testApp = realm.getClientByClientId("test-app");
for (UserSessionModel loadedSession : loadedSessions) {
assertEquals(user.getId(), loadedSession.getUser().getId());
assertEquals("127.0.0.1", loadedSession.getIpAddress());
assertEquals(user.getUsername(), loadedSession.getLoginUsername());
assertEquals(1, loadedSession.getAuthenticatedClientSessions().size());
assertTrue(loadedSession.getAuthenticatedClientSessions().containsKey(testApp.getId()));
}
return null;
});
}
use of org.keycloak.models.UserSessionModel in project keycloak by keycloak.
the class UserSessionPersisterProviderTest method testUpdateAndRemove.
@Test
public void testUpdateAndRemove() {
int started = Time.currentTime();
AtomicReference<UserSessionModel[]> origSessionsAt = new AtomicReference<>();
AtomicReference<List<UserSessionModel>> loadedSessionsAt = new AtomicReference<>();
AtomicReference<UserSessionModel> userSessionAt = new AtomicReference<>();
AtomicReference<UserSessionModel> persistedSessionAt = new AtomicReference<>();
inComittedTransaction(session -> {
// Create some sessions in infinispan
UserSessionModel[] origSessions = createSessions(session, realmId);
origSessionsAt.set(origSessions);
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
UserSessionModel[] origSessions = origSessionsAt.get();
// Persist 1 offline session
UserSessionModel userSession = session.sessions().getUserSession(realm, origSessions[1].getId());
userSessionAt.set(userSession);
persistUserSession(session, userSession, true);
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
// Load offline session
List<UserSessionModel> 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");
// create new clientSession
AuthenticatedClientSessionModel clientSession = createClientSession(session, realmId, realm.getClientByClientId("third-party"), session.sessions().getUserSession(realm, persistedSession.getId()), "http://redirect", "state");
persister.createClientSession(clientSession, true);
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
UserSessionModel userSession = userSessionAt.get();
// Remove clientSession
persister.removeClientSession(userSession.getId(), realm.getClientByClientId("third-party").getId(), true);
});
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
// Assert clientSession removed
List<UserSessionModel> loadedSessions = loadPersistedSessionsPaginated(session, true, 10, 1, 1);
UserSessionModel persistedSession = loadedSessions.get(0);
assertSession(persistedSession, session.users().getUserByUsername(realm, "user1"), "127.0.0.2", started, started, "test-app");
// Remove userSession
persister.removeUserSession(persistedSession.getId(), true);
});
inComittedTransaction(session -> {
// Assert nothing found
loadPersistedSessionsPaginated(session, true, 10, 0, 0);
});
}
Aggregations