1   package com.atlassian.seraph.filter;
2   
3   import com.atlassian.seraph.auth.AuthenticationErrorType;
4   
5   import javax.servlet.http.HttpServletRequest;
6   
7   /**
8    * Helper class to provide safe access to HTTP Request attributes set by the BaseLoginFilter.
9    *
10   * @since v2.4
11   */
12  public final class LoginFilterRequest
13  {
14      /**
15       * Returns the authentication status code set by the LoginFilter as a request Attribute.
16       * <p/>
17       * The possible statuses are:
18       * <ul>
19       * <li> BaseLoginFilter.LOGIN_SUCCESS - the login was processed, and user was logged in
20       * <li> BaseLoginFilter.LOGIN_FAILURE - the login was processed, the user gave a bad username or password
21       * <li> BaseLoginFilter.LOGIN_ERROR - the login was processed, an exception occurred trying to log the user in
22       * <li> BaseLoginFilter.LOGIN_NOATTEMPT - the login was no processed, no form parameters existed
23       * </ul>
24       *
25       * @param request the HttpServletRequest to retrieve the attribute from.
26       * @return the authentication status code set by the Login Filter as a request Attribute.
27       *
28       * @see com.atlassian.seraph.filter.BaseLoginFilter#login(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
29       */
30      public static String getAuthenticationStatus(HttpServletRequest request)
31      {
32          final Object authStatus = request.getAttribute(LoginFilter.OS_AUTHSTATUS_KEY);
33          if (authStatus == null)
34          {
35              return null;
36          }
37          if (authStatus instanceof String)
38          {
39              return (String) authStatus;
40          }
41          else
42          {
43              // Should never happen
44              throw new IllegalStateException("Illegal Authentication Status " + authStatus);
45          }
46      }
47  
48      /**
49       * Returns the authentication error type set by the LoginFilter as a request Attribute.
50       * <p/>
51       * This will only be set if authentication status is "error" ({@link com.atlassian.seraph.filter.BaseLoginFilter#LOGIN_ERROR}),
52       * and is not even then guaranteed to be set by all implementations of {@link com.atlassian.seraph.filter.BaseLoginFilter}.
53       * <p/>
54       * The original purpose of this was to indicate when a communication error occurs with a remote authentication server.
55       *
56       * @param request the HttpServletRequest to retrieve the attribute from.
57       * @return the authentication status code set by the Login Filter as a request Attribute.
58       *
59       * @see com.atlassian.seraph.auth.AuthenticatorException#getErrorType()
60       */
61      public static AuthenticationErrorType getAuthenticationErrorType(HttpServletRequest request)
62      {
63          final Object errorType = request.getAttribute(LoginFilter.AUTHENTICATION_ERROR_TYPE);
64          if (errorType == null)
65          {
66              return null;
67          }
68          if (errorType instanceof AuthenticationErrorType)
69          {
70              return (AuthenticationErrorType) errorType;
71          }
72          else
73          {
74              // Should never happen
75              throw new IllegalStateException("Illegal Authentication ErrorType " + errorType);
76          }
77      }
78  }