aboutsummaryrefslogtreecommitdiff
path: root/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/AuthenticationSessionStoreage.java
diff options
context:
space:
mode:
Diffstat (limited to 'id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/AuthenticationSessionStoreage.java')
-rw-r--r--id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/AuthenticationSessionStoreage.java242
1 files changed, 242 insertions, 0 deletions
diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/AuthenticationSessionStoreage.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/AuthenticationSessionStoreage.java
new file mode 100644
index 000000000..70156deb7
--- /dev/null
+++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/storage/AuthenticationSessionStoreage.java
@@ -0,0 +1,242 @@
+package at.gv.egovernment.moa.id.storage;
+
+import iaik.util.logging.Log;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.hibernate.HibernateException;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import at.gv.egovernment.moa.id.AuthenticationException;
+import at.gv.egovernment.moa.id.MOAIDException;
+import at.gv.egovernment.moa.id.auth.data.AuthenticationSession;
+import at.gv.egovernment.moa.id.commons.db.HibernateUtil;
+import at.gv.egovernment.moa.id.commons.db.dao.session.AssertionStore;
+import at.gv.egovernment.moa.id.commons.db.dao.session.AuthenticatedSessionStore;
+import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException;
+import at.gv.egovernment.moa.id.util.Random;
+import at.gv.egovernment.moa.logging.Logger;
+import at.gv.egovernment.moa.util.MiscUtil;
+
+public class AuthenticationSessionStoreage {
+
+ //private static HashMap<String, AuthenticationSession> sessionStore = new HashMap<String, AuthenticationSession>();
+
+ public static boolean isAuthenticated(String moaSessionID) {
+
+ AuthenticatedSessionStore session;
+
+ try {
+ session = searchInDatabase(moaSessionID);
+ return session.isAuthenticated();
+
+ } catch (MOADatabaseException e) {
+ return false;
+ }
+ }
+
+
+ public static AuthenticationSession createSession() throws MOADatabaseException {
+ String id = Random.nextRandom();
+ AuthenticationSession session = new AuthenticationSession(id);
+
+ AuthenticatedSessionStore dbsession = new AuthenticatedSessionStore();
+ dbsession.setSessionid(id);
+ dbsession.setAuthenticated(false);
+
+ //set Timestamp in this state, because automated timestamp generation is buggy in Hibernate 4.2.1
+ dbsession.setCreated(new Date());
+ dbsession.setUpdated(new Date());
+
+ dbsession.setSession(SerializationUtils.serialize(session));
+
+ //store AssertionStore element to Database
+ try {
+ HibernateUtil.saveOrUpdate(dbsession);
+ Log.info("MOASession with sessionID=" + id + " is stored in Database");
+
+ } catch (MOADatabaseException e) {
+ Logger.warn("MOASession could not be created.");
+ throw new MOADatabaseException(e);
+ }
+
+ return session;
+ }
+
+ public static void storeSession(AuthenticationSession session) throws MOADatabaseException {
+
+ try {
+ AuthenticatedSessionStore dbsession = searchInDatabase(session.getSessionID());
+ dbsession.setAuthenticated(session.isAuthenticated());
+ dbsession.setSession(SerializationUtils.serialize(session));
+
+ //set Timestamp in this state, because automated timestamp generation is buggy in Hibernate 4.2.1
+ dbsession.setUpdated(new Date());
+
+ HibernateUtil.saveOrUpdate(dbsession);
+ Log.info("MOASession with sessionID=" + session.getSessionID() + " is stored in Database");
+
+ } catch (MOADatabaseException e) {
+ Logger.warn("MOASession could not be stored.");
+ throw new MOADatabaseException(e);
+ }
+
+
+ }
+
+ public static void destroySession(String moaSessionID) throws MOADatabaseException {
+
+ try {
+ AuthenticatedSessionStore dbsession = searchInDatabase(moaSessionID);
+ HibernateUtil.delete(dbsession);
+
+ } catch (MOADatabaseException e) {
+ Logger.warn("MOASession could not be destroyed.");
+ throw new MOADatabaseException(e);
+ }
+
+
+ }
+
+// public static void dumpSessionStore() {
+// synchronized (sessionStore) {
+// Set<String> keys = sessionStore.keySet();
+// Iterator<String> 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 {
+
+ try {
+ AuthenticatedSessionStore dbsession = searchInDatabase(session.getSessionID());
+
+ String id = Random.nextRandom();
+ session.setSessionID(id);
+
+ dbsession.setSessionid(id);
+ dbsession.setAuthenticated(session.isAuthenticated());
+ dbsession.setSession(SerializationUtils.serialize(session));
+
+ //set Timestamp in this state, because automated timestamp generation is buggy in Hibernate 4.2.1
+ dbsession.setUpdated(new Date());
+
+ HibernateUtil.saveOrUpdate(dbsession);
+
+ return id;
+
+ } catch (MOADatabaseException e) {
+ throw new AuthenticationException("TODO!", null);
+ }
+
+
+
+
+
+// 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) throws MOADatabaseException {
+
+ try {
+ AuthenticatedSessionStore dbsession = searchInDatabase(sessionID);
+ AuthenticationSession session = (AuthenticationSession) SerializationUtils.deserialize(dbsession.getSession());
+
+ return session;
+
+ } catch (MOADatabaseException e) {
+ Logger.info("No MOA Session with id: " + sessionID);
+ return null;
+
+ } catch (Throwable e) {
+ Log.warn("MOASession deserialization-exception by using MOASessionID=" + sessionID);
+ throw new MOADatabaseException("MOASession deserialization-exception");
+ }
+ }
+
+ public static void clean(long now, long authDataTimeOut) {
+ Date expioredate = new Date(now - authDataTimeOut);
+
+ List<AuthenticatedSessionStore> results;
+ Session session = HibernateUtil.getCurrentSession();
+
+ synchronized (session) {
+ session.beginTransaction();
+ Query query = session.getNamedQuery("getMOAISessionsWithTimeOut");
+ query.setTimestamp("timeout", expioredate);
+ results = query.list();
+ session.getTransaction().commit();
+ }
+
+ if (results.size() != 0) {
+ for(AuthenticatedSessionStore result : results) {
+ try {
+ HibernateUtil.delete(result);
+ Logger.info("Authenticated session with sessionID=" + result.getSessionid()
+ + " after session timeout.");
+
+ } catch (HibernateException e){
+ Logger.warn("Authenticated session with sessionID=" + result.getSessionid()
+ + " not removed after timeout! (Error during Database communication)", e);
+ }
+
+ }
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ private static AuthenticatedSessionStore searchInDatabase(String sessionID) throws MOADatabaseException {
+ MiscUtil.assertNotNull(sessionID, "moasessionID");
+ Logger.trace("Get authenticated session with sessionID " + sessionID + " from database.");
+ Session session = HibernateUtil.getCurrentSession();
+
+ List result;
+
+ synchronized (session) {
+ session.beginTransaction();
+ Query query = session.getNamedQuery("getSessionWithID");
+ query.setString("sessionid", sessionID);
+ result = query.list();
+
+ //send transaction
+ session.getTransaction().commit();
+ }
+
+ Logger.trace("Found entries: " + result.size());
+
+ //Assertion requires an unique artifact
+ if (result.size() != 1) {
+ Logger.trace("No entries found.");
+ throw new MOADatabaseException("No session found with this sessionID");
+ }
+
+ return (AuthenticatedSessionStore) result.get(0);
+ }
+}