summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/frontend/LoginAuthenticator.java
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2013-06-03 18:30:46 -0700
committerJesse Morgan <jesse@jesterpm.net>2013-06-03 18:30:46 -0700
commit9024f49ca63d0e7204bf61de5b06717e2ee6a1e6 (patch)
tree70526506fc982ddb84317966f7d7bcdd878b47d9 /src/com/p4square/grow/frontend/LoginAuthenticator.java
parentee39f861d7a5147a8292647488b7dfc0cbeb1cce (diff)
Adding Login logic and updating navigation.
Adding a LoginAuthenticator to parse session cookies and a LoginPageResource to handle submissions to the login page. Also updating the navigation header to show Take Assessment when the user is authenticated.
Diffstat (limited to 'src/com/p4square/grow/frontend/LoginAuthenticator.java')
-rw-r--r--src/com/p4square/grow/frontend/LoginAuthenticator.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/com/p4square/grow/frontend/LoginAuthenticator.java b/src/com/p4square/grow/frontend/LoginAuthenticator.java
new file mode 100644
index 0000000..64f5827
--- /dev/null
+++ b/src/com/p4square/grow/frontend/LoginAuthenticator.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.grow.frontend;
+
+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;
+
+/**
+ * LoginAuthenticator decrypts a cookie containing the user's session info
+ * and makes that information available as the ClientInfo's User object.
+ *
+ * If this Authenticator is not optional, the user will be redirected to a
+ * login page.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class LoginAuthenticator extends Authenticator {
+ private static Logger cLog = Logger.getLogger(LoginAuthenticator.class);
+
+ public static final String COOKIE_NAME = "growsession";
+
+ private final String mLoginPage;
+
+ public LoginAuthenticator(Context context, boolean optional, String loginPage) {
+ super(context, optional);
+
+ mLoginPage = loginPage;
+ }
+
+ 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;
+ }
+}