bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
SessionManager.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.session;
2 
3 import java.sql.SQLException;
4 import java.util.Iterator;
5 import java.util.LinkedHashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.UUID;
9 import java.util.concurrent.ConcurrentHashMap;
10 import java.util.concurrent.TimeUnit;
11 
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;
18 import org.openslx.bwlp.thrift.iface.UserInfo;
21 import org.openslx.util.QuickTimer;
22 import org.openslx.util.QuickTimer.Task;
23 
28 public class SessionManager
29 {
30  private static Logger log = LogManager.getLogger( SessionManager.class );
31 
32  // Map of currently known sessions
33  private static final Map<String, Session> sessions = new LinkedHashMap<>();
34 
35  // Map of pending "access code -> session" lookups
36  private static final Map<String, AccessCode> accessCodes = new ConcurrentHashMap<>();
37 
38  public static ClientSessionData addSession( Session session )
39  {
40  final String authToken = Hash.md5( UUID.randomUUID().toString() );
41  final String sessionId = Hash.sha256( UUID.randomUUID().toString() );
42 
43  synchronized ( sessions ) {
44  sessions.put( authToken, session );
45  sessions.put( sessionId, session );
46  }
47  UserInfo ui = session.getUserInfo();
48  List<Satellite> sats;
49  try {
50  sats = DbSatellite.getSatellites( ui );
51  } catch ( SQLException e ) {
52  sats = null;
53  }
54  return new ClientSessionData( sessionId, authToken, sats, ui );
55  }
56 
57  public static ClientSessionData addSession( Session session, String accessToken )
58  {
59  ClientSessionData s = addSession( session );
60  if ( accessToken != null ) {
61  accessCodes.put( accessToken, new AccessCode( s, null ) );
62  }
63  return s;
64  }
65 
66  public static void addAuthError( TAuthorizationException ex, String accessToken )
67  {
68  if ( accessToken == null )
69  return;
70  accessCodes.put( accessToken, new AccessCode( null, ex ) );
71  }
72 
73  static {
74  QuickTimer.scheduleAtFixedDelay( new Task() {
75  @Override
76  public void fire()
77  {
78  synchronized ( sessions ) {
79  Iterator<Session> it = sessions.values().iterator();
80  while ( it.hasNext() ) {
81  final Session s = it.next();
82  if ( s.timedOut() ) {
83  it.remove();
84  }
85  }
86  }
87  Iterator<AccessCode> it = accessCodes.values().iterator();
88  while ( it.hasNext() ) {
89  final AccessCode s = it.next();
90  if ( s.timedOut() ) {
91  it.remove();
92  }
93  }
94  }
95  }, 123, TimeUnit.MINUTES.toMillis( 13 ) );
96  }
97 
101  public static Session getSessionFromToken( String token )
102  {
103  if ( token == null || token.length() != 32 ) {
104  log.debug( "invalid token format: " + token );
105  return null;
106  }
107  final Session session;
108  synchronized ( sessions ) {
109  session = sessions.get( token );
110  }
111  if ( session == null || session.timedOut() ) {
112  return null;
113  }
114  return session;
115  }
116 
120  public static Session getSessionFromSessionId( String sessionId )
121  {
122  if ( sessionId == null || sessionId.length() != 64 ) {
123  log.debug( "invalid sessionid format: " + sessionId );
124  return null;
125  }
126  final Session session;
127  synchronized ( sessions ) {
128  session = sessions.get( sessionId );
129  }
130  if ( session == null || session.timedOut() ) {
131  return null;
132  }
133  session.refresh();
134  return session;
135  }
136 
137  public static Session getSessionFromSessionIdOrToken( String sessionId )
138  {
139  final Session session;
140  synchronized ( sessions ) {
141  session = sessions.get( sessionId );
142  }
143  if ( session == null || session.timedOut() ) {
144  return null;
145  }
146  return session;
147  }
148 
149  public static void invalidate( String sessionId )
150  {
151  if ( sessionId == null || sessionId.length() != 64 ) {
152  log.debug( "invalidate: invalid sessionid format: " + sessionId );
153  return;
154  }
155  synchronized ( sessions ) {
156  Session session = sessions.get( sessionId );
157  if ( session != null ) {
158  session.invalidate();
159  }
160  }
161  }
162 
168  public static ClientSessionData getSessionFromAccessCode( String accessCode )
169  throws TNotFoundException, TAuthorizationException
170  {
171  AccessCode data = accessCodes.remove( accessCode );
172  if ( data == null )
173  throw new TNotFoundException();
174  if ( data.ex != null )
175  throw data.ex;
176  return data.clientSession;
177  }
178 
179 }
static String md5(final byte[] bytes)
Compute md5 hash of given binary data.
Definition: Hash.java:60
static Session getSessionFromToken(String token)
Get from userToken, known to satellite servers.
synchronized boolean timedOut()
Definition: Session.java:39
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...
static final Map< String, Session > sessions
static void addAuthError(TAuthorizationException ex, String accessToken)
static ClientSessionData addSession(Session session)
static String sha256(final byte[] bytes)
Compute sha256 hash of given binary data.
Definition: Hash.java:85
Simple representation of a user session.
Definition: Session.java:11
static ClientSessionData addSession(Session session, String accessToken)
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
Definition: AccessCode.java:13