diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2013-08-27 08:28:16 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2013-08-27 08:28:16 -0700 |
commit | 362cef066725dec0ffaeacb91e7e3c287d68bc51 (patch) | |
tree | 712fa205868adf14ebe08ba24dffdd72bfe616dc /src/com/p4square/grow/frontend/session/Sessions.java | |
parent | 9b33aaf27cd8f73402ee9967c6b0fd76a90f8ebe (diff) |
Introducing F1 Authentication and Adding Site Content.
This change introduced the f1oauth and jesterpm oauth packages for
interacting with Fellowship One's developer API. I have also reworked
the login authentication to verify credentials through F1 and added
session management to track logged in users.
The Authenticator chain works as follows: on every page load we check
for a session cookie, if the cookie exists, the Request is marked as
authenticated and the OAuthUser object is restored in ClientInfo. If
this request is going to an account page, we require authentication. The
LoginFormAuthenticator checks if the user is already authenticated (via
cookie) and if not redirects the user to the login page. When the login
form is submitted, LoginFormAuthenticator catches the POST request and
authenticates the user through F1.
I'm also adding a new account page, but it is currently a work in
progress.
This commit also adds Allen's content to the site.
Diffstat (limited to 'src/com/p4square/grow/frontend/session/Sessions.java')
-rw-r--r-- | src/com/p4square/grow/frontend/session/Sessions.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/com/p4square/grow/frontend/session/Sessions.java b/src/com/p4square/grow/frontend/session/Sessions.java new file mode 100644 index 0000000..094d2f0 --- /dev/null +++ b/src/com/p4square/grow/frontend/session/Sessions.java @@ -0,0 +1,80 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.frontend.session; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.Map; + +import org.restlet.Response; +import org.restlet.Request; +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 Sessions THE = new Sessions(); + public static Sessions getInstance() { + return THE; + } + + private final Map<String, Session> mSessions; + + private Sessions() { + mSessions = new ConcurrentHashMap<String, Session>(); + } + + 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. + * @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; + } + + 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; + } + + /** + * Create a new Session and add the Session cookie to the response. + */ + public Session create(Request request, Response response) { + Session s = create(request.getClientInfo().getUser()); + + request.getCookies().add(COOKIE_NAME, s.getId()); + response.getCookieSettings().add(COOKIE_NAME, s.getId()); + + return s; + } +} |