Authentication through the canvas-attribute signed_request
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / UserCookieInterceptor.java
1 package de.juplo.yourshouter;
2
3
4 import java.io.IOException;
5 import java.util.Collections;
6 import java.util.regex.Pattern;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import org.springframework.social.connect.UsersConnectionRepository;
13 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
14
15
16 /**
17  * Intercepts all requests to handle the user-cookie.
18  *
19  * @author Kai Moritz
20  */
21 public final class UserCookieInterceptor extends HandlerInterceptorAdapter
22 {
23   private final static Logger LOG =
24       LoggerFactory.getLogger(UserCookieInterceptor.class);
25   private final static Pattern PATTERN = Pattern.compile("^/signin|canvas");
26
27
28   private final UsersConnectionRepository repository;
29
30
31   /**
32    * Creates an instance of this class, that uses the given instance of
33    * {@link UsersConnectionRepository}.
34    *
35    * @param repository
36    *     The instance of {@link UsersConnectionRepository} to use.
37    */
38   public UserCookieInterceptor(UsersConnectionRepository repository)
39   {
40     this.repository = repository;
41   }
42
43
44   /**
45    * Before a request is handled, the current user is loaded from the cookie,
46    * if the cookie is present and the user is known. If the user is not known,
47    * the cookie is removed.
48    *
49    * @param request
50    *     The {@link HttpServletRequest} that is intercepted.
51    * @param response
52    *     The {@link HttpServletResponse} that is intercepted.
53    * @param handler
54    *     The handler, that handles the intercepted request.
55    * @return
56    *     Always <code>true</code>, to indicate, that the intercepted request
57    *     should be handled normally.
58    * @throws java.io.IOException
59    *     if something wents wrong, while sending the redirect to the
60    *     sign-in-page.
61    */
62   @Override
63   public boolean preHandle(
64       HttpServletRequest request,
65       HttpServletResponse response,
66       Object handler
67       )
68       throws
69         IOException
70   {
71     if (PATTERN.matcher(request.getServletPath()).find())
72       return true;
73
74     String user = UserCookieGenerator.INSTANCE.readCookieValue(request);
75     if (user != null)
76     {
77       if (!repository
78           .findUserIdsConnectedTo("facebook", Collections.singleton(user))
79           .isEmpty()
80           )
81       {
82         LOG.info("loading user {} from cookie", user);
83         SecurityContext.setCurrentUser(user);
84         return true;
85       }
86       else
87       {
88         LOG.warn("user {} is not known!", user);
89         UserCookieGenerator.INSTANCE.removeCookie(response);
90       }
91     }
92
93     response.sendRedirect("/signin.html");
94     return false;
95   }
96
97   /**
98    * After a request, the user is removed from the security-context.
99    *
100    * @param request
101    *     The {@link HttpServletRequest} that is intercepted.
102    * @param response
103    *     The {@link HttpServletResponse} that is intercepted.
104    * @param handler
105    *     The handler, that handles the intercepted request.
106    * @param exception
107    *     If an exception was thrown during the handling of this request, it is
108    *     handed in through this parameter.
109    */
110   @Override
111   public void afterCompletion(
112       HttpServletRequest request,
113       HttpServletResponse response,
114       Object handler,
115       Exception exception
116       )
117   {
118     SecurityContext.remove();
119   }
120 }