1   package com.atlassian.seraph.filter;
2   
3   import com.atlassian.seraph.auth.AuthenticationContext;
4   import com.atlassian.seraph.auth.Authenticator;
5   import com.atlassian.seraph.auth.DefaultAuthenticator;
6   import com.sun.security.auth.UserPrincipal;
7   import junit.framework.TestCase;
8   
9   import static org.mockito.Mockito.*;
10  
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  import java.security.Principal;
14  
15  public class TestContextAwareAuthenticator extends TestCase
16  {
17      AuthenticationContext mockAuthenticationContext;
18      HttpServletRequest mockRequest;
19      TestFilter testFilter;
20      BaseLoginFilter.SecurityHttpRequestWrapper testSecurityWrapper;
21      Principal testPrincipal;
22  
23      public void setUp() throws Exception
24      {
25          super.setUp();
26  
27          testFilter = new TestFilter();
28          testPrincipal = new UserPrincipal("Matt");
29  
30          mockAuthenticationContext = mock(AuthenticationContext.class);
31          testFilter.setAuthenticationContext(mockAuthenticationContext);
32  
33          mockRequest = mock(HttpServletRequest.class);
34          testSecurityWrapper = testFilter.getHttpRequestWrapper(mockRequest);
35  
36          stub(mockAuthenticationContext.getUser()).toReturn(testPrincipal);
37      }
38  
39      public void testContextAwareAuthenticator()
40      {
41          Authenticator authenticator = new AuthenticationContextAwareAuthenticator();
42          testFilter.setAuthenticator(authenticator);
43          assertEquals("Matt", testSecurityWrapper.getRemoteUser());
44      }
45  
46      public void testNonContextAwareAuthenticator()
47      {
48          Authenticator authenticator = new NonAuthenticationContextAwareAuthenticator();
49          testFilter.setAuthenticator(authenticator);
50          assertEquals("Bob", testSecurityWrapper.getRemoteUser());
51      }
52  
53      @com.atlassian.seraph.auth.AuthenticationContextAwareAuthenticator
54      public class AuthenticationContextAwareAuthenticator extends DefaultAuthenticator
55      {
56          protected boolean authenticate(Principal user, String password)
57          {
58              return true;
59          }
60  
61          protected Principal getUser(String username)
62          {
63              return null;
64          }
65  
66          public Principal getUser(HttpServletRequest request)
67          {
68              return new UserPrincipal("Bob");
69          }
70      }
71  
72      //Note: annotations are not inherited.
73      public class NonAuthenticationContextAwareAuthenticator extends AuthenticationContextAwareAuthenticator
74      { }
75  
76  
77      public class TestFilter extends BaseLoginFilter
78      {
79          private Authenticator authenticator;
80          private AuthenticationContext authenticationContext;
81  
82          public SecurityHttpRequestWrapper getHttpRequestWrapper(HttpServletRequest request)
83          {
84              return new SecurityHttpRequestWrapper(request);
85          }
86  
87          //We are required to implement this
88          public String login(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
89          {
90              return null;
91          }
92  
93          public void setAuthenticator(Authenticator authenticator)
94          {
95              this.authenticator = authenticator;
96          }
97  
98          protected Authenticator getAuthenticator()
99          {
100             return authenticator;
101         }
102 
103         public AuthenticationContext getAuthenticationContext()
104         {
105             return authenticationContext;
106         }
107 
108         public void setAuthenticationContext(AuthenticationContext authenticationContext)
109         {
110             this.authenticationContext = authenticationContext;
111         }
112     }
113 }