diff options
Diffstat (limited to 'src/com/p4square/session')
-rw-r--r-- | src/com/p4square/session/Session.java | 59 | ||||
-rw-r--r-- | src/com/p4square/session/SessionAuthenticator.java | 36 | ||||
-rw-r--r-- | src/com/p4square/session/SessionCheckingAuthenticator.java | 39 | ||||
-rw-r--r-- | src/com/p4square/session/SessionCookieAuthenticator.java | 59 | ||||
-rw-r--r-- | src/com/p4square/session/SessionCreatingAuthenticator.java | 46 | ||||
-rw-r--r-- | src/com/p4square/session/Sessions.java | 155 |
6 files changed, 0 insertions, 394 deletions
diff --git a/src/com/p4square/session/Session.java b/src/com/p4square/session/Session.java deleted file mode 100644 index 1bb65f5..0000000 --- a/src/com/p4square/session/Session.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2013 Jesse Morgan - */ - -package com.p4square.session; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import org.restlet.security.User; - -/** - * - * @author Jesse Morgan <jesse@jesterpm.net> - */ -public class Session { - static final long LIFETIME = 86400000; - - private final String mSessionId; - private final User mUser; - private final Map<String, String> mData; - private long mExpires; - - Session(User user) { - mUser = user; - mSessionId = UUID.randomUUID().toString(); - mExpires = System.currentTimeMillis() + LIFETIME; - mData = new HashMap<String, String>(); - } - - void touch() { - mExpires = System.currentTimeMillis() + LIFETIME; - } - - boolean isExpired() { - return System.currentTimeMillis() > mExpires; - } - - public String getId() { - return mSessionId; - } - - public Object get(String key) { - return mData.get(key); - } - - public void put(String key, String value) { - mData.put(key, value); - } - - public User getUser() { - return mUser; - } - - public Map<String, String> getMap() { - return mData; - } -} diff --git a/src/com/p4square/session/SessionAuthenticator.java b/src/com/p4square/session/SessionAuthenticator.java deleted file mode 100644 index 794e1a8..0000000 --- a/src/com/p4square/session/SessionAuthenticator.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2013 Jesse Morgan - */ - -package com.p4square.session; - -import org.restlet.Context; -import org.restlet.Request; -import org.restlet.Response; -import org.restlet.security.Authenticator; -import org.restlet.security.User; - -/** - * - * @author Jesse Morgan <jesse@jesterpm.net> - */ -public class SessionAuthenticator /*extends Authenticator*/ { - /* - @Override - protected boolean authenticate(Request request, Response response) { - // Check for authentication cookie - final String cookie = request.getCookies().getFirstValue(COOKIE_NAME); - if (cookie != null) { - cLog.debug("Got cookie: " + cookie); - // TODO Decrypt user info - User user = new User(cookie); - request.getClientInfo().setUser(user); - return true; - } - - // Challenge the user if not authenticated - response.redirectSeeOther(mLoginPage); - return false; - } - */ -} diff --git a/src/com/p4square/session/SessionCheckingAuthenticator.java b/src/com/p4square/session/SessionCheckingAuthenticator.java deleted file mode 100644 index 489d6a0..0000000 --- a/src/com/p4square/session/SessionCheckingAuthenticator.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2013 Jesse Morgan - */ - -package com.p4square.session; - -import org.apache.log4j.Logger; - -import org.restlet.Context; -import org.restlet.Request; -import org.restlet.Response; -import org.restlet.security.Authenticator; - -/** - * Authenticator which succeeds if a valid Session exists. - * - * @author Jesse Morgan <jesse@jesterpm.net> - */ -public class SessionCheckingAuthenticator extends Authenticator { - private static final Logger LOG = Logger.getLogger(SessionCheckingAuthenticator.class); - - public SessionCheckingAuthenticator(Context context, boolean optional) { - super(context, optional); - } - - protected boolean authenticate(Request request, Response response) { - Session s = Sessions.getInstance().get(request); - - if (s != null) { - LOG.debug("Found session for user " + s.getUser()); - request.getClientInfo().setUser(s.getUser()); - return true; - - } else { - return false; - } - } - -} diff --git a/src/com/p4square/session/SessionCookieAuthenticator.java b/src/com/p4square/session/SessionCookieAuthenticator.java deleted file mode 100644 index 0074b77..0000000 --- a/src/com/p4square/session/SessionCookieAuthenticator.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2013 Jesse Morgan - */ - -package com.p4square.session; - -import org.apache.log4j.Logger; - -import org.restlet.Context; -import org.restlet.Request; -import org.restlet.Response; -import org.restlet.security.Authenticator; - -/** - * - * @author Jesse Morgan <jesse@jesterpm.net> - */ -public class SessionCookieAuthenticator extends Authenticator { - private static final Logger LOG = Logger.getLogger(SessionCookieAuthenticator.class); - - private static final String COOKIE_NAME = "S"; - - private final Sessions mSessions; - - public SessionCookieAuthenticator(Context context, boolean optional, Sessions sessions) { - super(context, optional); - - mSessions = sessions; - } - - protected boolean authenticate(Request request, Response response) { - final String cookie = request.getCookies().getFirstValue(COOKIE_NAME); - - if (request.getClientInfo().isAuthenticated()) { - // Request is already authenticated... create session if it doesn't exist. - if (cookie == null) { - Session s = mSessions.create(request.getClientInfo().getUser()); - response.getCookieSettings().add(COOKIE_NAME, s.getId()); - } - - return true; - - } else { - // Check for authentication cookie - if (cookie != null) { - LOG.debug("Got cookie: " + cookie); - - Session s = mSessions.get(cookie); - if (s != null) { - request.getClientInfo().setUser(s.getUser()); - return true; - } - } - - return false; - } - } - -} diff --git a/src/com/p4square/session/SessionCreatingAuthenticator.java b/src/com/p4square/session/SessionCreatingAuthenticator.java deleted file mode 100644 index 3ec14b4..0000000 --- a/src/com/p4square/session/SessionCreatingAuthenticator.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2013 Jesse Morgan - */ - -package com.p4square.session; - -import org.apache.log4j.Logger; - -import org.restlet.Context; -import org.restlet.Request; -import org.restlet.Response; -import org.restlet.security.Authenticator; -import org.restlet.security.User; - -/** - * Authenticator which creates a Session for the request and adds a cookie - * to the response. - * - * The Request MUST be Authenticated and MUST have a User object associated. - * - * @author Jesse Morgan <jesse@jesterpm.net> - */ -public class SessionCreatingAuthenticator extends Authenticator { - private static final Logger LOG = Logger.getLogger(SessionCreatingAuthenticator.class); - - public SessionCreatingAuthenticator(Context context) { - super(context, true); - } - - protected boolean authenticate(Request request, Response response) { - if (Sessions.getInstance().get(request) != null) { - return true; - } - - User user = request.getClientInfo().getUser(); - - if (request.getClientInfo().isAuthenticated() && user != null) { - Sessions.getInstance().create(request, response); - LOG.debug(response); - return true; - } - - return false; - } - -} diff --git a/src/com/p4square/session/Sessions.java b/src/com/p4square/session/Sessions.java deleted file mode 100644 index 9f9dda0..0000000 --- a/src/com/p4square/session/Sessions.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright 2013 Jesse Morgan - */ - -package com.p4square.session; - -import java.util.concurrent.ConcurrentHashMap; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; - -import org.restlet.Response; -import org.restlet.Request; -import org.restlet.data.CookieSetting; -import org.restlet.security.User; - -/** - * Singleton Session Manager. - * - * @author Jesse Morgan <jesse@jesterpm.net> - */ -public class Sessions { - private static final String COOKIE_NAME = "S"; - private static final int DELETE = 0; - - private static final Sessions THE = new Sessions(); - public static Sessions getInstance() { - return THE; - } - - private final Map<String, Session> mSessions; - private final Timer mCleanupTimer; - - private Sessions() { - mSessions = new ConcurrentHashMap<String, Session>(); - - mCleanupTimer = new Timer("sessionCleaner", true); - mCleanupTimer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - for (Session s : mSessions.values()) { - if (s.isExpired()) { - mSessions.remove(s.getId()); - } - } - } - }, Session.LIFETIME, Session.LIFETIME); - } - - /** - * Get a session by ID. - * - * @param sessionid - * The Session id - * @return The Session if found and not expired, null otherwise. - */ - public Session get(String sessionid) { - Session s = mSessions.get(sessionid); - - if (s != null && !s.isExpired()) { - s.touch(); - return s; - } - - return null; - } - - /** - * Get the Session associated with the Request. - * - * @param request - * The request to fetch a session for. - * @return A session or null if no session is found. - */ - public Session get(Request request) { - final String cookie = request.getCookies().getFirstValue(COOKIE_NAME); - - if (cookie != null) { - return get(cookie); - } - - return null; - } - - /** - * Create a new Session for the given User object. - * - * @param user - * The User to associate with the Session. - * @return The new Session object. - */ - public Session create(User user) { - if (user == null) { - throw new IllegalArgumentException("Can not create session for null user."); - } - - Session s = new Session(user); - mSessions.put(s.getId(), s); - - return s; - } - - /** - * Delete a Session. - * - * @param sessionid - * The id of the Session to remove. - */ - public void delete(String sessionid) { - mSessions.remove(sessionid); - } - - /** - * Create a new Session and add the Session cookie to the response. - * - * @param request - * The request to create the Session for. - * @param response - * The response to add the session cookie to. - * @return The new Session. - */ - public Session create(Request request, Response response) { - Session s = create(request.getClientInfo().getUser()); - - CookieSetting cookie = new CookieSetting(COOKIE_NAME, s.getId()); - cookie.setPath("/"); - - request.getCookies().add(cookie); - response.getCookieSettings().add(cookie); - - return s; - } - - /** - * Remove a Session and delete the cookies. - * - * @param request - * The request with the session cookie to remove - * @param response - * The response to remove the session cookie from. - */ - public void delete(Request request, Response response) { - final String sessionid = request.getCookies().getFirstValue(COOKIE_NAME); - - delete(sessionid); - - CookieSetting cookie = new CookieSetting(COOKIE_NAME, ""); - cookie.setPath("/"); - cookie.setMaxAge(DELETE); - - request.getCookies().add(cookie); - response.getCookieSettings().add(cookie); - } - -} |