@@ -76,8 +76,8 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
76
76
@ BeforeEach
77
77
public void setUp () throws NamingException {
78
78
this .provider = new ActiveDirectoryLdapAuthenticationProvider ("mydomain.eu" , "ldap://192.168.1.200/" );
79
- ctx = mock (DirContext .class );
80
- given (ctx .getNameInNamespace ()).willReturn ("" );
79
+ this . ctx = mock (DirContext .class );
80
+ given (this . ctx .getNameInNamespace ()).willReturn ("" );
81
81
}
82
82
83
83
@ Test
@@ -97,11 +97,11 @@ public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Excepti
97
97
String customSearchFilter = "(&(objectClass=user)(sAMAccountName={0}))" ;
98
98
DirContextAdapter dca = new DirContextAdapter ();
99
99
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
100
- 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 )))
101
101
.willReturn (new MockNamingEnumeration (sr ));
102
102
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
103
103
"mydomain.eu" , "ldap://192.168.1.200/" );
104
- customProvider .contextFactory = createContextFactoryReturning (ctx );
104
+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
105
105
customProvider .setSearchFilter (customSearchFilter );
106
106
Authentication result = customProvider .authenticate (this .joe );
107
107
assertThat (result .isAuthenticated ()).isTrue ();
@@ -112,14 +112,15 @@ public void defaultSearchFilter() throws Exception {
112
112
final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))" ;
113
113
DirContextAdapter dca = new DirContextAdapter ();
114
114
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
115
- 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 )))
116
116
.willReturn (new MockNamingEnumeration (sr ));
117
117
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
118
118
"mydomain.eu" , "ldap://192.168.1.200/" );
119
- customProvider .contextFactory = createContextFactoryReturning (ctx );
119
+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
120
120
Authentication result = customProvider .authenticate (this .joe );
121
121
assertThat (result .isAuthenticated ()).isTrue ();
122
- 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 ));
123
124
}
124
125
125
126
// SEC-2897,SEC-2224
@@ -129,11 +130,11 @@ public void bindPrincipalAndUsernameUsed() throws Exception {
129
130
ArgumentCaptor <Object []> captor = ArgumentCaptor .forClass (Object [].class );
130
131
DirContextAdapter dca = new DirContextAdapter ();
131
132
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
132
- 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 )))
133
134
.willReturn (new MockNamingEnumeration (sr ));
134
135
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
135
136
"mydomain.eu" , "ldap://192.168.1.200/" );
136
- customProvider .contextFactory = createContextFactoryReturning (ctx );
137
+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
137
138
Authentication result = customProvider .authenticate (this .joe );
138
139
assertThat (
captor .
getValue ()).
containsExactly (
"[email protected] " ,
"joe" );
139
140
assertThat (result .isAuthenticated ()).isTrue ();
@@ -154,28 +155,28 @@ public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws
154
155
this .provider = new ActiveDirectoryLdapAuthenticationProvider (null , "ldap://192.168.1.200/" );
155
156
DirContextAdapter dca = new DirContextAdapter ();
156
157
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
157
- 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 ),
158
159
any (Object [].class ), any (SearchControls .class )))
159
160
.willReturn (new MockNamingEnumeration (sr ));
160
- this .provider .contextFactory = createContextFactoryReturning (ctx );
161
+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
161
162
assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
162
163
this .
provider .
authenticate (
UsernamePasswordAuthenticationToken .
unauthenticated (
"[email protected] " ,
"password" ));
163
164
}
164
165
165
166
@ Test
166
167
public void failedUserSearchCausesBadCredentials () throws Exception {
167
- 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 )))
168
169
.willThrow (new NameNotFoundException ());
169
- this .provider .contextFactory = createContextFactoryReturning (ctx );
170
+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
170
171
assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
171
172
}
172
173
173
174
// SEC-2017
174
175
@ Test
175
176
public void noUserSearchCausesUsernameNotFound () throws Exception {
176
- 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 )))
177
178
.willReturn (new EmptyEnumeration <>());
178
- this .provider .contextFactory = createContextFactoryReturning (ctx );
179
+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
179
180
assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
180
181
}
181
182
@@ -194,9 +195,9 @@ public void duplicateUserSearchCausesError() throws Exception {
194
195
SearchResult searchResult = mock (SearchResult .class );
195
196
given (searchResult .getObject ()).willReturn (new DirContextAdapter ("ou=1" ), new DirContextAdapter ("ou=2" ));
196
197
given (searchResults .next ()).willReturn (searchResult );
197
- 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 )))
198
199
.willReturn (searchResults );
199
- this .provider .contextFactory = createContextFactoryReturning (ctx );
200
+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
200
201
assertThatExceptionOfType (IncorrectResultSizeDataAccessException .class )
201
202
.isThrownBy (() -> this .provider .authenticate (this .joe ));
202
203
}
@@ -352,10 +353,10 @@ private void checkAuthentication(String rootDn, ActiveDirectoryLdapAuthenticatio
352
353
SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
353
354
@ SuppressWarnings ("deprecation" )
354
355
Name searchBaseDn = LdapNameBuilder .newInstance (rootDn ).build ();
355
- 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 )))
356
357
.willReturn (new MockNamingEnumeration (sr ))
357
358
.willReturn (new MockNamingEnumeration (sr ));
358
- provider .contextFactory = createContextFactoryReturning (ctx );
359
+ provider .contextFactory = createContextFactoryReturning (this . ctx );
359
360
Authentication result = provider .authenticate (this .joe );
360
361
assertThat (result .getAuthorities ()).isEmpty ();
361
362
dca .addAttributeValue ("memberOf" , "CN=Admin,CN=Users,DC=mydomain,DC=eu" );
0 commit comments