Added methods for 'simple' bind authentication in LdapTemplate (LdapOperations, SimpleLdapOperations and SimpleLdapTemplate as well).

Introduced new callback interface for use with certain authentication methods.
Added LdapEntryIdentification to wrap the full identification of an LDAP entry (absolute and relative DNs)
Added LdapEntryIdentificationContextMapper.
This commit is contained in:
Mattias Arthursson
2008-11-18 14:10:27 +00:00
parent 0819e34ab6
commit e910acfbba
12 changed files with 3300 additions and 2867 deletions

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2005-2008 the original author or authors.
*
* 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.springframework.ldap;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.AbstractContextSource;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;
import org.springframework.test.context.ContextConfiguration;
/**
* Tests the lookup methods of LdapTemplate.
*
* @author Mattias Hellborg Arthursson
* @author Ulrik Sandberg
*/
@ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" })
public class LdapTemplateAuthenticationITest extends AbstractLdapTemplateIntegrationTest {
@Autowired
private LdapTemplate tested;
@Test
public void testAuthenticate() {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3"));
assertTrue(tested.authenticate("", filter.toString(), "password"));
}
@Test
public void testAuthenticateWithInvalidPassword() {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3"));
assertFalse(tested.authenticate("", filter.toString(), "invalidpassword"));
}
@Test
public void testAuthenticateWithFilterThatDoesNotMatchAnything() {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person")).and(
new EqualsFilter("uid", "some.person.that.isnt.there"));
assertFalse(tested.authenticate("", filter.toString(), "password"));
}
@Test
public void testAuthenticateWithFilterThatMatchesSeveralEntries() {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person")).and(new WhitespaceWildcardsFilter("uid", "some.person"));
assertFalse(tested.authenticate("", filter.toString(), "password"));
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.ldap;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
@@ -28,8 +29,13 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.AbstractContextMapper;
import org.springframework.ldap.test.AttributeCheckAttributesMapper;
import org.springframework.ldap.test.AttributeCheckContextMapper;
import org.springframework.test.context.ContextConfiguration;
@@ -140,6 +146,35 @@ public class LdapTemplateSearchResultITest extends AbstractLdapTemplateIntegrati
assertEquals(1, list.size());
}
@Test
public void testSearchForObject() {
contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
contextMapper.setExpectedValues(ALL_VALUES);
DirContextAdapter result = (DirContextAdapter) tested
.searchForObject(BASE_STRING, FILTER_STRING, contextMapper);
assertNotNull(result);
}
@Test(expected = IncorrectResultSizeDataAccessException.class)
public void testSearchForObjectWithMultipleHits() {
tested.searchForObject(BASE_STRING, "(&(objectclass=person)(sn=*))", new AbstractContextMapper() {
@Override
protected Object doMapFromContext(DirContextOperations ctx) {
return ctx;
}
});
}
@Test(expected = EmptyResultDataAccessException.class)
public void testSearchForObjectNoHits() {
tested.searchForObject(BASE_STRING, "(&(objectclass=person)(sn=Person does not exist))", new AbstractContextMapper() {
@Override
protected Object doMapFromContext(DirContextOperations ctx) {
return ctx;
}
});
}
@Test
public void testSearch_SearchScope_ContextMapper() {
contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);

View File

@@ -33,6 +33,8 @@ import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DirContextProcessor;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations = { "/conf/simpleLdapTemplateTestContext.xml" })
@@ -65,6 +67,12 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
assertEquals("Some Person3", cns.get(0));
}
@Test
public void testSearchForObject() {
String cn = ldapTemplate.searchForObject("", "(&(objectclass=person)(sn=Person3))", new CnContextMapper());
assertEquals("Some Person3", cn);
}
@Test
public void testSearchProcessor() {
SearchControls searchControls = new SearchControls();
@@ -184,6 +192,13 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
verifyCleanup();
}
@Test
public void testAuthenticate() {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3"));
assertTrue(ldapTemplate.authenticate("", filter.toString(), "password"));
}
private void verifyBoundCorrectData() {
DirContextOperations result = ldapTemplate.lookupContext(DN_STRING);
assertEquals("Some Person4", result.getStringAttribute("cn"));