SEC-449: Make LdapUserDetailsMapper a pure ContextMapper so it can be used with LdapTemplate.

This commit is contained in:
Luke Taylor
2007-09-13 20:42:50 +00:00
parent 6d8f92e1b8
commit d208cf3824
8 changed files with 62 additions and 58 deletions

View File

@@ -92,7 +92,7 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests
locator.setSearchSubtree(true);
LdapUserDetails ben = locator.searchForUser("Ben Alex");
assertEquals("Ben Alex", ben.getUsername());
assertEquals("ben", ben.getUsername());
// assertEquals("uid=ben,ou=people,dc=acegisecurity,dc=org", ben.getDn());
}

View File

@@ -23,6 +23,7 @@ import org.jmock.MockObjectTestCase;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.BasicAttribute;
/**
@@ -33,9 +34,10 @@ import javax.naming.directory.DirContext;
public class PasswordComparisonAuthenticatorMockTests extends MockObjectTestCase {
//~ Methods ========================================================================================================
public void testLdapCompareIsUsedWhenPasswordIsNotRetrieved()
throws Exception {
public void testLdapCompareIsUsedWhenPasswordIsNotRetrieved() throws Exception {
Mock mockCtx = mock(DirContext.class);
BasicAttributes attrs = new BasicAttributes();
attrs.put(new BasicAttribute("uid", "bob"));
PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator(new MockInitialDirContextFactory(
(DirContext) mockCtx.proxy(), "dc=acegisecurity,dc=org"));
@@ -46,7 +48,7 @@ public class PasswordComparisonAuthenticatorMockTests extends MockObjectTestCase
mockCtx.expects(atLeastOnce()).method("getNameInNamespace").will(returnValue("dc=acegisecurity,dc=org"));
mockCtx.expects(once()).method("lookup").with(eq("cn=Bob, ou=people")).will(returnValue(true));
mockCtx.expects(once()).method("getAttributes").with(eq("cn=Bob, ou=people"), NULL)
.will(returnValue(new BasicAttributes()));
.will(returnValue(attrs));
// Setup a single return value (i.e. success)
Attributes searchResults = new BasicAttributes("", null);

View File

@@ -86,7 +86,7 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapIntegratio
public void testLdapPasswordCompareFailsWithWrongPassword() {
// Don't retrieve the password
authenticator.setUserAttributes(new String[] {"cn", "sn"});
authenticator.setUserAttributes(new String[] {"uid", "cn", "sn"});
try {
authenticator.authenticate("Bob", "wrongpassword");
fail("Authentication should fail with wrong password.");
@@ -95,9 +95,9 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapIntegratio
}
public void testLocalPasswordComparisonSucceedsWithCorrectPassword() {
LdapUserDetails user = authenticator.authenticate("Bob", "bobspassword");
LdapUserDetails user = authenticator.authenticate("bob", "bobspassword");
// check username is retrieved.
assertEquals("Bob", user.getUsername());
assertEquals("bob", user.getUsername());
assertEquals("bobspassword", user.getPassword());
}
@@ -107,16 +107,16 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapIntegratio
}
public void testOnlySpecifiedAttributesAreRetrieved() throws Exception {
authenticator.setUserAttributes(new String[] {"userPassword"});
authenticator.setUserAttributes(new String[] {"uid", "userPassword"});
authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
LdapUserDetails user = authenticator.authenticate("Bob", "bobspassword");
assertEquals("Should have retrieved 1 attribute (userPassword)", 1, user.getAttributes().size());
assertEquals("Should have retrieved 2 attribute (uid, userPassword)", 2, user.getAttributes().size());
}
public void testLdapCompareSucceedsWithCorrectPassword() {
// Don't retrieve the password
authenticator.setUserAttributes(new String[] {"cn"});
authenticator.setUserAttributes(new String[] {"uid"});
// Bob has a plaintext password.
authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
authenticator.authenticate("bob", "bobspassword");
@@ -124,7 +124,7 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapIntegratio
public void testLdapCompareSucceedsWithShaEncodedPassword() {
// Don't retrieve the password
authenticator.setUserAttributes(new String[] {"cn"});
authenticator.setUserAttributes(new String[] {"uid"});
authenticator.authenticate("ben", "benspassword");
}
@@ -145,10 +145,10 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapIntegratio
}
public void testLdapCompareWithDifferentPasswordAttributeSucceeds() {
authenticator.setUserAttributes(new String[] {"cn"});
authenticator.setUserAttributes(new String[] {"uid"});
authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
authenticator.setPasswordAttributeName("uid");
authenticator.authenticate("bob", "bob");
authenticator.setPasswordAttributeName("cn");
authenticator.authenticate("bob", "Bob Hamilton");
}
public void testWithUserSearch() {

View File

@@ -42,10 +42,11 @@ public class LdapUserDetailsMapperTests extends TestCase {
DirContextAdapter ctx = new DirContextAdapter();
ctx.setAttributeValues("userRole", new String[] {"X", "Y", "Z"});
ctx.setAttributeValue("uid", "ani");
LdapUserDetailsImpl.Essence user = (LdapUserDetailsImpl.Essence) mapper.mapFromContext(ctx);
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapFromContext(ctx);
assertEquals(3, user.getGrantedAuthorities().length);
assertEquals(3, user.getAuthorities().length);
}
/**
@@ -60,11 +61,12 @@ public class LdapUserDetailsMapperTests extends TestCase {
attrs.put(new BasicAttribute("userRole", "x"));
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
ctx.setAttributeValue("uid", "ani");
LdapUserDetailsImpl.Essence user = (LdapUserDetailsImpl.Essence) mapper.mapFromContext(ctx);
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapFromContext(ctx);
assertEquals(1, user.getGrantedAuthorities().length);
assertEquals("ROLE_X", user.getGrantedAuthorities()[0].getAuthority());
assertEquals(1, user.getAuthorities().length);
assertEquals("ROLE_X", user.getAuthorities()[0].getAuthority());
}
// public void testNonStringRoleAttributeIsIgnoredByDefault() throws Exception {
@@ -90,9 +92,9 @@ public class LdapUserDetailsMapperTests extends TestCase {
attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes()));
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
ctx.setAttributeValue("uid", "ani");
LdapUserDetails user =
((LdapUserDetailsImpl.Essence) mapper.mapFromContext(ctx)).createUserDetails();
LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapFromContext(ctx);
assertEquals("mypassword", user.getPassword());
}