12 changed files with 197 additions and 105 deletions
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
#Mon May 09 00:49:50 GMT 2005 |
||||
eclipse.preferences.version=1 |
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled |
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 |
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve |
||||
org.eclipse.jdt.core.compiler.compliance=1.5 |
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate |
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate |
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate |
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error |
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error |
||||
org.eclipse.jdt.core.compiler.source=1.5 |
||||
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
# $Id$ |
||||
|
||||
# Values in this file will be overriden by any values with the same name |
||||
# in the user-created build.properties file. |
||||
|
||||
# Compile settings |
||||
# |
||||
# Java 1.5 is required because we use enums extensively in this subproject |
||||
# (main Acegi Security project / parent) is Java 1.3 compatible |
||||
# |
||||
maven.compile.target=1.5 |
||||
maven.compile.source=1.5 |
||||
@ -1,49 +1,76 @@
@@ -1,49 +1,76 @@
|
||||
/* 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 net.sf.acegisecurity.providers.dao.ldap; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import junit.framework.TestCase; |
||||
|
||||
import java.util.Hashtable; |
||||
|
||||
import javax.naming.Context; |
||||
import javax.naming.NamingException; |
||||
import javax.naming.directory.DirContext; |
||||
import javax.naming.directory.InitialDirContext; |
||||
import junit.framework.TestCase; |
||||
|
||||
/** Important note: this class merely defines certain |
||||
* base properties needed by all LDAP unit tests. |
||||
|
||||
/** |
||||
* Important note: this class merely defines certain base properties needed by |
||||
* all LDAP unit tests. |
||||
*/ |
||||
public class BaseLdapTestCase extends TestCase { |
||||
//~ Static fields/initializers =============================================
|
||||
|
||||
// static finalizers, they'd be nice, as LdapTestHelper
|
||||
// never seems to get the chance to cleanup after itself
|
||||
protected static LdapTestHelper ldapTestHelper = new LdapTestHelper(); |
||||
|
||||
protected static LdapTestHelper ldapTestHelper = new LdapTestHelper(); |
||||
|
||||
static { |
||||
//InputStream in = BaseLdapTestCase.class.getResourceAsStream("net/sf/acegisecurity/providers/dao/ldap/test-data.ldif");
|
||||
|
||||
/* InputStream in = ldapTestHelper.getClass().getResourceAsStream("test-data.ldif"); |
||||
try { |
||||
ldapTestHelper.importLDIF(in); |
||||
} catch (Exception x) { |
||||
x.printStackTrace(); |
||||
ldapTestHelper.shutdownServer(); |
||||
ldapTestHelper = null; |
||||
throw new RuntimeException("Server initialization failed."); |
||||
} */ |
||||
DirContentsInitializer.initialize( ldapTestHelper.getServerContext() ); |
||||
try { |
||||
ldapTestHelper.importLDIF(in); |
||||
} catch (Exception x) { |
||||
x.printStackTrace(); |
||||
ldapTestHelper.shutdownServer(); |
||||
ldapTestHelper = null; |
||||
throw new RuntimeException("Server initialization failed."); |
||||
} */ |
||||
DirContentsInitializer.initialize(ldapTestHelper.getServerContext()); |
||||
} |
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
protected DirContext getClientContext() throws NamingException { |
||||
Hashtable env = new Hashtable(); |
||||
env.put(Context.PROVIDER_URL, "ldap://localhost:389/ou=system"); |
||||
env.put(Context.INITIAL_CONTEXT_FACTORY, |
||||
"com.sun.jndi.ldap.LdapCtxFactory"); |
||||
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"); |
||||
env.put(Context.SECURITY_CREDENTIALS, "secret"); |
||||
|
||||
return new InitialDirContext(env); |
||||
} |
||||
|
||||
/** |
||||
* DOCUMENT ME! |
||||
* |
||||
* @return The server context for LDAP ops. used for things like |
||||
* addding/removing users. |
||||
*/ |
||||
protected DirContext getServerContext() { |
||||
return ldapTestHelper.getServerContext(); |
||||
} |
||||
|
||||
protected DirContext getClientContext() throws NamingException { |
||||
Hashtable env = new Hashtable(); |
||||
env.put( Context.PROVIDER_URL, "ldap://localhost:389/ou=system" ); |
||||
env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" ); |
||||
env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" ); |
||||
env.put( Context.SECURITY_CREDENTIALS, "secret" ); |
||||
return new InitialDirContext( env ); |
||||
} |
||||
|
||||
/** @return The server context for LDAP ops. used for things like addding/removing users. */ |
||||
protected DirContext getServerContext() { |
||||
return ldapTestHelper.getServerContext(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -1,93 +1,132 @@
@@ -1,93 +1,132 @@
|
||||
package net.sf.acegisecurity.providers.dao.ldap; |
||||
/* 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. |
||||
*/ |
||||
|
||||
import javax.naming.NamingException; |
||||
package net.sf.acegisecurity.providers.dao.ldap; |
||||
|
||||
import net.sf.acegisecurity.BadCredentialsException; |
||||
import net.sf.acegisecurity.UserDetails; |
||||
import net.sf.acegisecurity.GrantedAuthority; |
||||
|
||||
import javax.naming.NamingException; |
||||
|
||||
|
||||
/** |
||||
* DOCUMENT ME! |
||||
* |
||||
* @author $author$ |
||||
* @version $Revision$ |
||||
*/ |
||||
public class LdapPasswordAuthenticationTest extends BaseLdapTestCase { |
||||
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private LdapPasswordAuthenticationDao dao; |
||||
|
||||
private String DEFAULT_ROLE = "DEFAULT_ROLE"; |
||||
|
||||
/** Setup the basic properties of our LdapPasswordAuthenticationDao */ |
||||
protected void setUp() { |
||||
dao = new LdapPasswordAuthenticationDao(); |
||||
dao.setURL("ldap://localhost:389/ou=system"); |
||||
} |
||||
|
||||
|
||||
public void testSimpleUidUser() throws NamingException { |
||||
//~ Methods ================================================================
|
||||
|
||||
public void testEmptyRoles() { |
||||
dao.setUserContext("uid={0},ou=users,ou=system"); |
||||
dao.setDefaultRole(DEFAULT_ROLE); |
||||
|
||||
try { |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", "plaintext"); |
||||
assertEquals(1, userDetails.getAuthorities().length ); |
||||
assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() ); |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("user.two", |
||||
"plaintext2"); |
||||
fail("No roles are accessible for user; this test _should_ fail."); |
||||
} catch (BadCredentialsException ex) { |
||||
fail(); |
||||
assertTrue("No roles are accessible for user; this test _should_ fail.", |
||||
ex.getMessage().startsWith(LdapPasswordAuthenticationDao.BAD_CREDENTIALS_EXCEPTION_MESSAGE)); |
||||
} |
||||
} |
||||
|
||||
public void testSimpleUidUserBadPassword() throws NamingException { |
||||
dao.setUserContext("uid={0},ou=users,ou=system"); |
||||
|
||||
public void testSimpleCnUser() throws NamingException { |
||||
dao.setUserContext("cn={0},ou=users,ou=system"); |
||||
dao.setDefaultRole(DEFAULT_ROLE); |
||||
|
||||
try { |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", "plainlywrong"); |
||||
//assertEquals(1, userDetails.getAuthorities().length );
|
||||
//assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() );
|
||||
fail(); |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("user.two", |
||||
"plaintext2"); |
||||
assertEquals(1, userDetails.getAuthorities().length); |
||||
assertEquals(DEFAULT_ROLE, |
||||
userDetails.getAuthorities()[0].getAuthority()); |
||||
} catch (BadCredentialsException ex) { |
||||
assertTrue(true); |
||||
fail(); |
||||
} |
||||
} |
||||
|
||||
public void testSimpleCnUser() throws NamingException { |
||||
dao.setUserContext("cn={0},ou=users,ou=system"); |
||||
|
||||
public void testSimpleMultiUserContext() throws NamingException { |
||||
dao.setUserContexts(new String[] {"uid={0},ou=users,ou=system", "cn={0},ou=users,ou=system"}); |
||||
dao.setDefaultRole(DEFAULT_ROLE); |
||||
|
||||
try { |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("user.two", "plaintext2"); |
||||
assertEquals(1, userDetails.getAuthorities().length ); |
||||
assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() ); |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", |
||||
"plaintext"); |
||||
assertEquals(1, userDetails.getAuthorities().length); |
||||
assertEquals(DEFAULT_ROLE, |
||||
userDetails.getAuthorities()[0].getAuthority()); |
||||
|
||||
UserDetails userDetails2 = dao.loadUserByUsernameAndPassword("user.two", |
||||
"plaintext2"); |
||||
assertEquals(1, userDetails2.getAuthorities().length); |
||||
assertEquals(DEFAULT_ROLE, |
||||
userDetails2.getAuthorities()[0].getAuthority()); |
||||
} catch (BadCredentialsException ex) { |
||||
fail(); |
||||
} |
||||
} |
||||
|
||||
public void testSimpleMultiUserContext() throws NamingException { |
||||
dao.setUserContexts(new String[]{"uid={0},ou=users,ou=system", "cn={0},ou=users,ou=system"}); |
||||
|
||||
public void testSimpleUidUser() throws NamingException { |
||||
dao.setUserContext("uid={0},ou=users,ou=system"); |
||||
dao.setDefaultRole(DEFAULT_ROLE); |
||||
|
||||
try { |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", "plaintext"); |
||||
assertEquals(1, userDetails.getAuthorities().length ); |
||||
assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() ); |
||||
|
||||
UserDetails userDetails2 = dao.loadUserByUsernameAndPassword("user.two", "plaintext2"); |
||||
assertEquals(1, userDetails2.getAuthorities().length ); |
||||
assertEquals(DEFAULT_ROLE, userDetails2.getAuthorities()[0].getAuthority() ); |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", |
||||
"plaintext"); |
||||
assertEquals(1, userDetails.getAuthorities().length); |
||||
assertEquals(DEFAULT_ROLE, |
||||
userDetails.getAuthorities()[0].getAuthority()); |
||||
} catch (BadCredentialsException ex) { |
||||
fail(); |
||||
} |
||||
} |
||||
|
||||
public void testEmptyRoles() { |
||||
|
||||
public void testSimpleUidUserBadPassword() throws NamingException { |
||||
dao.setUserContext("uid={0},ou=users,ou=system"); |
||||
dao.setDefaultRole(DEFAULT_ROLE); |
||||
|
||||
try { |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("user.two", "plaintext2"); |
||||
fail("No roles are accessible for user; this test _should_ fail."); |
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", |
||||
"plainlywrong"); |
||||
|
||||
//assertEquals(1, userDetails.getAuthorities().length );
|
||||
//assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() );
|
||||
fail(); |
||||
} catch (BadCredentialsException ex) { |
||||
assertTrue("No roles are accessible for user; this test _should_ fail.", |
||||
ex.getMessage().startsWith(LdapPasswordAuthenticationDao.BAD_CREDENTIALS_EXCEPTION_MESSAGE) ); |
||||
assertTrue(true); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Setup the basic properties of our LdapPasswordAuthenticationDao |
||||
*/ |
||||
protected void setUp() { |
||||
dao = new LdapPasswordAuthenticationDao(); |
||||
dao.setURL("ldap://localhost:389/ou=system"); |
||||
} |
||||
|
||||
/* |
||||
* @todo: |
||||
* 1. two different groups... |
||||
* 2. two groups, limit 'roles' |
||||
* 3. other stuff... |
||||
*/ |
||||
|
||||
} |
||||
|
||||
Loading…
Reference in new issue