1
2
3
4
5
6
7
8
9
10
11
12
13
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
38
39
40
41
42 public class DaoX509AuthoritiesPopulatorTests extends TestCase {
43
44
45 public DaoX509AuthoritiesPopulatorTests() {
46 }
47
48 public DaoX509AuthoritiesPopulatorTests(String arg0) {
49 super(arg0);
50 }
51
52
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=(.*?,");
81
82 try {
83 populator.afterPropertiesSet();
84 fail("Should have thrown IllegalArgumentException");
85 } catch (IllegalArgumentException failed) {
86
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
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
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
130 }
131 }
132
133
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 }