11/*
2- * Copyright 2002-2022 the original author or authors.
2+ * Copyright 2002-2024 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
5959/**
6060 * @author Luke Taylor
6161 * @author Rob Winch
62+ * @author Gengwu Zhao
6263 */
6364public class ActiveDirectoryLdapAuthenticationProviderTests {
6465
@@ -70,9 +71,13 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
7071
7172 UsernamePasswordAuthenticationToken joe = UsernamePasswordAuthenticationToken .unauthenticated ("joe" , "password" );
7273
74+ DirContext ctx ;
75+
7376 @ BeforeEach
74- public void setUp () {
77+ public void setUp () throws NamingException {
7578 this .provider = new ActiveDirectoryLdapAuthenticationProvider ("mydomain.eu" , "ldap://192.168.1.200/" );
79+ this .ctx = mock (DirContext .class );
80+ given (this .ctx .getNameInNamespace ()).willReturn ("" );
7681 }
7782
7883 @ Test
@@ -90,15 +95,13 @@ public void successfulAuthenticationProducesExpectedAuthorities() throws Excepti
9095 @ Test
9196 public void customSearchFilterIsUsedForSuccessfulAuthentication () throws Exception {
9297 String customSearchFilter = "(&(objectClass=user)(sAMAccountName={0}))" ;
93- DirContext ctx = mock (DirContext .class );
94- given (ctx .getNameInNamespace ()).willReturn ("" );
9598 DirContextAdapter dca = new DirContextAdapter ();
9699 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 )))
98101 .willReturn (new MockNamingEnumeration (sr ));
99102 ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
100103 "mydomain.eu" , "ldap://192.168.1.200/" );
101- customProvider .contextFactory = createContextFactoryReturning (ctx );
104+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
102105 customProvider .setSearchFilter (customSearchFilter );
103106 Authentication result = customProvider .authenticate (this .joe );
104107 assertThat (result .isAuthenticated ()).isTrue ();
@@ -107,34 +110,31 @@ public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Excepti
107110 @ Test
108111 public void defaultSearchFilter () throws Exception {
109112 final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))" ;
110- DirContext ctx = mock (DirContext .class );
111- given (ctx .getNameInNamespace ()).willReturn ("" );
112113 DirContextAdapter dca = new DirContextAdapter ();
113114 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 )))
115116 .willReturn (new MockNamingEnumeration (sr ));
116117 ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
117118 "mydomain.eu" , "ldap://192.168.1.200/" );
118- customProvider .contextFactory = createContextFactoryReturning (ctx );
119+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
119120 Authentication result = customProvider .authenticate (this .joe );
120121 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 ));
122124 }
123125
124126 // SEC-2897,SEC-2224
125127 @ Test
126128 public void bindPrincipalAndUsernameUsed () throws Exception {
127129 final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))" ;
128130 ArgumentCaptor <Object []> captor = ArgumentCaptor .forClass (Object [].class );
129- DirContext ctx = mock (DirContext .class );
130- given (ctx .getNameInNamespace ()).willReturn ("" );
131131 DirContextAdapter dca = new DirContextAdapter ();
132132 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 )))
134134 .willReturn (new MockNamingEnumeration (sr ));
135135 ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider (
136136 "mydomain.eu" , "ldap://192.168.1.200/" );
137- customProvider .contextFactory = createContextFactoryReturning (ctx );
137+ customProvider .contextFactory = createContextFactoryReturning (this . ctx );
138138 Authentication result = customProvider .authenticate (this .joe );
139139 assertThat (
captor .
getValue ()).
containsExactly (
"[email protected] " ,
"joe" );
140140 assertThat (result .isAuthenticated ()).isTrue ();
@@ -153,36 +153,30 @@ public void setSearchFilterEmpty() {
153153 @ Test
154154 public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal () throws Exception {
155155 this .provider = new ActiveDirectoryLdapAuthenticationProvider (null , "ldap://192.168.1.200/" );
156- DirContext ctx = mock (DirContext .class );
157- given (ctx .getNameInNamespace ()).willReturn ("" );
158156 DirContextAdapter dca = new DirContextAdapter ();
159157 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 ),
161159 any (Object [].class ), any (SearchControls .class )))
162160 .willReturn (new MockNamingEnumeration (sr ));
163- this .provider .contextFactory = createContextFactoryReturning (ctx );
161+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
164162 assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
165163 this .
provider .
authenticate (
UsernamePasswordAuthenticationToken .
unauthenticated (
"[email protected] " ,
"password" ));
166164 }
167165
168166 @ Test
169167 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 )))
173169 .willThrow (new NameNotFoundException ());
174- this .provider .contextFactory = createContextFactoryReturning (ctx );
170+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
175171 assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
176172 }
177173
178174 // SEC-2017
179175 @ Test
180176 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 )))
184178 .willReturn (new EmptyEnumeration <>());
185- this .provider .contextFactory = createContextFactoryReturning (ctx );
179+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
186180 assertThatExceptionOfType (BadCredentialsException .class ).isThrownBy (() -> this .provider .authenticate (this .joe ));
187181 }
188182
@@ -196,16 +190,14 @@ public void sec2500PreventAnonymousBind() {
196190 @ Test
197191 @ SuppressWarnings ("unchecked" )
198192 public void duplicateUserSearchCausesError () throws Exception {
199- DirContext ctx = mock (DirContext .class );
200- given (ctx .getNameInNamespace ()).willReturn ("" );
201193 NamingEnumeration <SearchResult > searchResults = mock (NamingEnumeration .class );
202194 given (searchResults .hasMore ()).willReturn (true , true , false );
203195 SearchResult searchResult = mock (SearchResult .class );
204196 given (searchResult .getObject ()).willReturn (new DirContextAdapter ("ou=1" ), new DirContextAdapter ("ou=2" ));
205197 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 )))
207199 .willReturn (searchResults );
208- this .provider .contextFactory = createContextFactoryReturning (ctx );
200+ this .provider .contextFactory = createContextFactoryReturning (this . ctx );
209201 assertThatExceptionOfType (IncorrectResultSizeDataAccessException .class )
210202 .isThrownBy (() -> this .provider .authenticate (this .joe ));
211203 }
@@ -357,16 +349,14 @@ DirContext createContext(Hashtable<?, ?> env) {
357349
358350 private void checkAuthentication (String rootDn , ActiveDirectoryLdapAuthenticationProvider provider )
359351 throws NamingException {
360- DirContext ctx = mock (DirContext .class );
361- given (ctx .getNameInNamespace ()).willReturn ("" );
362352 DirContextAdapter dca = new DirContextAdapter ();
363353 SearchResult sr = new SearchResult ("CN=Joe Jannsen,CN=Users" , dca , dca .getAttributes ());
364354 @ SuppressWarnings ("deprecation" )
365355 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 )))
367357 .willReturn (new MockNamingEnumeration (sr ))
368358 .willReturn (new MockNamingEnumeration (sr ));
369- provider .contextFactory = createContextFactoryReturning (ctx );
359+ provider .contextFactory = createContextFactoryReturning (this . ctx );
370360 Authentication result = provider .authenticate (this .joe );
371361 assertThat (result .getAuthorities ()).isEmpty ();
372362 dca .addAttributeValue ("memberOf" , "CN=Admin,CN=Users,DC=mydomain,DC=eu" );
0 commit comments