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 int hashCode() { return getId().hashCode(); }
@Override public void restartSession(RealmModel realm, UserModel user, String loginUsername, String ipAddress, String authMethod, boolean rememberMe, String brokerSessionId, String brokerUserId) { UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { provider.updateSessionEntity(entity, realm, user, loginUsername, ipAddress, authMethod, rememberMe, brokerSessionId, brokerUserId); entity.setState(null); entity.getNotes().clear(); entity.getAuthenticatedClientSessions().clear(); } }; update(task); }
private AuthenticatedClientSessionAdapter importClientSession(UserSessionAdapter sessionToImportInto, AuthenticatedClientSessionModel clientSession, InfinispanChangelogBasedTransaction<String, UserSessionEntity> userSessionUpdateTx, InfinispanChangelogBasedTransaction<UUID, AuthenticatedClientSessionEntity> clientSessionUpdateTx, boolean offline) { AuthenticatedClientSessionEntity entity = createAuthenticatedClientSessionInstance(clientSession, sessionToImportInto.getRealm().getId(), offline); final UUID clientSessionId = entity.getId(); SessionUpdateTask<AuthenticatedClientSessionEntity> createClientSessionTask = Tasks.addIfAbsentSync(); clientSessionUpdateTx.addTask(entity.getId(), createClientSessionTask, entity); AuthenticatedClientSessionStore clientSessions = sessionToImportInto.getEntity().getAuthenticatedClientSessions(); clientSessions.put(clientSession.getClient().getId(), clientSessionId); SessionUpdateTask registerClientSessionTask = new RegisterClientSessionTask(clientSession.getClient().getId(), clientSessionId); userSessionUpdateTx.addTask(sessionToImportInto.getId(), registerClientSessionTask); return new AuthenticatedClientSessionAdapter(session,this, entity, clientSession.getClient(), sessionToImportInto, userSessionUpdateTx, clientSessionUpdateTx, offline); }
@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; }
void attachSession(UserSessionAdapter userSession, ClientSessionModel clientSession) { UserSessionEntity entity = userSession.getEntity(); String clientSessionId = clientSession.getId(); if (entity.getClientSessions() == null) { entity.setClientSessions(new HashSet<String>()); } if (!entity.getClientSessions().contains(clientSessionId)) { entity.getClientSessions().add(clientSessionId); userSession.update(); } }
UserSessionEntity getUserSessionEntity(UserSessionModel userSession, boolean offline) { if (userSession instanceof UserSessionAdapter) { return ((UserSessionAdapter) userSession).getEntity(); } else { return getUserSessionEntity(userSession.getId(), offline); } }
@Override public Map<String, AuthenticatedClientSessionModel> getAuthenticatedClientSessions() { AuthenticatedClientSessionStore clientSessionEntities = entity.getAuthenticatedClientSessions(); Map<String, AuthenticatedClientSessionModel> result = new HashMap<>(); List<String> removedClientUUIDS = new LinkedList<>(); if (clientSessionEntities != null) { clientSessionEntities.forEach((String key, UUID value) -> { // Check if client still exists ClientModel client = realm.getClientById(key); if (client != null) { final AuthenticatedClientSessionAdapter clientSession = provider.getClientSession(this, client, value, offline); if (clientSession != null) { result.put(key, clientSession); } } else { removedClientUUIDS.add(key); } }); } removeAuthenticatedClientSessions(removedClientUUIDS); return Collections.unmodifiableMap(result); }
@Override public void runUpdate(UserSessionEntity entity) { if (value == null) { if (entity.getNotes().containsKey(name)) { removeNote(name); } return; } entity.getNotes().put(name, value); }
@Override public String getLoginUsername() { if (entity.getLoginUsername() == null) { // this is a hack so that UserModel doesn't have to be available when offline token is imported. // see related JIRA - KEYCLOAK-5350 and corresponding test return getUser().getUsername(); } else { return entity.getLoginUsername(); } }
@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; }
void dettachSession(UserSessionAdapter userSession, ClientSessionModel clientSession) { UserSessionEntity entity = userSession.getEntity(); String clientSessionId = clientSession.getId(); if (entity.getClientSessions() != null && entity.getClientSessions().contains(clientSessionId)) { entity.getClientSessions().remove(clientSessionId); if (entity.getClientSessions().isEmpty()) { entity.setClientSessions(null); } userSession.update(); } }
protected void removeClientSession(RealmModel realm, ClientSessionModel clientSession, boolean offline) { Cache<String, SessionEntity> cache = getCache(offline); UserSessionModel userSession = clientSession.getUserSession(); if (userSession != null) { UserSessionEntity entity = ((UserSessionAdapter) userSession).getEntity(); if (entity.getClientSessions() != null) { entity.getClientSessions().remove(clientSession.getId()); } tx.replace(cache, entity.getId(), entity); } tx.remove(cache, clientSession.getId()); }
@Override public AuthenticatedClientSessionModel getAuthenticatedClientSessionByClient(String clientUUID) { AuthenticatedClientSessionStore clientSessionEntities = entity.getAuthenticatedClientSessions(); final UUID clientSessionId = clientSessionEntities.get(clientUUID); if (clientSessionId == null) { return null; } ClientModel client = realm.getClientById(clientUUID); if (client != null) { return provider.getClientSession(this, client, clientSessionId, offline); } removeAuthenticatedClientSessions(Collections.singleton(clientUUID)); return null; }
@Override public void removeNote(String name) { UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { entity.getNotes().remove(name); } }; update(task); }
@Override public int hashCode() { return getId().hashCode(); }
protected void removeClientSession(RealmModel realm, ClientSessionModel clientSession, boolean offline) { Cache<String, SessionEntity> cache = getCache(offline); UserSessionModel userSession = clientSession.getUserSession(); if (userSession != null) { UserSessionEntity entity = ((UserSessionAdapter) userSession).getEntity(); if (entity.getClientSessions() != null) { entity.getClientSessions().remove(clientSession.getId()); } tx.replace(cache, entity.getId(), entity); } tx.remove(cache, clientSession.getId()); }
UserSessionAdapter wrap(RealmModel realm, UserSessionEntity entity, boolean offline) { InfinispanChangelogBasedTransaction<String, UserSessionEntity> userSessionUpdateTx = getTransaction(offline); InfinispanChangelogBasedTransaction<UUID, AuthenticatedClientSessionEntity> clientSessionUpdateTx = getClientSessionTransaction(offline); return entity != null ? new UserSessionAdapter(session, this, userSessionUpdateTx, clientSessionUpdateTx, realm, entity, offline) : null; }
@Override public void setState(State state) { UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { entity.setState(state); } }; update(task); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof UserSessionModel)) return false; UserSessionModel that = (UserSessionModel) o; return that.getId().equals(getId()); }