1 package org.openslx.imagemaster.session;
3 import java.sql.SQLException;
4 import java.util.Iterator;
5 import java.util.LinkedHashMap;
9 import java.util.concurrent.ConcurrentHashMap;
10 import java.util.concurrent.TimeUnit;
12 import org.apache.logging.log4j.LogManager;
13 import org.apache.logging.log4j.Logger;
14 import org.
openslx.bwlp.thrift.iface.ClientSessionData;
15 import org.
openslx.bwlp.thrift.iface.Satellite;
16 import org.
openslx.bwlp.thrift.iface.TAuthorizationException;
17 import org.
openslx.bwlp.thrift.iface.TNotFoundException;
33 private static final Map<String, Session>
sessions =
new LinkedHashMap<>();
36 private static final Map<String, AccessCode>
accessCodes =
new ConcurrentHashMap<>();
40 final String authToken =
Hash.
md5( UUID.randomUUID().toString() );
41 final String sessionId =
Hash.
sha256( UUID.randomUUID().toString() );
44 sessions.put( authToken, session );
45 sessions.put( sessionId, session );
51 }
catch ( SQLException e ) {
54 return new ClientSessionData( sessionId, authToken, sats, ui );
60 if ( accessToken != null ) {
61 accessCodes.put( accessToken,
new AccessCode( s, null ) );
66 public static void addAuthError( TAuthorizationException ex, String accessToken )
68 if ( accessToken == null )
70 accessCodes.put( accessToken,
new AccessCode( null, ex ) );
74 QuickTimer.scheduleAtFixedDelay(
new Task() {
79 Iterator<Session> it = sessions.values().iterator();
80 while ( it.hasNext() ) {
87 Iterator<AccessCode> it = accessCodes.values().iterator();
88 while ( it.hasNext() ) {
89 final AccessCode s = it.next();
95 }, 123, TimeUnit.MINUTES.toMillis( 13 ) );
103 if ( token == null || token.length() != 32 ) {
104 log.debug(
"invalid token format: " + token );
109 session = sessions.get( token );
111 if ( session == null || session.
timedOut() ) {
122 if ( sessionId == null || sessionId.length() != 64 ) {
123 log.debug(
"invalid sessionid format: " + sessionId );
128 session = sessions.get( sessionId );
130 if ( session == null || session.
timedOut() ) {
141 session = sessions.get( sessionId );
143 if ( session == null || session.
timedOut() ) {
151 if ( sessionId == null || sessionId.length() != 64 ) {
152 log.debug(
"invalidate: invalid sessionid format: " + sessionId );
156 Session session = sessions.get( sessionId );
157 if ( session != null ) {
169 throws TNotFoundException, TAuthorizationException
171 AccessCode data = accessCodes.remove( accessCode );
173 throw new TNotFoundException();
174 if ( data.
ex != null )
static String md5(final byte[] bytes)
Compute md5 hash of given binary data.
static Session getSessionFromToken(String token)
Get from userToken, known to satellite servers.
synchronized boolean timedOut()
static List< Satellite > getSatellites(UserInfo ui)
static ClientSessionData getSessionFromAccessCode(String accessCode)
Get the according session data (satToken, masterToken) for given access code, which was supplied by t...
final ClientSessionData clientSession
static final Map< String, Session > sessions
static void addAuthError(TAuthorizationException ex, String accessToken)
static void invalidate(String sessionId)
static ClientSessionData addSession(Session session)
static String sha256(final byte[] bytes)
Compute sha256 hash of given binary data.
Simple representation of a user session.
synchronized void invalidate()
static ClientSessionData addSession(Session session, String accessToken)
synchronized void refresh()
static Session getSessionFromSessionIdOrToken(String sessionId)
static final Map< String, AccessCode > accessCodes
Class for managing active user sessions.
static Session getSessionFromSessionId(String sessionId)
Get from sessionId, only known by client/user and us.
final TAuthorizationException ex