1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.providers.x509.populator;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.BadCredentialsException;
21  import org.acegisecurity.GrantedAuthority;
22  import org.acegisecurity.GrantedAuthorityImpl;
23  
24  import org.acegisecurity.providers.x509.X509TestUtils;
25  
26  import org.acegisecurity.userdetails.User;
27  import org.acegisecurity.userdetails.UserDetails;
28  import org.acegisecurity.userdetails.UserDetailsService;
29  import org.acegisecurity.userdetails.UsernameNotFoundException;
30  
31  import org.springframework.dao.DataAccessException;
32  
33  import java.security.cert.X509Certificate;
34  
35  
36  /**
37   * Tests for {@link DaoX509AuthoritiesPopulator}
38   *
39   * @author Luke Taylor
40   * @version $Id: DaoX509AuthoritiesPopulatorTests.java 1994 2007-08-30 20:55:49Z luke_t $
41   */
42  public class DaoX509AuthoritiesPopulatorTests extends TestCase {
43      //~ Constructors ===================================================================================================
44  
45      public DaoX509AuthoritiesPopulatorTests() {
46      }
47  
48      public DaoX509AuthoritiesPopulatorTests(String arg0) {
49          super(arg0);
50      }
51  
52      //~ Methods ========================================================================================================
53  
54      public final void setUp() throws Exception {
55          super.setUp();
56      }
57  
58      public void testDefaultCNPatternMatch() throws Exception {
59          X509Certificate cert = X509TestUtils.buildTestCertificate();
60          DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
61  
62          populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
63          populator.afterPropertiesSet();
64          populator.getUserDetails(cert);
65      }
66  
67      public void testEmailPatternMatch() throws Exception {
68          X509Certificate cert = X509TestUtils.buildTestCertificate();
69          DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
70  
71          populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
72          populator.setSubjectDNRegex("emailAddress=(.*?),");
73          populator.afterPropertiesSet();
74          populator.getUserDetails(cert);
75      }
76  
77      public void testInvalidRegexFails() throws Exception {
78          DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
79          populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
80          populator.setSubjectDNRegex("CN=(.*?,"); // missing closing bracket on group
81  
82          try {
83              populator.afterPropertiesSet();
84              fail("Should have thrown IllegalArgumentException");
85          } catch (IllegalArgumentException failed) {
86              // ignored
87          }
88      }
89  
90      public void testMatchOnShoeSizeFieldInDNFails() throws Exception {
91          X509Certificate cert = X509TestUtils.buildTestCertificate();
92          DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
93  
94          populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
95          populator.setSubjectDNRegex("shoeSize=(.*?),");
96          populator.afterPropertiesSet();
97  
98          try {
99              populator.getUserDetails(cert);
100             fail("Should have thrown BadCredentialsException.");
101         } catch (BadCredentialsException failed) {
102             // ignored
103         }
104     }
105 
106     public void testPatternWithNoGroupFails() throws Exception {
107         X509Certificate cert = X509TestUtils.buildTestCertificate();
108         DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
109 
110         populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
111         populator.setSubjectDNRegex("CN=.*?,");
112         populator.afterPropertiesSet();
113 
114         try {
115             populator.getUserDetails(cert);
116             fail("Should have thrown IllegalArgumentException for regexp without group");
117         } catch (IllegalArgumentException e) {
118             // ignored
119         }
120     }
121 
122     public void testRequiresDao() throws Exception {
123         DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
124 
125         try {
126             populator.afterPropertiesSet();
127             fail("Should have thrown IllegalArgumentException");
128         } catch (IllegalArgumentException failed) {
129             // ignored
130         }
131     }
132 
133     //~ Inner Classes ==================================================================================================
134 
135     private class MockAuthenticationDaoMatchesNameOrEmail implements UserDetailsService {
136         public UserDetails loadUserByUsername(String username)
137             throws UsernameNotFoundException, DataAccessException {
138             if ("Luke Taylor".equals(username) || "luke@monkeymachine".equals(username)) {
139                 return new User("luke", "monkey", true, true, true, true,
140                     new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE")});
141             } else {
142                 throw new UsernameNotFoundException("Could not find: " + username);
143             }
144         }
145     }
146 }