From 9024f49ca63d0e7204bf61de5b06717e2ee6a1e6 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Mon, 3 Jun 2013 18:30:46 -0700 Subject: 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. --- .../p4square/grow/frontend/LoginAuthenticator.java | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/com/p4square/grow/frontend/LoginAuthenticator.java (limited to 'src/com/p4square/grow/frontend/LoginAuthenticator.java') 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 + */ +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; + } +} -- cgit v1.2.3