package at.gv.egovernment.moa.id.moduls; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import at.gv.egovernment.moa.id.AuthenticationException; import at.gv.egovernment.moa.id.auth.data.AuthenticationSession; import at.gv.egovernment.moa.id.util.Random; import at.gv.egovernment.moa.logging.Logger; public class AuthenticationSessionStore { private static HashMap sessionStore = new HashMap(); public static boolean isAuthenticated(String moaSessionID) { synchronized (sessionStore) { if (sessionStore.containsKey(moaSessionID)) { return sessionStore.get(moaSessionID).isAuthenticated(); } } return false; } public static AuthenticationSession createSession() { String id = Random.nextRandom(); AuthenticationSession session = new AuthenticationSession(id); synchronized (sessionStore) { sessionStore.put(id, session); } return session; } public static void destroySession(String moaSessionID) { synchronized (sessionStore) { if (sessionStore.containsKey(moaSessionID)) { sessionStore.remove(moaSessionID); } } } public static void dumpSessionStore() { synchronized (sessionStore) { Set keys = sessionStore.keySet(); Iterator keyIterator = keys.iterator(); while(keyIterator.hasNext()) { String key = keyIterator.next(); AuthenticationSession session = sessionStore.get(key); Logger.info("Key: " + key + " -> " + session.toString()); } } } public static String changeSessionID(AuthenticationSession session) throws AuthenticationException { synchronized (sessionStore) { if (sessionStore.containsKey(session.getSessionID())) { AuthenticationSession theSession = sessionStore.get(session .getSessionID()); if (theSession != session) { throw new AuthenticationException("TODO!", null); } sessionStore.remove(session.getSessionID()); String id = Random.nextRandom(); session.setSessionID(id); sessionStore.put(id, session); return id; } } throw new AuthenticationException("TODO!", null); } public static AuthenticationSession getSession(String sessionID) { synchronized (sessionStore) { if (sessionStore.containsKey(sessionID)) { return sessionStore.get(sessionID); } } Logger.info("No MOA Session with id: " + sessionID); return null; } }