use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.
the class InfinispanUserSessionProvider method removeOfflineUserSession.
@Override
public void removeOfflineUserSession(RealmModel realm, UserSessionModel userSession) {
UserSessionEntity userSessionEntity = getUserSessionEntity(realm, userSession, true);
if (userSessionEntity != null) {
removeUserSession(userSessionEntity, true);
}
session.getProvider(UserSessionPersisterProvider.class).removeUserSession(userSession.getId(), true);
}
use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.
the class InfinispanUserSessionProvider method importUserSession.
private UserSessionEntity importUserSession(RealmModel realm, boolean offline, UserSessionModel persistentUserSession) {
String sessionId = persistentUserSession.getId();
log.debugf("Attempting to import user-session for sessionId=%s offline=%s", sessionId, offline);
session.sessions().importUserSessions(Collections.singleton(persistentUserSession), offline);
log.debugf("user-session imported, trying another lookup for sessionId=%s offline=%s", sessionId, offline);
UserSessionEntity ispnUserSessionEntity = getUserSessionEntity(realm, sessionId, offline);
if (ispnUserSessionEntity != null) {
log.debugf("user-session found after import for sessionId=%s offline=%s", sessionId, offline);
return ispnUserSessionEntity;
}
log.debugf("user-session could not be found after import for sessionId=%s offline=%s", sessionId, offline);
return null;
}
use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.
the class InfinispanUserSessionProvider method getUserSessionWithPredicate.
@Override
public UserSessionModel getUserSessionWithPredicate(RealmModel realm, String id, boolean offline, Predicate<UserSessionModel> predicate) {
UserSessionModel userSession = getUserSession(realm, id, offline);
if (userSession == null) {
return null;
}
// We have userSession, which passes predicate. No need for remote lookup.
if (predicate.test(userSession)) {
log.debugf("getUserSessionWithPredicate(%s): found in local cache", id);
return userSession;
}
// Try lookup userSession from remoteCache
Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline);
RemoteCache remoteCache = InfinispanUtil.getRemoteCache(cache);
if (remoteCache != null) {
SessionEntityWrapper<UserSessionEntity> remoteSessionEntityWrapper = (SessionEntityWrapper<UserSessionEntity>) remoteCache.get(id);
if (remoteSessionEntityWrapper != null) {
UserSessionEntity remoteSessionEntity = remoteSessionEntityWrapper.getEntity();
log.debugf("getUserSessionWithPredicate(%s): remote cache contains session entity %s", id, remoteSessionEntity);
UserSessionModel remoteSessionAdapter = wrap(realm, remoteSessionEntity, offline);
if (predicate.test(remoteSessionAdapter)) {
InfinispanChangelogBasedTransaction<String, UserSessionEntity> tx = getTransaction(offline);
// Remote entity contains our predicate. Update local cache with the remote entity
SessionEntityWrapper<UserSessionEntity> sessionWrapper = remoteSessionEntity.mergeRemoteEntityWithLocalEntity(tx.get(id));
// Replace entity just in ispn cache. Skip remoteStore
cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE, Flag.SKIP_CACHE_LOAD, Flag.IGNORE_RETURN_VALUES).replace(id, sessionWrapper);
tx.reloadEntityInCurrentTransaction(realm, id, sessionWrapper);
// Recursion. We should have it locally now
return getUserSessionWithPredicate(realm, id, offline, predicate);
} else {
log.debugf("getUserSessionWithPredicate(%s): found, but predicate doesn't pass", id);
return null;
}
} else {
log.debugf("getUserSessionWithPredicate(%s): not found", id);
// Session not available on remoteCache. Was already removed there. So removing locally too.
// TODO: Can be optimized to skip calling remoteCache.remove
removeUserSession(realm, userSession);
return null;
}
} else {
log.debugf("getUserSessionWithPredicate(%s): remote cache not available", id);
return null;
}
}
use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.
the class ConcurrencyJDGOfflineBackupsTest method main.
public static void main(String[] args) throws Exception {
Cache<String, SessionEntityWrapper<UserSessionEntity>> cache1 = createManager(1).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
try {
// Create initial item
UserSessionEntity session = new UserSessionEntity();
session.setId("123");
session.setRealmId("foo");
session.setBrokerSessionId("!23123123");
session.setBrokerUserId(null);
session.setUser("foo");
session.setLoginUsername("foo");
session.setIpAddress("123.44.143.178");
session.setStarted(Time.currentTime());
session.setLastSessionRefresh(Time.currentTime());
// AuthenticatedClientSessionEntity clientSession = new AuthenticatedClientSessionEntity();
// clientSession.setAuthMethod("saml");
// clientSession.setAction("something");
// clientSession.setTimestamp(1234);
// clientSession.setProtocolMappers(new HashSet<>(Arrays.asList("mapper1", "mapper2")));
// clientSession.setRoles(new HashSet<>(Arrays.asList("role1", "role2")));
// session.getAuthenticatedClientSessions().put(CLIENT_1_UUID.toString(), clientSession.getId());
SessionEntityWrapper<UserSessionEntity> wrappedSession = new SessionEntityWrapper<>(session);
// Some dummy testing of remoteStore behaviour
logger.info("Before put");
AtomicInteger successCount = new AtomicInteger(0);
AtomicInteger errorsCount = new AtomicInteger(0);
for (int i = 0; i < 100; i++) {
try {
cache1.getAdvancedCache().withFlags(// will still invoke remoteStore . Just doesn't propagate to cluster
Flag.CACHE_MODE_LOCAL).put("123", wrappedSession);
successCount.incrementAndGet();
Thread.sleep(1000);
logger.infof("Success in the iteration: %d", i);
} catch (HotRodClientException hrce) {
logger.errorf("Failed to put the item in the iteration: %d ", i);
errorsCount.incrementAndGet();
}
}
logger.infof("SuccessCount: %d, ErrorsCount: %d", successCount.get(), errorsCount.get());
// logger.info("After put");
//
// cache1.replace("123", wrappedSession);
//
// logger.info("After replace");
//
// cache1.get("123");
//
// logger.info("After cache1.get");
// cache2.get("123");
//
// logger.info("After cache2.get");
} finally {
// Finish JVM
cache1.getCacheManager().stop();
}
}
use of org.keycloak.models.sessions.infinispan.entities.UserSessionEntity in project keycloak by keycloak.
the class ConcurrencyDistributedRemoveSessionTest method createSessionEntity.
private static SessionEntityWrapper<UserSessionEntity> createSessionEntity(String sessionId) {
// Create 100 initial sessions
UserSessionEntity session = new UserSessionEntity();
session.setId(sessionId);
session.setRealmId("foo");
session.setBrokerSessionId("!23123123");
session.setBrokerUserId(null);
session.setUser("foo");
session.setLoginUsername("foo");
session.setIpAddress("123.44.143.178");
session.setStarted(Time.currentTime());
session.setLastSessionRefresh(Time.currentTime());
AuthenticatedClientSessionEntity clientSession = new AuthenticatedClientSessionEntity(UUID.randomUUID());
clientSession.setAuthMethod("saml");
clientSession.setAction("something");
clientSession.setTimestamp(1234);
session.getAuthenticatedClientSessions().put(CLIENT_1_UUID.toString(), clientSession.getId());
SessionEntityWrapper<UserSessionEntity> wrappedSession = new SessionEntityWrapper<>(session);
return wrappedSession;
}
Aggregations