1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
59
59
/**
60
60
* @author Luke Taylor
61
61
* @author Rob Winch
62
+ * @author Gengwu Zhao
62
63
*/
63
64
public class ActiveDirectoryLdapAuthenticationProviderTests {
64
65
@@ -70,9 +71,13 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
70
71
71
72
UsernamePasswordAuthenticationToken joe = UsernamePasswordAuthenticationToken .unauthenticated ("joe" , "password" );
72
73
74
+ DirContext ctx ;
75
+
73
76
@ BeforeEach
74
- public void setUp () {
77
+ public void setUp () throws NamingException {
75
78
this .provider = new ActiveDirectoryLdapAuthenticationProvider ("mydomain.eu" , "ldap://192.168.1.200/" );
79
+ this .ctx = mock (DirContext .class );
80
+ given (this .ctx .getNameInNamespace ()).willReturn ("" );
76
81
}
77
82
78
83
@ Test
@@ -90,15 +95,13 @@ public void successfulAuthenticationProducesExpectedAuthorities() throws Excepti
90
95
@ Test
91
96
public void customSearchFilterIsUsedForSuccessfulAuthentication () throws Exception {
92
97
String customSearchFilter = "(&(objectClass=user)(sAMAccountName={0}))" ;
93
- DirContext ctx = mock (DirContext .class );
94
- given (ctx .getNameInNamespace ()).willReturn ("" );
95
98
DirContextAdapter dca = new DirContextAdapter ();
96
99
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
97
- given (ctx .search (any (Name .class ), eq (customSearchFilter ), any (Object [].class ), any (SearchControls .class )))
100
+ given (this . ctx .search (any (Name .class ), eq (customSearchFilter ), any (Object [].class ), any (SearchControls .class )))
98
101
.willReturn (new MockNamingEnumeration (sr ));
99
102
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
100
103
"mydomain.eu" , "ldap://192.168.1.200/" );
101
- customProvider .contextFactory = createContextFactoryReturning (ctx );
104
+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
102
105
customProvider .setSearchFilter (customSearchFilter );
103
106
Authentication result = customProvider .authenticate (this .joe );
104
107
assertThat (result .isAuthenticated ()).isTrue ();
@@ -107,34 +110,31 @@ public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Excepti
107
110
@ Test
108
111
public void defaultSearchFilter () throws Exception {
109
112
final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))" ;
110
- DirContext ctx = mock (DirContext .class );
111
- given (ctx .getNameInNamespace ()).willReturn ("" );
112
113
DirContextAdapter dca = new DirContextAdapter ();
113
114
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
114
- given (ctx .search (any (Name .class ), eq (defaultSearchFilter ), any (Object [].class ), any (SearchControls .class )))
115
+ given (this . ctx .search (any (Name .class ), eq (defaultSearchFilter ), any (Object [].class ), any (SearchControls .class )))
115
116
.willReturn (new MockNamingEnumeration (sr ));
116
117
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
117
118
"mydomain.eu" , "ldap://192.168.1.200/" );
118
- customProvider .contextFactory = createContextFactoryReturning (ctx );
119
+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
119
120
Authentication result = customProvider .authenticate (this .joe );
120
121
assertThat (result .isAuthenticated ()).isTrue ();
121
- verify (ctx ).search (any (Name .class ), eq (defaultSearchFilter ), any (Object [].class ), any (SearchControls .class ));
122
+ verify (this .ctx ).search (any (Name .class ), eq (defaultSearchFilter ), any (Object [].class ),
123
+ any (SearchControls .class ));
122
124
}
123
125
124
126
// SEC-2897,SEC-2224
125
127
@ Test
126
128
public void bindPrincipalAndUsernameUsed () throws Exception {
127
129
final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))" ;
128
130
ArgumentCaptor <Object []> captor = ArgumentCaptor .forClass (Object [].class );
129
- DirContext ctx = mock (DirContext .class );
130
- given (ctx .getNameInNamespace ()).willReturn ("" );
131
131
DirContextAdapter dca = new DirContextAdapter ();
132
132
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
133
- given (ctx .search (any (Name .class ), eq (defaultSearchFilter ), captor .capture (), any (SearchControls .class )))
133
+ given (this . ctx .search (any (Name .class ), eq (defaultSearchFilter ), captor .capture (), any (SearchControls .class )))
134
134
.willReturn (new MockNamingEnumeration (sr ));
135
135
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
136
136
"mydomain.eu" , "ldap://192.168.1.200/" );
137
- customProvider .contextFactory = createContextFactoryReturning (ctx );
137
+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
138
138
Authentication result = customProvider .authenticate (this .joe );
139
139
assertThat (
captor .
getValue ()).
containsExactly (
"[email protected] " ,
"joe" );
140
140
assertThat (result .isAuthenticated ()).isTrue ();
@@ -153,36 +153,30 @@ public void setSearchFilterEmpty() {
153
153
@ Test
154
154
public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal () throws Exception {
155
155
this .provider = new ActiveDirectoryLdapAuthenticationProvider (null , "ldap://192.168.1.200/" );
156
- DirContext ctx = mock (DirContext .class );
157
- given (ctx .getNameInNamespace ()).willReturn ("" );
158
156
DirContextAdapter dca = new DirContextAdapter ();
159
157
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
160
- given (ctx .search (eq (LdapNameBuilder .newInstance ("DC=mydomain,DC=eu" ).build ()), any (String .class ),
158
+ given (this . ctx .search (eq (LdapNameBuilder .newInstance ("DC=mydomain,DC=eu" ).build ()), any (String .class ),
161
159
any (Object [].class ), any (SearchControls .class )))
162
160
.willReturn (new MockNamingEnumeration (sr ));
163
- this .provider .contextFactory = createContextFactoryReturning (ctx );
161
+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
164
162
assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
165
163
this .
provider .
authenticate (
UsernamePasswordAuthenticationToken .
unauthenticated (
"[email protected] " ,
"password" ));
166
164
}
167
165
168
166
@ Test
169
167
public void failedUserSearchCausesBadCredentials () throws Exception {
170
- DirContext ctx = mock (DirContext .class );
171
- given (ctx .getNameInNamespace ()).willReturn ("" );
172
- given (ctx .search (any (Name .class ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
168
+ given (this .ctx .search (any (Name .class ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
173
169
.willThrow (new NameNotFoundException ());
174
- this .provider .contextFactory = createContextFactoryReturning (ctx );
170
+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
175
171
assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
176
172
}
177
173
178
174
// SEC-2017
179
175
@ Test
180
176
public void noUserSearchCausesUsernameNotFound () throws Exception {
181
- DirContext ctx = mock (DirContext .class );
182
- given (ctx .getNameInNamespace ()).willReturn ("" );
183
- given (ctx .search (any (Name .class ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
177
+ given (this .ctx .search (any (Name .class ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
184
178
.willReturn (new EmptyEnumeration <>());
185
- this .provider .contextFactory = createContextFactoryReturning (ctx );
179
+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
186
180
assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
187
181
}
188
182
@@ -196,16 +190,14 @@ public void sec2500PreventAnonymousBind() {
196
190
@ Test
197
191
@ SuppressWarnings ("unchecked" )
198
192
public void duplicateUserSearchCausesError () throws Exception {
199
- DirContext ctx = mock (DirContext .class );
200
- given (ctx .getNameInNamespace ()).willReturn ("" );
201
193
NamingEnumeration <SearchResult > searchResults = mock (NamingEnumeration .class );
202
194
given (searchResults .hasMore ()).willReturn (true , true , false );
203
195
SearchResult searchResult = mock (SearchResult .class );
204
196
given (searchResult .getObject ()).willReturn (new DirContextAdapter ("ou=1" ), new DirContextAdapter ("ou=2" ));
205
197
given (searchResults .next ()).willReturn (searchResult );
206
- given (ctx .search (any (Name .class ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
198
+ given (this . ctx .search (any (Name .class ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
207
199
.willReturn (searchResults );
208
- this .provider .contextFactory = createContextFactoryReturning (ctx );
200
+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
209
201
assertThatExceptionOfType (IncorrectResultSizeDataAccessException .class )
210
202
.isThrownBy (() -> this .provider .authenticate (this .joe ));
211
203
}
@@ -357,16 +349,14 @@ DirContext createContext(Hashtable<?, ?> env) {
357
349
358
350
private void checkAuthentication (String rootDn , ActiveDirectoryLdapAuthenticationProvider provider )
359
351
throws NamingException {
360
- DirContext ctx = mock (DirContext .class );
361
- given (ctx .getNameInNamespace ()).willReturn ("" );
362
352
DirContextAdapter dca = new DirContextAdapter ();
363
353
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
364
354
@ SuppressWarnings ("deprecation" )
365
355
Name searchBaseDn = LdapNameBuilder .newInstance (rootDn ).build ();
366
- given (ctx .search (eq (searchBaseDn ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
356
+ given (this . ctx .search (eq (searchBaseDn ), any (String .class ), any (Object [].class ), any (SearchControls .class )))
367
357
.willReturn (new MockNamingEnumeration (sr ))
368
358
.willReturn (new MockNamingEnumeration (sr ));
369
- provider .contextFactory = createContextFactoryReturning (ctx );
359
+ provider .contextFactory = createContextFactoryReturning (this . ctx );
370
360
Authentication result = provider .authenticate (this .joe );
371
361
assertThat (result .getAuthorities ()).isEmpty ();
372
362
dca .addAttributeValue ("memberOf" , "CN=Admin,CN=Users,DC=mydomain,DC=eu" );
0 commit comments