Sort out LDAP tests to match up with moved production classes.
This commit is contained in:
parent
bf4fca9126
commit
267c846e12
|
@ -1,51 +0,0 @@
|
|||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.acegisecurity.providers.ldap;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.apache.ldap.server.jndi.CoreContextFactory;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
public abstract class AbstractLdapServerTestCase extends TestCase {
|
||||
protected static final String ROOT_DN = "dc=acegisecurity,dc=org";
|
||||
protected static final String MANAGER_USER = "cn=manager," + ROOT_DN;
|
||||
protected static final String MANAGER_PASSWORD = "acegisecurity";
|
||||
|
||||
// External server config
|
||||
// protected static final String PROVIDER_URL = "ldap://monkeymachine:389/"+ROOT_DN;
|
||||
// protected static final String CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
|
||||
// protected static final Hashtable EXTRA_ENV = new Hashtable();
|
||||
|
||||
|
||||
// Embedded (non-networked) server config
|
||||
private static final LdapTestServer SERVER = new LdapTestServer();
|
||||
protected static final String PROVIDER_URL = ROOT_DN;
|
||||
protected static final String CONTEXT_FACTORY = CoreContextFactory.class.getName();
|
||||
protected static final Hashtable EXTRA_ENV = SERVER.getConfiguration().toJndiEnvironment();
|
||||
|
||||
protected AbstractLdapServerTestCase() {
|
||||
}
|
||||
|
||||
protected AbstractLdapServerTestCase(String string) {
|
||||
super(string);
|
||||
}
|
||||
}
|
|
@ -1,162 +0,0 @@
|
|||
package org.acegisecurity.providers.ldap;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.directory.DirContext;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
|
||||
/**
|
||||
* Tests {@link DefaultInitialDirContextFactory}.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
public class DefaultInitialDirContextFactoryTests extends AbstractLdapServerTestCase {
|
||||
DefaultInitialDirContextFactory idf;
|
||||
|
||||
public void setUp() {
|
||||
idf = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
idf.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
idf.setExtraEnvVars(EXTRA_ENV);
|
||||
}
|
||||
|
||||
// public void testNonLdapUrlIsRejected() throws Exception {
|
||||
// DefaultInitialDirContextFactory idf = new DefaultInitialDirContextFactory();
|
||||
//
|
||||
// idf.setUrl("http://acegisecurity.org/dc=acegisecurity,dc=org");
|
||||
// idf.setInitialContextFactory(CoreContextFactory.class.getName());
|
||||
//
|
||||
// try {
|
||||
// idf.afterPropertiesSet();
|
||||
// fail("Expected exception for non 'ldap://' URL");
|
||||
// } catch(IllegalArgumentException expected) {
|
||||
// }
|
||||
// }
|
||||
|
||||
public void testServiceLocationUrlIsSupported() {
|
||||
idf = new DefaultInitialDirContextFactory("ldap:///dc=acegisecurity,dc=org");
|
||||
assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
|
||||
|
||||
}
|
||||
|
||||
public void testSecureLdapUrlIsSupported() {
|
||||
idf = new DefaultInitialDirContextFactory("ldaps://localhost/dc=acegisecurity,dc=org");
|
||||
assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
|
||||
}
|
||||
|
||||
public void testConnectionFailure() throws Exception {
|
||||
// Use the wrong port
|
||||
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);
|
||||
|
||||
try {
|
||||
idf.newInitialDirContext();
|
||||
fail("Connection succeeded unexpectedly");
|
||||
} catch(LdapDataAccessException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testAnonymousBindSucceeds() throws Exception {
|
||||
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
|
||||
// assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
public void testBindAsManagerSucceeds() throws Exception {
|
||||
idf.setManagerPassword(MANAGER_PASSWORD);
|
||||
idf.setManagerDn(MANAGER_USER);
|
||||
|
||||
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"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
public void testBindAsManagerFailsIfNoPasswordSet() throws Exception {
|
||||
idf.setManagerDn(MANAGER_USER);
|
||||
|
||||
DirContext ctx = null;
|
||||
|
||||
try {
|
||||
ctx = idf.newInitialDirContext();
|
||||
fail("Binding with no manager password should fail.");
|
||||
// Can't rely on this property being there with embedded server
|
||||
// assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
|
||||
} catch(BadCredentialsException expected) {
|
||||
}
|
||||
|
||||
LdapUtils.closeContext(ctx);
|
||||
}
|
||||
|
||||
public void testInvalidPasswordCausesBadCredentialsException() throws Exception {
|
||||
idf.setManagerDn(MANAGER_USER);
|
||||
idf.setManagerPassword("wrongpassword");
|
||||
|
||||
DirContext ctx = null;
|
||||
try {
|
||||
ctx = idf.newInitialDirContext();
|
||||
fail("Binding with wrong credentials should fail.");
|
||||
} catch(BadCredentialsException expected) {
|
||||
}
|
||||
|
||||
LdapUtils.closeContext(ctx);
|
||||
}
|
||||
|
||||
public void testConnectionAsSpecificUserSucceeds() throws Exception {
|
||||
DirContext ctx = idf.newInitialDirContext("uid=Bob,ou=people,dc=acegisecurity,dc=org",
|
||||
"bobspassword");
|
||||
// We don't want pooling for specific users.
|
||||
// assertNull(ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
|
||||
// com.sun.jndi.ldap.LdapPoolManager.showStats(System.out);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
public void testEnvironment() {
|
||||
idf = new DefaultInitialDirContextFactory("ldap://acegisecurity.org/");
|
||||
|
||||
// check basic env
|
||||
Hashtable env = idf.getEnvironment();
|
||||
//assertEquals("com.sun.jndi.ldap.LdapCtxFactory", env.get(Context.INITIAL_CONTEXT_FACTORY));
|
||||
assertEquals("ldap://acegisecurity.org/", env.get(Context.PROVIDER_URL));
|
||||
assertEquals("simple",env.get(Context.SECURITY_AUTHENTICATION));
|
||||
assertNull(env.get(Context.SECURITY_PRINCIPAL));
|
||||
assertNull(env.get(Context.SECURITY_CREDENTIALS));
|
||||
|
||||
// Ctx factory.
|
||||
idf.setInitialContextFactory("org.acegisecurity.NonExistentCtxFactory");
|
||||
env = idf.getEnvironment();
|
||||
assertEquals("org.acegisecurity.NonExistentCtxFactory", env.get(Context.INITIAL_CONTEXT_FACTORY));
|
||||
|
||||
// Auth type
|
||||
idf.setAuthenticationType("myauthtype");
|
||||
env = idf.getEnvironment();
|
||||
assertEquals("myauthtype", env.get(Context.SECURITY_AUTHENTICATION));
|
||||
|
||||
// Check extra vars
|
||||
Hashtable extraVars = new Hashtable();
|
||||
extraVars.put("extravar", "extravarvalue");
|
||||
idf.setExtraEnvVars(extraVars);
|
||||
env = idf.getEnvironment();
|
||||
assertEquals("extravarvalue", env.get("extravar"));
|
||||
}
|
||||
|
||||
public void testBaseDnIsParsedFromCorrectlyFromUrl() throws Exception {
|
||||
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("ldap://acegisecurity.org/");
|
||||
assertEquals("", idf.getRootDn());
|
||||
|
||||
// Empty root without trailing slash
|
||||
idf = new DefaultInitialDirContextFactory("ldap://acegisecurity.org");
|
||||
assertEquals("", idf.getRootDn());
|
||||
}
|
||||
|
||||
}
|
|
@ -6,6 +6,9 @@ import javax.naming.directory.BasicAttributes;
|
|||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
import org.acegisecurity.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.ldap.DefaultInitialDirContextFactory;
|
||||
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
import org.acegisecurity.userdetails.UserDetails;
|
||||
|
||||
|
|
|
@ -1,229 +0,0 @@
|
|||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.acegisecurity.providers.ldap;
|
||||
|
||||
import org.apache.ldap.server.configuration.MutableDirectoryPartitionConfiguration;
|
||||
import org.apache.ldap.server.configuration.MutableStartupConfiguration;
|
||||
import org.apache.ldap.server.configuration.Configuration;
|
||||
import org.apache.ldap.server.jndi.CoreContextFactory;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.NameAlreadyBoundException;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.BasicAttributes;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.BasicAttribute;
|
||||
import javax.naming.directory.DirContext;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* An embedded LDAP test server, complete with test data for running the
|
||||
* unit tests against.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
public class LdapTestServer {
|
||||
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private DirContext serverContext;
|
||||
|
||||
private MutableStartupConfiguration cfg;
|
||||
|
||||
// Move the working dir to the temp directory
|
||||
private File workingDir = new File( System.getProperty("java.io.tmpdir")
|
||||
+ File.separator + "apacheds-work" );
|
||||
|
||||
|
||||
//~ Constructors ================================================================
|
||||
|
||||
/**
|
||||
* Starts up and configures ApacheDS.
|
||||
*/
|
||||
public LdapTestServer() {
|
||||
startLdapServer();
|
||||
createManagerUser();
|
||||
initTestData();
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
private void startLdapServer() {
|
||||
|
||||
cfg = new MutableStartupConfiguration();
|
||||
((MutableStartupConfiguration)cfg).setWorkingDirectory(workingDir);
|
||||
|
||||
System.out.println("Working directory is " + workingDir.getAbsolutePath());
|
||||
|
||||
initConfiguration();
|
||||
|
||||
Properties env = new Properties();
|
||||
|
||||
env.setProperty( Context.PROVIDER_URL, "dc=acegisecurity,dc=org" );
|
||||
env.setProperty( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.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");
|
||||
createUser("bob","Bob Hamilton", "bobspassword");
|
||||
createUser("ben","Ben Alex", "{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
|
||||
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]});
|
||||
}
|
||||
|
||||
private void createManagerUser() {
|
||||
Attributes user = new BasicAttributes( "cn", "manager" , true );
|
||||
user.put( "userPassword", "acegisecurity" );
|
||||
Attribute objectClass = new BasicAttribute("objectClass");
|
||||
user.put( objectClass );
|
||||
objectClass.add( "top" );
|
||||
objectClass.add( "person" );
|
||||
objectClass.add( "organizationalPerson" );
|
||||
objectClass.add( "inetOrgPerson" );
|
||||
user.put( "sn", "Manager" );
|
||||
user.put( "cn", "manager" );
|
||||
try {
|
||||
serverContext.createSubcontext("cn=manager", user );
|
||||
} catch(NameAlreadyBoundException ignore) {
|
||||
// System.out.println("Manager user already exists.");
|
||||
} catch (NamingException ne) {
|
||||
System.err.println("Failed to create manager user.");
|
||||
ne.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void createUser( String uid, String cn, String password ) {
|
||||
Attributes user = new BasicAttributes("uid", uid);
|
||||
user.put( "cn", cn);
|
||||
user.put( "userPassword", LdapUtils.getUtf8Bytes(password) );
|
||||
Attribute objectClass = new BasicAttribute( "objectClass" );
|
||||
user.put( objectClass );
|
||||
objectClass.add( "top" );
|
||||
objectClass.add( "person" );
|
||||
objectClass.add( "organizationalPerson" );
|
||||
objectClass.add( "inetOrgPerson" );
|
||||
user.put( "sn", uid );
|
||||
|
||||
try {
|
||||
serverContext.createSubcontext( "uid="+uid+",ou=people", user );
|
||||
} catch(NameAlreadyBoundException ignore) {
|
||||
// System.out.println(" user " + uid + " already exists.");
|
||||
} catch (NamingException ne) {
|
||||
System.err.println("Failed to create user.");
|
||||
ne.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void createOu(String name) {
|
||||
Attributes ou = new BasicAttributes( "ou", name );
|
||||
Attribute objectClass = new BasicAttribute( "objectClass" );
|
||||
objectClass.add("top");
|
||||
objectClass.add("organizationalUnit");
|
||||
ou.put(objectClass);
|
||||
|
||||
try {
|
||||
serverContext.createSubcontext( "ou="+name, ou);
|
||||
} catch(NameAlreadyBoundException ignore) {
|
||||
// System.out.println(" ou " + name + " already exists.");
|
||||
} catch (NamingException ne) {
|
||||
System.err.println("Failed to create ou.");
|
||||
ne.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void createGroup( String cn, String ou, String[] memberDns ) {
|
||||
Attributes group = new BasicAttributes("cn", cn);
|
||||
Attribute members = new BasicAttribute("member");
|
||||
Attribute orgUnit = new BasicAttribute("ou", ou);
|
||||
|
||||
for(int i=0; i < memberDns.length; i++) {
|
||||
members.add(memberDns[i]);
|
||||
}
|
||||
|
||||
Attribute objectClass = new BasicAttribute( "objectClass" );
|
||||
objectClass.add( "top" );
|
||||
objectClass.add( "groupOfNames" );
|
||||
|
||||
group.put(objectClass);
|
||||
group.put(members);
|
||||
group.put(orgUnit);
|
||||
|
||||
try {
|
||||
serverContext.createSubcontext( "cn="+cn+",ou=groups", group );
|
||||
} catch(NameAlreadyBoundException ignore) {
|
||||
// System.out.println(" group " + cn + " already exists.");
|
||||
} catch (NamingException ne) {
|
||||
System.err.println("Failed to create group.");
|
||||
ne.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void initConfiguration() {
|
||||
|
||||
// Create the partition for the acegi tests
|
||||
MutableDirectoryPartitionConfiguration acegiDit = new MutableDirectoryPartitionConfiguration();
|
||||
acegiDit.setName("acegisecurity");
|
||||
acegiDit.setSuffix("dc=acegisecurity,dc=org");
|
||||
BasicAttributes attributes = new BasicAttributes();
|
||||
BasicAttribute objectClass = new BasicAttribute("objectClass");
|
||||
objectClass.add("top");
|
||||
objectClass.add("domain");
|
||||
objectClass.add("extensibleObject");
|
||||
attributes.put(objectClass);
|
||||
acegiDit.setContextEntry(attributes);
|
||||
|
||||
Set indexedAttrs = new HashSet();
|
||||
indexedAttrs.add("objectClass");
|
||||
indexedAttrs.add("uid");
|
||||
indexedAttrs.add("cn");
|
||||
indexedAttrs.add("ou");
|
||||
indexedAttrs.add("member");
|
||||
|
||||
acegiDit.setIndexedAttributes(indexedAttrs);
|
||||
|
||||
Set partitions = new HashSet();
|
||||
partitions.add(acegiDit);
|
||||
|
||||
cfg.setContextPartitionConfigurations(partitions);
|
||||
}
|
||||
|
||||
public Configuration getConfiguration() {
|
||||
return cfg;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
LdapTestServer server = new LdapTestServer();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,7 @@
|
|||
package org.acegisecurity.providers.ldap.authenticator;
|
||||
|
||||
import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory;
|
||||
import org.acegisecurity.providers.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
|
||||
/**
|
||||
|
@ -13,19 +12,16 @@ import org.acegisecurity.BadCredentialsException;
|
|||
*/
|
||||
public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
|
||||
|
||||
private DefaultInitialDirContextFactory dirCtxFactory;
|
||||
private BindAuthenticator authenticator;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
dirCtxFactory.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
|
||||
authenticator = new BindAuthenticator(dirCtxFactory);
|
||||
public void onSetUp() {
|
||||
authenticator = new BindAuthenticator(getInitialCtxFactory());
|
||||
}
|
||||
|
||||
public void testUserDnPatternReturnsCorrectDn() throws Exception {
|
||||
authenticator.setUserDnPatterns(new String[] {"cn={0},ou=people"});
|
||||
assertEquals("cn=Joe,ou=people,"+ ROOT_DN, authenticator.getUserDns("Joe").get(0));
|
||||
assertEquals("cn=Joe,ou=people,"+ getInitialCtxFactory().getRootDn(),
|
||||
authenticator.getUserDns("Joe").get(0));
|
||||
}
|
||||
|
||||
public void testAuthenticationWithCorrectPasswordSucceeds() throws Exception {
|
||||
|
@ -34,7 +30,7 @@ public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
|
|||
}
|
||||
|
||||
public void testAuthenticationWithWrongPasswordFails() {
|
||||
BindAuthenticator authenticator = new BindAuthenticator(dirCtxFactory);
|
||||
// BindAuthenticator authenticator = new BindAuthenticator(dirCtxFactory);
|
||||
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
|
||||
|
@ -46,7 +42,7 @@ public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
|
|||
}
|
||||
|
||||
public void testAuthenticationWithUserSearch() throws Exception {
|
||||
LdapUserInfo user = new LdapUserInfo("uid=bob,ou=people," + ROOT_DN, null);
|
||||
LdapUserInfo user = new LdapUserInfo("uid=bob,ou=people," + getInitialCtxFactory().getRootDn(), null);
|
||||
authenticator.setUserSearch(new MockUserSearch(user));
|
||||
authenticator.afterPropertiesSet();
|
||||
authenticator.authenticate("bob","bobspassword");
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package org.acegisecurity.providers.ldap.authenticator;
|
||||
|
||||
import org.acegisecurity.providers.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.providers.ldap.LdapUserSearch;
|
||||
import org.acegisecurity.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.ldap.LdapUserSearch;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.acegisecurity.providers.ldap.authenticator;
|
|||
|
||||
import org.jmock.Mock;
|
||||
import org.jmock.MockObjectTestCase;
|
||||
import org.acegisecurity.providers.ldap.InitialDirContextFactory;
|
||||
import org.acegisecurity.ldap.InitialDirContextFactory;
|
||||
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.directory.BasicAttributes;
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
package org.acegisecurity.providers.ldap.authenticator;
|
||||
|
||||
import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory;
|
||||
import org.acegisecurity.providers.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
||||
|
||||
|
@ -15,16 +14,12 @@ import javax.naming.directory.BasicAttributes;
|
|||
* @version $Id$
|
||||
*/
|
||||
public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTestCase {
|
||||
private DefaultInitialDirContextFactory dirCtxFactory;
|
||||
private PasswordComparisonAuthenticator authenticator;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
dirCtxFactory.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
|
||||
dirCtxFactory.setManagerDn(MANAGER_USER);
|
||||
dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
|
||||
authenticator = new PasswordComparisonAuthenticator(dirCtxFactory);
|
||||
public void onSetUp() {
|
||||
getInitialCtxFactory().setManagerDn(MANAGER_USER);
|
||||
getInitialCtxFactory().setManagerPassword(MANAGER_PASSWORD);
|
||||
authenticator = new PasswordComparisonAuthenticator(getInitialCtxFactory());
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
}
|
||||
|
||||
|
@ -73,7 +68,7 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
|||
}
|
||||
|
||||
public void testLocalCompareSucceedsWithShaEncodedPassword() {
|
||||
authenticator = new PasswordComparisonAuthenticator(dirCtxFactory);
|
||||
authenticator = new PasswordComparisonAuthenticator(getInitialCtxFactory());
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
authenticator.authenticate("ben", "benspassword");
|
||||
}
|
||||
|
@ -116,17 +111,18 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
|||
*/
|
||||
|
||||
public void testWithUserSearch() {
|
||||
authenticator = new PasswordComparisonAuthenticator(dirCtxFactory);
|
||||
authenticator = new PasswordComparisonAuthenticator(getInitialCtxFactory());
|
||||
assertTrue("User DN matches shouldn't be available",
|
||||
authenticator.getUserDns("Bob").isEmpty());
|
||||
LdapUserInfo user = new LdapUserInfo("uid=Bob,ou=people" + ROOT_DN,
|
||||
LdapUserInfo user = new LdapUserInfo("uid=Bob,ou=people" +
|
||||
getInitialCtxFactory().getRootDn(),
|
||||
new BasicAttributes("userPassword","bobspassword"));
|
||||
authenticator.setUserSearch(new MockUserSearch(user));
|
||||
authenticator.authenticate("ShouldntBeUsed","bobspassword");
|
||||
}
|
||||
|
||||
public void testFailedSearchGivesUserNotFoundException() throws Exception {
|
||||
authenticator = new PasswordComparisonAuthenticator(dirCtxFactory);
|
||||
authenticator = new PasswordComparisonAuthenticator(getInitialCtxFactory());
|
||||
assertTrue("User DN matches shouldn't be available",
|
||||
authenticator.getUserDns("Bob").isEmpty());
|
||||
authenticator.setUserSearch(new MockUserSearch(null));
|
||||
|
|
|
@ -5,8 +5,7 @@ import javax.naming.directory.BasicAttributes;
|
|||
import javax.naming.directory.BasicAttribute;
|
||||
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory;
|
||||
import org.acegisecurity.ldap.AbstractLdapServerTestCase;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
|
@ -16,14 +15,10 @@ import java.util.HashSet;
|
|||
* @version $Id$
|
||||
*/
|
||||
public class DefaultLdapAuthoritiesPopulatorTests extends AbstractLdapServerTestCase {
|
||||
private DefaultInitialDirContextFactory dirCtxFactory;
|
||||
|
||||
public void setUp() {
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
dirCtxFactory.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
|
||||
dirCtxFactory.setManagerDn(MANAGER_USER);
|
||||
dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
|
||||
public void onSetUp() {
|
||||
getInitialCtxFactory().setManagerDn(MANAGER_USER);
|
||||
getInitialCtxFactory().setManagerPassword(MANAGER_PASSWORD);
|
||||
}
|
||||
|
||||
public void testUserAttributeMappingToRoles() {
|
||||
|
@ -39,7 +34,8 @@ public class DefaultLdapAuthoritiesPopulatorTests extends AbstractLdapServerTest
|
|||
attr.add("role2"); // duplicate
|
||||
userAttrs.put(attr);
|
||||
|
||||
GrantedAuthority[] authorities = populator.getGrantedAuthorities("Ignored", "Ignored", userAttrs);
|
||||
GrantedAuthority[] authorities =
|
||||
populator.getGrantedAuthorities("Ignored", "Ignored", userAttrs);
|
||||
assertEquals("User should have three roles", 3, authorities.length);
|
||||
}
|
||||
|
||||
|
@ -47,13 +43,15 @@ public class DefaultLdapAuthoritiesPopulatorTests extends AbstractLdapServerTest
|
|||
DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator();
|
||||
populator.setDefaultRole("ROLE_USER");
|
||||
|
||||
GrantedAuthority[] authorities = populator.getGrantedAuthorities("Ignored", "Ignored", new BasicAttributes());
|
||||
GrantedAuthority[] authorities =
|
||||
populator.getGrantedAuthorities("Ignored", "Ignored", new BasicAttributes());
|
||||
assertEquals(1, authorities.length);
|
||||
assertEquals("ROLE_USER", authorities[0].getAuthority());
|
||||
}
|
||||
|
||||
public void testGroupSearch() throws Exception {
|
||||
DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator(dirCtxFactory, "ou=groups");
|
||||
DefaultLdapAuthoritiesPopulator populator =
|
||||
new DefaultLdapAuthoritiesPopulator(getInitialCtxFactory(), "ou=groups");
|
||||
populator.setRolePrefix("ROLE_");
|
||||
populator.setGroupRoleAttribute("ou");
|
||||
populator.setSearchSubtree(true);
|
||||
|
@ -61,7 +59,9 @@ public class DefaultLdapAuthoritiesPopulatorTests extends AbstractLdapServerTest
|
|||
populator.setConvertToUpperCase(true);
|
||||
populator.setGroupSearchFilter("(member={0})");
|
||||
|
||||
GrantedAuthority[] authorities = populator.getGrantedAuthorities("ben", "uid=ben,ou=people,"+ROOT_DN, new BasicAttributes());
|
||||
GrantedAuthority[] authorities =
|
||||
populator.getGrantedAuthorities("ben", "uid=ben,ou=people,"+
|
||||
getInitialCtxFactory().getRootDn(), new BasicAttributes());
|
||||
assertEquals("Should have 2 roles", 2, authorities.length);
|
||||
Set roles = new HashSet();
|
||||
roles.add(authorities[0].toString());
|
||||
|
|
|
@ -1,90 +0,0 @@
|
|||
package org.acegisecurity.providers.ldap.search;
|
||||
|
||||
import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory;
|
||||
import org.acegisecurity.providers.ldap.LdapUserInfo;
|
||||
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
|
||||
/**
|
||||
* Tests for FilterBasedLdapUserSearch.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
|
||||
private DefaultInitialDirContextFactory dirCtxFactory;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
|
||||
dirCtxFactory.setInitialContextFactory(CONTEXT_FACTORY);
|
||||
dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
|
||||
dirCtxFactory.setManagerDn(MANAGER_USER);
|
||||
dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
|
||||
}
|
||||
|
||||
public FilterBasedLdapUserSearchTests(String string) {
|
||||
super(string);
|
||||
}
|
||||
|
||||
public FilterBasedLdapUserSearchTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void testBasicSearch() throws Exception {
|
||||
FilterBasedLdapUserSearch locator =
|
||||
new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory);
|
||||
LdapUserInfo bob = locator.searchForUser("bob");
|
||||
locator.setSearchSubtree(false);
|
||||
locator.setSearchTimeLimit(0);
|
||||
// name is wrong with embedded apacheDS
|
||||
// assertEquals("uid=bob,ou=people,"+ROOT_DN, bob.getDn());
|
||||
}
|
||||
|
||||
public void testSubTreeSearchSucceeds() throws Exception {
|
||||
// Don't set the searchBase, so search from the root.
|
||||
FilterBasedLdapUserSearch locator =
|
||||
new FilterBasedLdapUserSearch("", "(cn={0})", dirCtxFactory);
|
||||
locator.setSearchSubtree(true);
|
||||
|
||||
LdapUserInfo ben = locator.searchForUser("Ben Alex");
|
||||
// assertEquals("uid=ben,ou=people,"+ROOT_DN, bob.getDn());
|
||||
}
|
||||
|
||||
public void testSearchForInvalidUserFails() {
|
||||
FilterBasedLdapUserSearch locator =
|
||||
new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory);
|
||||
|
||||
try {
|
||||
locator.searchForUser("Joe");
|
||||
fail("Expected UsernameNotFoundException for non-existent user.");
|
||||
} catch (UsernameNotFoundException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testFailsOnMultipleMatches() {
|
||||
FilterBasedLdapUserSearch locator =
|
||||
new FilterBasedLdapUserSearch("ou=people", "(cn=*)", dirCtxFactory);
|
||||
|
||||
try {
|
||||
locator.searchForUser("Ignored");
|
||||
fail("Expected exception for multiple search matches.");
|
||||
} catch (BadCredentialsException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
// Try some funny business with filters.
|
||||
|
||||
public void testExtraFilterPartToExcludeBob() throws Exception {
|
||||
FilterBasedLdapUserSearch locator =
|
||||
new FilterBasedLdapUserSearch("ou=people",
|
||||
"(&(cn=*)(!(|(uid={0})(uid=marissa))))",
|
||||
dirCtxFactory);
|
||||
|
||||
// Search for bob, get back ben...
|
||||
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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue