protected UserSessionAdapter getUserSession(RealmModel realm, String id, boolean offline) { Cache<String, SessionEntity> cache = getCache(offline); UserSessionEntity entity = (UserSessionEntity) cache.get(id); // Chance created in this transaction if (entity == null) { entity = (UserSessionEntity) tx.get(cache, id); } return wrap(realm, entity, offline); }
@Override public ClientSessionModel getClientSession(RealmModel realm, String id) { return getClientSession(realm, id, false); }
@Override public UserSessionModel getOfflineUserSession(RealmModel realm, String userSessionId) { return getUserSession(realm, userSessionId, true); }
@Override public void removeExpired(RealmModel realm) { removeExpiredUserSessions(realm); removeExpiredClientSessions(realm); removeExpiredOfflineUserSessions(realm); removeExpiredOfflineClientSessions(realm); removeExpiredClientInitialAccess(realm); }
@Override public void removeExpired(RealmModel realm) { log.debugf("Removing expired sessions"); removeExpiredUserSessions(realm); removeExpiredOfflineUserSessions(realm); }
@Override public UserSessionModel getUserSessionWithPredicate(RealmModel realm, String id, boolean offline, Predicate<UserSessionModel> predicate) { UserSessionModel userSession = getUserSession(realm, id, offline); if (userSession == null) { return null; Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); RemoteCache remoteCache = InfinispanUtil.getRemoteCache(cache); 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); return getUserSessionWithPredicate(realm, id, offline, predicate); } else { log.debugf("getUserSessionWithPredicate(%s): found, but predicate doesn't pass", id); removeUserSession(realm, userSession);
@Override public List<UserSessionModel> getOfflineUserSessions(RealmModel realm, ClientModel client, int first, int max) { return getUserSessions(realm, client, first, max, true); }
UserSessionAdapter wrap(RealmModel realm, UserSessionEntity entity, boolean offline) { Cache<String, SessionEntity> cache = getCache(offline); return entity != null ? new UserSessionAdapter(session, this, cache, realm, entity, offline) : null; }
@Override public AuthenticatedClientSessionModel createOfflineClientSession(AuthenticatedClientSessionModel clientSession, UserSessionModel offlineUserSession) { UserSessionAdapter userSessionAdapter = (offlineUserSession instanceof UserSessionAdapter) ? (UserSessionAdapter) offlineUserSession : getOfflineUserSession(offlineUserSession.getRealm(), offlineUserSession.getId()); InfinispanChangelogBasedTransaction<String, UserSessionEntity> userSessionUpdateTx = getTransaction(true); InfinispanChangelogBasedTransaction<UUID, AuthenticatedClientSessionEntity> clientSessionUpdateTx = getClientSessionTransaction(true); AuthenticatedClientSessionAdapter offlineClientSession = importClientSession(userSessionAdapter, clientSession, userSessionUpdateTx, clientSessionUpdateTx, true); // update timestamp to current time offlineClientSession.setTimestamp(Time.currentTime()); return offlineClientSession; }
protected void removeUserSessions(RealmModel realm, UserModel user, boolean offline) { Cache<String, SessionEntity> cache = getCache(offline); Iterator<String> itr = cache.entrySet().stream().filter(UserSessionPredicate.create(realm.getId()).user(user.getId())).map(Mappers.sessionId()).iterator(); while (itr.hasNext()) { removeUserSession(realm, itr.next(), offline); } }
protected List<UserSessionModel> getUserSessionModels(RealmModel realm, int firstResult, int maxResults, boolean offline, UserSessionPredicate predicate) { Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); cache = CacheDecorators.skipCacheLoaders(cache); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> clientSessionCache = getClientSessionCache(offline); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> clientSessionCacheDecorated = CacheDecorators.skipCacheLoaders(clientSessionCache); Stream<UserSessionEntity> stream = cache.entrySet().stream() .filter(predicate) .map(Mappers.userSessionEntity()) .sorted(Comparators.userSessionLastSessionRefresh()); if (firstResult > 0) { stream = stream.skip(firstResult); } if (maxResults > 0) { stream = stream.limit(maxResults); } final List<UserSessionModel> sessions = new LinkedList<>(); Iterator<UserSessionEntity> itr = stream.iterator(); while (itr.hasNext()) { UserSessionEntity userSessionEntity = itr.next(); sessions.add(wrap(realm, userSessionEntity, offline)); } return sessions; }
.map((UserSessionModel persistentUserSession) -> { UserSessionEntity userSessionEntityToImport = createUserSessionEntityInstance(persistentUserSession); AuthenticatedClientSessionEntity clientSessionToImport = createAuthenticatedClientSessionInstance(clientSession, userSessionEntityToImport.getRealmId(), offline); Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = CacheDecorators.skipCacheLoaders(getCache(offline)); cache.putAll(sessionsById);
@Override public long getActiveUserSessions(RealmModel realm, ClientModel client) { return getUserSessionsCount(realm, client, false); }
private void removeLocalUserSessions(String realmId, boolean offline) { FuturesHelper futures = new FuturesHelper(); Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); Cache<String, SessionEntityWrapper<UserSessionEntity>> localCache = CacheDecorators.localCache(cache); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> clientSessionCache = getClientSessionCache(offline); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> localClientSessionCache = CacheDecorators.localCache(clientSessionCache); Cache<String, SessionEntityWrapper<UserSessionEntity>> localCacheStoreIgnore = CacheDecorators.skipCacheLoaders(localCache); final AtomicInteger userSessionsSize = new AtomicInteger(); localCacheStoreIgnore .entrySet() .stream() .filter(SessionPredicate.create(realmId)) .map(Mappers.userSessionEntity()) .forEach(new Consumer<UserSessionEntity>() { @Override public void accept(UserSessionEntity userSessionEntity) { userSessionsSize.incrementAndGet(); // Remove session from remoteCache too. Use removeAsync for better perf Future future = localCache.removeAsync(userSessionEntity.getId()); futures.addTask(future); userSessionEntity.getAuthenticatedClientSessions().forEach((clientUUID, clientSessionId) -> { Future f = localClientSessionCache.removeAsync(clientSessionId); futures.addTask(f); }); } }); futures.waitForAllToFinish(); log.debugf("Removed %d sessions in realm %s. Offline: %b", (Object) userSessionsSize.get(), realmId, offline); }
@Override public InfinispanUserSessionProvider create(KeycloakSession session) { InfinispanConnectionProvider connections = session.getProvider(InfinispanConnectionProvider.class); Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = connections.getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME); Cache<String, SessionEntityWrapper<UserSessionEntity>> offlineSessionsCache = connections.getCache(InfinispanConnectionProvider.OFFLINE_USER_SESSION_CACHE_NAME); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> clientSessionCache = connections.getCache(InfinispanConnectionProvider.CLIENT_SESSION_CACHE_NAME); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> offlineClientSessionsCache = connections.getCache(InfinispanConnectionProvider.OFFLINE_CLIENT_SESSION_CACHE_NAME); Cache<LoginFailureKey, SessionEntityWrapper<LoginFailureEntity>> loginFailures = connections.getCache(InfinispanConnectionProvider.LOGIN_FAILURE_CACHE_NAME); return new InfinispanUserSessionProvider(session, remoteCacheInvoker, lastSessionRefreshStore, offlineLastSessionRefreshStore, persisterLastSessionRefreshStore, keyGenerator, cache, offlineSessionsCache, clientSessionCache, offlineClientSessionsCache, loginFailures); }
@Override public UserSessionModel createOfflineUserSession(UserSessionModel userSession) { UserSessionAdapter offlineUserSession = importUserSession(userSession, true); // started and lastSessionRefresh set to current time int currentTime = Time.currentTime(); offlineUserSession.getEntity().setStarted(currentTime); offlineUserSession.setLastSessionRefresh(currentTime); return offlineUserSession; }
@Override public ClientSessionModel createOfflineClientSession(ClientSessionModel clientSession) { ClientSessionAdapter offlineClientSession = importClientSession(clientSession, true); // update timestamp to current time offlineClientSession.setTimestamp(Time.currentTime()); return offlineClientSession; }
@Override public List<UserSessionModel> getOfflineUserSessions(RealmModel realm, ClientModel client, int first, int max) { return getUserSessions(realm, client, first, max, true); }
ClientInitialAccessAdapter wrap(RealmModel realm, ClientInitialAccessEntity entity) { Cache<String, SessionEntity> cache = getCache(false); return entity != null ? new ClientInitialAccessAdapter(session, this, cache, realm, entity) : null; }
protected void removeUserSessions(RealmModel realm, UserModel user, boolean offline) { Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); cache = CacheDecorators.skipCacheLoaders(cache); Iterator<UserSessionEntity> itr = cache.entrySet().stream().filter(UserSessionPredicate.create(realm.getId()).user(user.getId())).map(Mappers.userSessionEntity()).iterator(); while (itr.hasNext()) { UserSessionEntity userSessionEntity = itr.next(); removeUserSession(userSessionEntity, offline); } }