1   package com.atlassian.seraph.auth;
2   
3   import javax.servlet.http.HttpServletRequest;
4   import javax.servlet.http.HttpServletResponse;
5   
6   /**
7    * An enumeration of why a login attempt has failed
8    */
9   public enum LoginReason
10  {
11      /**
12       * The user is not allowed to even attempt a login.  They are not allowed to by the {@link
13       * com.atlassian.seraph.elevatedsecurity.ElevatedSecurityGuard}
14       */
15      AUTHENTICATION_DENIED,
16      /**
17       * The user could not be authenticated.
18       */
19      AUTHENTICATED_FAILED,
20      /**
21       * The user could not be authorised.
22       */
23      AUTHORISATION_FAILED,
24      /**
25       * This indicates that person has in fact logged "out"
26       */
27      OUT,
28      /**
29       * The login was OK
30       */
31      OK;
32  
33      /**
34       * The name of the Header set by Seraph to indicate how the login process went 
35       */
36      public static final String X_SERAPH_LOGIN_REASON = "X-Seraph-LoginReason";
37      /**
38       * The name of the request attribute set by Seraph to indicate how the login process went
39       */
40      public static final String REQUEST_ATTR_NAME = LoginReason.class.getName();
41  
42      /**
43       * This will stamp an attribute to the request called "com.atlassian.seraph.auth.LoginReason" to the toString() of
44       * the enum and will also add an "X-Seraph-LoginReason" to this value
45       * <p/>
46       * Once a request/response is stamped, it wont be stamped again
47       *
48       * @param httpServletRequest  the request
49       * @param httpServletResponse the response
50       *
51       * @return this to give us a nice builder pattern
52       */
53      public LoginReason stampRequestResponse(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
54      {
55          if (httpServletRequest.getAttribute(REQUEST_ATTR_NAME) == null)
56          {
57              httpServletRequest.setAttribute(REQUEST_ATTR_NAME, this);
58              /**
59               * Whats this I hear say, how can there be no httpServletResponse when there is a httpServletRequest?  Its because the original makers of Seraph
60               * in their INFINITE design wisdom decide that you could make a call like Authenticator.getUser(httpServletRequest) BUT that it would
61               * expand our to call Authenticator.getUser(httpServletRequest, null).  FAN IN - FAN OUT !!
62               *
63               * So anyways its possible that you may not have a response object. depending on the weather and the call path.  So we handle it, he says shaking his head
64               * and generally feeling pretty disgusted, once again, in Seraphs design or lack thereof!
65               */
66              if (httpServletResponse != null)
67              {
68                  httpServletResponse.addHeader(X_SERAPH_LOGIN_REASON, this.toString());
69              }
70          }
71          return this;
72      }
73  
74      /**
75       * Checks if the given request has been stamped with this login reason.
76       *
77       * @param httpServletRequest the request
78       * @return true if the request has been stamped, false if otherwise
79       * @see #stampRequestResponse(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
80       */
81      public boolean isStamped(HttpServletRequest httpServletRequest)
82      {
83          return httpServletRequest.getAttribute(REQUEST_ATTR_NAME) == this;
84      }
85  }