Added support for multiple DN patterns. Changes to favour constructor injection for mandatory properties. Renamed LdapUserInfo to prevent confusion with UserDetails interface.
This commit is contained in:
+9
-22
@@ -30,20 +30,19 @@ public class InitialDirContextFactoryTests extends AbstractLdapServerTestCase {
|
||||
// }
|
||||
|
||||
public void setUp() {
|
||||
idf = new DefaultInitialDirContextFactory();
|
||||
idf = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
idf.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
idf.setExtraEnvVars(EXTRA_ENV);
|
||||
}
|
||||
|
||||
public void testConnectionFailure() throws Exception {
|
||||
|
||||
idf.setInitialContextFactory("com.sun.jndi.ldap.LdapCtxFactory");
|
||||
// Use the wrong port
|
||||
idf.setUrl("ldap://localhost:60389");
|
||||
idf = new DefaultInitialDirContextFactory("ldap://localhost:60389");
|
||||
idf.setInitialContextFactory("com.sun.jndi.ldap.LdapCtxFactory");
|
||||
Hashtable env = new Hashtable();
|
||||
env.put("com.sun.jndi.ldap.connect.timeout", "200");
|
||||
idf.setExtraEnvVars(env);
|
||||
idf.afterPropertiesSet();
|
||||
|
||||
try {
|
||||
idf.newInitialDirContext();
|
||||
fail("Connection succeeded unexpectedly");
|
||||
@@ -52,8 +51,6 @@ public class InitialDirContextFactoryTests extends AbstractLdapServerTestCase {
|
||||
}
|
||||
|
||||
public void testAnonymousBindSucceeds() throws Exception {
|
||||
idf.setUrl(PROVIDER_URL);
|
||||
idf.afterPropertiesSet();
|
||||
DirContext ctx = idf.newInitialDirContext();
|
||||
// Connection pooling should be set by default for anon users.
|
||||
// Can't rely on this property being there with embedded server
|
||||
@@ -62,10 +59,9 @@ public class InitialDirContextFactoryTests extends AbstractLdapServerTestCase {
|
||||
}
|
||||
|
||||
public void testBindAsManagerSucceeds() throws Exception {
|
||||
idf.setUrl(PROVIDER_URL);
|
||||
idf.setManagerPassword(MANAGER_PASSWORD);
|
||||
idf.setManagerDn(MANAGER_USER);
|
||||
idf.afterPropertiesSet();
|
||||
|
||||
DirContext ctx = idf.newInitialDirContext();
|
||||
// Can't rely on this property being there with embedded server
|
||||
// assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
|
||||
@@ -73,10 +69,8 @@ public class InitialDirContextFactoryTests extends AbstractLdapServerTestCase {
|
||||
}
|
||||
|
||||
public void testInvalidPasswordCausesBadCredentialsException() throws Exception {
|
||||
idf.setUrl(PROVIDER_URL);
|
||||
idf.setManagerDn(MANAGER_USER);
|
||||
idf.setManagerPassword("wrongpassword");
|
||||
idf.afterPropertiesSet();
|
||||
try {
|
||||
DirContext ctx = idf.newInitialDirContext();
|
||||
fail("Authentication with wrong credentials should fail.");
|
||||
@@ -85,8 +79,6 @@ public class InitialDirContextFactoryTests extends AbstractLdapServerTestCase {
|
||||
}
|
||||
|
||||
public void testConnectionAsSpecificUserSucceeds() throws Exception {
|
||||
idf.setUrl(PROVIDER_URL);
|
||||
idf.afterPropertiesSet();
|
||||
DirContext ctx = idf.newInitialDirContext("uid=Bob,ou=people,dc=acegisecurity,dc=org",
|
||||
"bobspassword");
|
||||
// We don't want pooling for specific users.
|
||||
@@ -95,7 +87,7 @@ public class InitialDirContextFactoryTests extends AbstractLdapServerTestCase {
|
||||
}
|
||||
|
||||
public void testEnvironment() {
|
||||
idf.setUrl("ldap://acegisecurity.org/");
|
||||
idf = new DefaultInitialDirContextFactory("ldap://acegisecurity.org/");
|
||||
|
||||
// check basic env
|
||||
Hashtable env = idf.getEnvironment();
|
||||
@@ -124,20 +116,15 @@ public class InitialDirContextFactoryTests extends AbstractLdapServerTestCase {
|
||||
}
|
||||
|
||||
public void testBaseDnIsParsedFromCorrectlyFromUrl() throws Exception {
|
||||
idf.setUrl("ldap://acegisecurity.org/dc=acegisecurity,dc=org");
|
||||
idf.afterPropertiesSet();
|
||||
idf = new DefaultInitialDirContextFactory("ldap://acegisecurity.org/dc=acegisecurity,dc=org");
|
||||
assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
|
||||
|
||||
// Check with an empty root
|
||||
idf = new DefaultInitialDirContextFactory();
|
||||
idf.setUrl("ldap://acegisecurity.org/");
|
||||
idf.afterPropertiesSet();
|
||||
idf = new DefaultInitialDirContextFactory("ldap://acegisecurity.org/");
|
||||
assertEquals("", idf.getRootDn());
|
||||
|
||||
// Empty root without trailing slash
|
||||
idf = new DefaultInitialDirContextFactory();
|
||||
idf.setUrl("ldap://acegisecurity.org");
|
||||
idf.afterPropertiesSet();
|
||||
idf = new DefaultInitialDirContextFactory("ldap://acegisecurity.org");
|
||||
assertEquals("", idf.getRootDn());
|
||||
}
|
||||
|
||||
|
||||
+6
-13
@@ -6,11 +6,7 @@ import javax.naming.directory.BasicAttributes;
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
import org.acegisecurity.Authentication;
|
||||
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
import org.acegisecurity.providers.ldap.authenticator.FilterBasedLdapUserSearch;
|
||||
import org.acegisecurity.providers.ldap.authenticator.BindAuthenticator;
|
||||
import org.acegisecurity.providers.ldap.populator.DefaultLdapAuthoritiesPopulator;
|
||||
import org.acegisecurity.userdetails.UserDetails;
|
||||
|
||||
/**
|
||||
@@ -30,11 +26,8 @@ public class LdapAuthenticationProviderTests extends AbstractLdapServerTestCase
|
||||
}
|
||||
|
||||
public void testNormalUsage() throws Exception {
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider();
|
||||
|
||||
ldapProvider.setAuthenticator(new MockAuthenticator());
|
||||
ldapProvider.setLdapAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
ldapProvider.afterPropertiesSet();
|
||||
LdapAuthenticationProvider ldapProvider
|
||||
= new LdapAuthenticationProvider(new MockAuthenticator(), new MockAuthoritiesPopulator());
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("bob","bobspassword");
|
||||
UserDetails user = ldapProvider.retrieveUser("bob", token);
|
||||
@@ -59,7 +52,7 @@ public class LdapAuthenticationProviderTests extends AbstractLdapServerTestCase
|
||||
BindAuthenticator authenticator = new BindAuthenticator();
|
||||
//PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator();
|
||||
authenticator.setInitialDirContextFactory(dirCtxFactory);
|
||||
//authenticator.setUserDnPattern("cn={0},ou=people");
|
||||
//authenticator.setUserDnPatterns("cn={0},ou=people");
|
||||
|
||||
FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch();
|
||||
userSearch.setSearchBase("ou=people");
|
||||
@@ -78,7 +71,7 @@ public class LdapAuthenticationProviderTests extends AbstractLdapServerTestCase
|
||||
populator.setGroupSearchBase("ou=groups");
|
||||
populator.afterPropertiesSet();
|
||||
|
||||
ldapProvider.setLdapAuthoritiesPopulator(populator);
|
||||
ldapProvider.setAuthoritiesPopulator(populator);
|
||||
ldapProvider.setAuthenticator(authenticator);
|
||||
Authentication auth = ldapProvider.authenticate(new UsernamePasswordAuthenticationToken("Ben Alex","benspassword"));
|
||||
assertEquals(2, auth.getAuthorities().length);
|
||||
@@ -94,10 +87,10 @@ public class LdapAuthenticationProviderTests extends AbstractLdapServerTestCase
|
||||
class MockAuthenticator implements LdapAuthenticator {
|
||||
Attributes userAttributes = new BasicAttributes("cn","bob");
|
||||
|
||||
public LdapUserDetails authenticate(String username, String password) {
|
||||
public LdapUserInfo authenticate(String username, String password) {
|
||||
if(username.equals("bob") && password.equals("bobspassword")) {
|
||||
|
||||
return new LdapUserDetails("cn=bob,ou=people,dc=acegisecurity,dc=org", userAttributes);
|
||||
return new LdapUserInfo("cn=bob,ou=people,dc=acegisecurity,dc=org", userAttributes);
|
||||
}
|
||||
throw new BadCredentialsException("Authentication of Bob failed.");
|
||||
}
|
||||
|
||||
@@ -90,33 +90,6 @@ public class LdapTestServer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// private void startLdapServer() {
|
||||
// ApplicationContext factory = new ClassPathXmlApplicationContext( "org/acegisecurity/providers/ldap/apacheds-context.xml");
|
||||
// MutableServerStartupConfiguration cfg = ( MutableServerStartupConfiguration ) factory.getBean( "configuration" );
|
||||
// ClassPathResource ldifDir = new ClassPathResource("org/acegisecurity/providers/ldap/ldif");
|
||||
//
|
||||
// try {
|
||||
// cfg.setLdifDirectory(ldifDir.getFile());
|
||||
// } catch (IOException e) {
|
||||
// System.err.println("Failed to set LDIF directory for server");
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// Properties env = ( Properties ) factory.getBean( "environment" );
|
||||
//
|
||||
// env.setProperty( Context.PROVIDER_URL, "dc=acegisecurity,dc=org" );
|
||||
// env.setProperty( Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName() );
|
||||
// env.putAll( cfg.toJndiEnvironment() );
|
||||
//
|
||||
// try {
|
||||
// serverContext = new InitialDirContext( env );
|
||||
// } catch (NamingException e) {
|
||||
// System.err.println("Failed to start Apache DS");
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
private void initTestData() {
|
||||
createOu("people");
|
||||
createOu("groups");
|
||||
@@ -125,7 +98,7 @@ public class LdapTestServer {
|
||||
String[] developers = new String[]
|
||||
{"uid=ben,ou=people,dc=acegisecurity,dc=org", "uid=bob,ou=people,dc=acegisecurity,dc=org"};
|
||||
createGroup("developers","developer",developers);
|
||||
createGroup("managers","manager",new String[] { developers[0]});
|
||||
createGroup("managers","manager", new String[] { developers[0]});
|
||||
}
|
||||
|
||||
private void createManagerUser() {
|
||||
@@ -257,5 +230,4 @@ public class LdapTestServer {
|
||||
LdapTestServer server = new LdapTestServer(false);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+12
-16
@@ -1,12 +1,12 @@
|
||||
package org.acegisecurity.providers.ldap.authenticator;
|
||||
|
||||
import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory;
|
||||
import org.acegisecurity.providers.ldap.LdapUserDetails;
|
||||
import org.acegisecurity.providers.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
|
||||
/**
|
||||
* Tests {@link BindAuthenticator}.
|
||||
* Tests for {@link BindAuthenticator}.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
@@ -17,30 +17,26 @@ public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
|
||||
private BindAuthenticator authenticator;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory();
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
dirCtxFactory.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
|
||||
dirCtxFactory.setUrl(PROVIDER_URL);
|
||||
dirCtxFactory.afterPropertiesSet();
|
||||
authenticator = new BindAuthenticator();
|
||||
authenticator.setInitialDirContextFactory(dirCtxFactory);
|
||||
authenticator = new BindAuthenticator(dirCtxFactory);
|
||||
}
|
||||
|
||||
public void testUserDnPatternReturnsCorrectDn() throws Exception {
|
||||
authenticator.setUserDnPattern("cn={0},ou=people");
|
||||
assertEquals("cn=Joe,ou=people,"+ ROOT_DN, authenticator.getUserDn("Joe"));
|
||||
authenticator.setUserDnPatterns(new String[] {"cn={0},ou=people"});
|
||||
assertEquals("cn=Joe,ou=people,"+ ROOT_DN, authenticator.getUserDns("Joe").get(0));
|
||||
}
|
||||
|
||||
public void testAuthenticationWithCorrectPasswordSucceeds() throws Exception {
|
||||
authenticator.setUserDnPattern("uid={0},ou=people");
|
||||
LdapUserDetails user = authenticator.authenticate("bob","bobspassword");
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
LdapUserInfo user = authenticator.authenticate("bob","bobspassword");
|
||||
}
|
||||
|
||||
public void testAuthenticationWithWrongPasswordFails() {
|
||||
BindAuthenticator authenticator = new BindAuthenticator();
|
||||
BindAuthenticator authenticator = new BindAuthenticator(dirCtxFactory);
|
||||
|
||||
authenticator.setInitialDirContextFactory(dirCtxFactory);
|
||||
authenticator.setUserDnPattern("uid={0},ou=people");
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
|
||||
try {
|
||||
authenticator.authenticate("bob","wrongpassword");
|
||||
@@ -50,7 +46,7 @@ public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
|
||||
}
|
||||
|
||||
public void testAuthenticationWithUserSearch() throws Exception {
|
||||
LdapUserDetails user = new LdapUserDetails("uid=bob,ou=people," + ROOT_DN, null);
|
||||
LdapUserInfo user = new LdapUserInfo("uid=bob,ou=people," + ROOT_DN, null);
|
||||
authenticator.setUserSearch(new MockUserSearch(user));
|
||||
authenticator.afterPropertiesSet();
|
||||
authenticator.authenticate("bob","bobspassword");
|
||||
@@ -63,7 +59,7 @@ public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
|
||||
// BindAuthenticator authenticator = new BindAuthenticator();
|
||||
//
|
||||
// authenticator.setInitialDirContextFactory(dirCtxFactory);
|
||||
// authenticator.setUserDnPattern("cn={0},ou=people");
|
||||
// authenticator.setUserDnPatterns("cn={0},ou=people");
|
||||
// try {
|
||||
// authenticator.authenticate("Baz","bobspassword");
|
||||
// fail("Shouldn't be able to bind with invalid username");
|
||||
|
||||
+6
-8
@@ -2,7 +2,7 @@ package org.acegisecurity.providers.ldap.authenticator;
|
||||
|
||||
import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory;
|
||||
import org.acegisecurity.providers.ldap.LdapUserDetails;
|
||||
import org.acegisecurity.providers.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
|
||||
@@ -17,13 +17,11 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
|
||||
private FilterBasedLdapUserSearch locator;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory();
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
dirCtxFactory.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
|
||||
dirCtxFactory.setUrl(PROVIDER_URL);
|
||||
dirCtxFactory.setManagerDn(MANAGER_USER);
|
||||
dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
|
||||
dirCtxFactory.afterPropertiesSet();
|
||||
locator = new FilterBasedLdapUserSearch();
|
||||
locator.setSearchSubtree(false);
|
||||
locator.setSearchTimeLimit(0);
|
||||
@@ -42,7 +40,7 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
|
||||
locator.setSearchBase("ou=people");
|
||||
locator.setSearchFilter("(uid={0})");
|
||||
locator.afterPropertiesSet();
|
||||
LdapUserDetails bob = locator.searchForUser("bob");
|
||||
LdapUserInfo bob = locator.searchForUser("bob");
|
||||
// name is wrong with embedded apacheDS
|
||||
// assertEquals("uid=bob,ou=people,"+ROOT_DN, bob.getDn());
|
||||
}
|
||||
@@ -52,7 +50,7 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
|
||||
locator.setSearchFilter("(cn={0})");
|
||||
locator.setSearchSubtree(true);
|
||||
locator.afterPropertiesSet();
|
||||
LdapUserDetails ben = locator.searchForUser("Ben Alex");
|
||||
LdapUserInfo ben = locator.searchForUser("Ben Alex");
|
||||
// assertEquals("uid=ben,ou=people,"+ROOT_DN, bob.getDn());
|
||||
}
|
||||
|
||||
@@ -82,10 +80,10 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
|
||||
|
||||
public void testExtraFilterPartToExcludeBob() throws Exception {
|
||||
locator.setSearchBase("ou=people");
|
||||
locator.setSearchFilter("(&(cn=*)(!(uid={0})))");
|
||||
locator.setSearchFilter("(&(cn=*)(!(|(uid={0})(uid=marissa))))");
|
||||
|
||||
// Search for bob, get back ben...
|
||||
LdapUserDetails ben = locator.searchForUser("bob");
|
||||
LdapUserInfo ben = locator.searchForUser("bob");
|
||||
String cn = (String)ben.getAttributes().get("cn").get();
|
||||
assertEquals("Ben Alex", cn);
|
||||
// assertEquals("uid=ben,ou=people,"+ROOT_DN, ben.getDn());
|
||||
|
||||
+4
-4
@@ -1,19 +1,19 @@
|
||||
package org.acegisecurity.providers.ldap.authenticator;
|
||||
|
||||
import org.acegisecurity.providers.ldap.LdapUserDetails;
|
||||
import org.acegisecurity.providers.ldap.LdapUserInfo;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
public class MockUserSearch implements LdapUserSearch {
|
||||
LdapUserDetails user;
|
||||
LdapUserInfo user;
|
||||
|
||||
public MockUserSearch(LdapUserDetails user) {
|
||||
public MockUserSearch(LdapUserInfo user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public LdapUserDetails searchForUser(String username) {
|
||||
public LdapUserInfo searchForUser(String username) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-5
@@ -17,11 +17,14 @@ public class PasswordComparisonAuthenticatorMockTests extends MockObjectTestCase
|
||||
public void testLdapCompareIsUsedWhenPasswordIsNotRetrieved() throws Exception {
|
||||
Mock mockCtx = new Mock(DirContext.class);
|
||||
|
||||
PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator();
|
||||
authenticator.setUserDnPattern("cn={0},ou=people");
|
||||
authenticator.setInitialDirContextFactory(
|
||||
new MockInitialDirContextFactory((DirContext)mockCtx.proxy(),
|
||||
"dc=acegisecurity,dc=org"));
|
||||
PasswordComparisonAuthenticator authenticator =
|
||||
new PasswordComparisonAuthenticator(new MockInitialDirContextFactory(
|
||||
(DirContext)mockCtx.proxy(),
|
||||
"dc=acegisecurity,dc=org")
|
||||
);
|
||||
|
||||
authenticator.setUserDnPatterns(new String[] {"cn={0},ou=people"});
|
||||
|
||||
// Get the mock to return an empty attribute set
|
||||
mockCtx.expects(atLeastOnce()).method("getNameInNamespace").will(returnValue("dc=acegisecurity,dc=org"));
|
||||
mockCtx.expects(once()).method("getAttributes").with(eq("cn=Bob,ou=people"), NULL).will(returnValue(new BasicAttributes()));
|
||||
|
||||
+18
-20
@@ -1,15 +1,16 @@
|
||||
package org.acegisecurity.providers.ldap.authenticator;
|
||||
|
||||
import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory;
|
||||
import org.acegisecurity.providers.ldap.LdapUserDetails;
|
||||
import org.acegisecurity.providers.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.providers.encoding.PlaintextPasswordEncoder;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
||||
|
||||
import javax.naming.directory.BasicAttributes;
|
||||
|
||||
/**
|
||||
* Tests for {@link PasswordComparisonAuthenticator}.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
@@ -18,17 +19,13 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
||||
private PasswordComparisonAuthenticator authenticator;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
// Connection information
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory();
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
dirCtxFactory.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
|
||||
dirCtxFactory.setUrl(PROVIDER_URL);
|
||||
dirCtxFactory.setManagerDn(MANAGER_USER);
|
||||
dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
|
||||
dirCtxFactory.afterPropertiesSet();
|
||||
authenticator = new PasswordComparisonAuthenticator();
|
||||
authenticator.setInitialDirContextFactory(dirCtxFactory);
|
||||
authenticator.setUserDnPattern("uid={0},ou=people");
|
||||
authenticator = new PasswordComparisonAuthenticator(dirCtxFactory);
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
}
|
||||
|
||||
public void tearDown() {
|
||||
@@ -46,7 +43,7 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
||||
public void testLdapCompareSucceedsWithShaEncodedPassword() {
|
||||
authenticator = new PasswordComparisonAuthenticator();
|
||||
authenticator.setInitialDirContextFactory(dirCtxFactory);
|
||||
authenticator.setUserDnPattern("uid={0},ou=people");
|
||||
authenticator.setUserDnPatterns("uid={0},ou=people");
|
||||
// Don't retrieve the password
|
||||
authenticator.setUserAttributes(new String[] {"cn"});
|
||||
authenticator.authenticate("ben", "benspassword");
|
||||
@@ -76,9 +73,8 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
||||
}
|
||||
|
||||
public void testLocalCompareSucceedsWithShaEncodedPassword() {
|
||||
authenticator = new PasswordComparisonAuthenticator();
|
||||
authenticator.setInitialDirContextFactory(dirCtxFactory);
|
||||
authenticator.setUserDnPattern("uid={0},ou=people");
|
||||
authenticator = new PasswordComparisonAuthenticator(dirCtxFactory);
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
authenticator.authenticate("ben", "benspassword");
|
||||
}
|
||||
|
||||
@@ -91,7 +87,7 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
||||
}
|
||||
|
||||
public void testAllAttributesAreRetrivedByDefault() {
|
||||
LdapUserDetails user = authenticator.authenticate("Bob", "bobspassword");
|
||||
LdapUserInfo user = authenticator.authenticate("Bob", "bobspassword");
|
||||
System.out.println(user.getAttributes().toString());
|
||||
assertEquals("User should have 5 attributes", 5, user.getAttributes().size());
|
||||
|
||||
@@ -100,7 +96,7 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
||||
public void testOnlySpecifiedAttributesAreRetrieved() throws Exception {
|
||||
authenticator.setUserAttributes(new String[] {"cn", "uid"});
|
||||
authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
|
||||
LdapUserDetails user = authenticator.authenticate("Bob", "bobspassword");
|
||||
LdapUserInfo user = authenticator.authenticate("Bob", "bobspassword");
|
||||
assertEquals("Should have retrieved 2 attributes (cn, uid)",2, user.getAttributes().size());
|
||||
assertEquals("Bob Hamilton", user.getAttributes().get("cn").get());
|
||||
assertEquals("bob", user.getAttributes().get("uid").get());
|
||||
@@ -120,17 +116,19 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
||||
*/
|
||||
|
||||
public void testWithUserSearch() {
|
||||
LdapUserDetails user = new LdapUserDetails("uid=Bob,ou=people" + ROOT_DN,
|
||||
authenticator = new PasswordComparisonAuthenticator(dirCtxFactory);
|
||||
assertTrue("User DN matches shouldn't be available",
|
||||
authenticator.getUserDns("Bob").isEmpty());
|
||||
LdapUserInfo user = new LdapUserInfo("uid=Bob,ou=people" + ROOT_DN,
|
||||
new BasicAttributes("userPassword","bobspassword"));
|
||||
authenticator.setUserDnPattern(null);
|
||||
assertNull(authenticator.getUserDnPattern());
|
||||
assertNull(authenticator.getUserDn("Bob"));
|
||||
authenticator.setUserSearch(new MockUserSearch(user));
|
||||
authenticator.authenticate("ShouldntBeUsed","bobspassword");
|
||||
}
|
||||
|
||||
public void testFailedSearchGivesUserNotFoundException() throws Exception {
|
||||
authenticator.setUserDnPattern(null);
|
||||
authenticator = new PasswordComparisonAuthenticator(dirCtxFactory);
|
||||
assertTrue("User DN matches shouldn't be available",
|
||||
authenticator.getUserDns("Bob").isEmpty());
|
||||
authenticator.setUserSearch(new MockUserSearch(null));
|
||||
authenticator.afterPropertiesSet();
|
||||
|
||||
|
||||
+4
-19
@@ -17,31 +17,17 @@ import java.util.HashSet;
|
||||
*/
|
||||
public class DefaultLdapAuthoritiesPopulatorTests extends AbstractLdapServerTestCase {
|
||||
private DefaultInitialDirContextFactory dirCtxFactory;
|
||||
private DefaultLdapAuthoritiesPopulator populator;
|
||||
|
||||
public void setUp() {
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory();
|
||||
dirCtxFactory.setUrl(PROVIDER_URL);
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
dirCtxFactory.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
|
||||
dirCtxFactory.setManagerDn(MANAGER_USER);
|
||||
dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
|
||||
|
||||
populator = new DefaultLdapAuthoritiesPopulator();
|
||||
populator.setRolePrefix("ROLE_");
|
||||
}
|
||||
|
||||
public void testCtxFactoryMustBeSetIfSearchBaseIsSet() throws Exception {
|
||||
populator.setGroupSearchBase("");
|
||||
|
||||
try {
|
||||
populator.afterPropertiesSet();
|
||||
fail("expected exception.");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testUserAttributeMappingToRoles() {
|
||||
DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator();
|
||||
populator.setUserRoleAttributes(new String[] {"userRole", "otherUserRole"});
|
||||
populator.getUserRoleAttributes();
|
||||
|
||||
@@ -58,14 +44,13 @@ public class DefaultLdapAuthoritiesPopulatorTests extends AbstractLdapServerTest
|
||||
}
|
||||
|
||||
public void testGroupSearch() throws Exception {
|
||||
populator.setInitialDirContextFactory(dirCtxFactory);
|
||||
populator.setGroupSearchBase("ou=groups");
|
||||
DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator(dirCtxFactory, "ou=groups");
|
||||
populator.setRolePrefix("ROLE_");
|
||||
populator.setGroupRoleAttribute("ou");
|
||||
populator.setSearchSubtree(true);
|
||||
populator.setSearchSubtree(false);
|
||||
populator.setConvertToUpperCase(true);
|
||||
populator.setGroupSearchFilter("(member={0})");
|
||||
populator.afterPropertiesSet();
|
||||
|
||||
GrantedAuthority[] authorities = populator.getGrantedAuthorities("ben", "uid=ben,ou=people,"+ROOT_DN, new BasicAttributes());
|
||||
assertEquals("Should have 2 roles", 2, authorities.length);
|
||||
|
||||
Reference in New Issue
Block a user