From 38c12cf70ef4714a7fc508f7fbaf44487ea971b7 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Mon, 31 Mar 2014 22:35:43 -0700 Subject: Locking down restlet library version. While trying to fix this issue, I also moved FMFacade into this package and fixed a couple bugs that snuck into the last commit. --- .../session/SessionCookieAuthenticator.java | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/com/p4square/session/SessionCookieAuthenticator.java (limited to 'src/com/p4square/session/SessionCookieAuthenticator.java') diff --git a/src/com/p4square/session/SessionCookieAuthenticator.java b/src/com/p4square/session/SessionCookieAuthenticator.java new file mode 100644 index 0000000..0074b77 --- /dev/null +++ b/src/com/p4square/session/SessionCookieAuthenticator.java @@ -0,0 +1,59 @@ +/* + * 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 + */ +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; + } + } + +} -- cgit v1.2.3