From 75feeba835a37a4b99b6bb6307f5afa3f4c1fd06 Mon Sep 17 00:00:00 2001 From: Mattias Arthursson Date: Thu, 5 Jun 2008 13:49:48 +0000 Subject: [PATCH] New module for acegi support --- mvn-build/acegi-support/pom.xml | 173 ++++++++++++++++++ .../AcegiAuthenticationSource.java | 87 +++++++++ .../AcegiAuthenticationSourceTest.java | 144 +++++++++++++++ 3 files changed, 404 insertions(+) create mode 100644 mvn-build/acegi-support/pom.xml create mode 100644 mvn-build/acegi-support/src/main/java/org/springframework/ldap/authentication/AcegiAuthenticationSource.java create mode 100644 mvn-build/acegi-support/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java diff --git a/mvn-build/acegi-support/pom.xml b/mvn-build/acegi-support/pom.xml new file mode 100644 index 00000000..0758029c --- /dev/null +++ b/mvn-build/acegi-support/pom.xml @@ -0,0 +1,173 @@ + + + 4.0.0 + org.springframework.ldap + spring-ldap-acegi-support + Spring LDAP Acegi Support + 1.2.2-SNAPSHOT + + org.springframework.ldap + master + 1.2.2-SNAPSHOT + + + + + + maven-compiler-plugin + + 1.4 + 1.4 + + + + maven-assembly-plugin + + + + src/assemble/bin-with-dependencies.xml + + + + + + + + + + + org.springframework.ldap + spring-ldap + 1.2.2-SNAPSHOT + + + commons-logging + commons-logging + 1.0.4 + + + org.acegisecurity + acegi-security + 1.0.7 + provided + + + aspectj + aspectj + + + cas + casclient + + + commons-codec + commons-codec + + + commons-collections + commons-collections + + + commons-lang + commons-lang + + + commons-logging + commons-logging + + + hsqldb + hsqldb + + + javax.servlet + jsp-api + + + javax.servlet + servlet-api + + + jmock + jmock + + + log4j + log4j + + + net.sj.ehcache + ehcache + + + org.apache.directory.server + apacheds-core + + + org.slf4j + slf4j-log4j12 + + + org.springframework + spring-core + + + org.springframework + spring-jdbc + + + org.springframework + spring-ldap + + + org.springframework + spring-mock + + + org.springframework + spring-remoting + + + org.springframework + spring-support + + + org.springframework + spring-web + + + oro + oro + + + taglibs + standard + + + + + junit + junit + 3.8.2 + test + + + easymock + easymock + 1.2_Java1.3 + test + + + junit + junit + 3.8.2 + test + + + org.springframework + spring-core + ${spring.version} + test + + + diff --git a/mvn-build/acegi-support/src/main/java/org/springframework/ldap/authentication/AcegiAuthenticationSource.java b/mvn-build/acegi-support/src/main/java/org/springframework/ldap/authentication/AcegiAuthenticationSource.java new file mode 100644 index 00000000..2a8fa91e --- /dev/null +++ b/mvn-build/acegi-support/src/main/java/org/springframework/ldap/authentication/AcegiAuthenticationSource.java @@ -0,0 +1,87 @@ +/* + * Copyright 2005-2007 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.authentication; + +import org.acegisecurity.Authentication; +import org.acegisecurity.context.SecurityContextHolder; +import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken; +import org.acegisecurity.userdetails.ldap.LdapUserDetails; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.ldap.core.AuthenticationSource; + +/** + * An AuthenticationSource to retrieve authentication information stored in + * Acegi's SecurityContextHolder. Use Acegi's LdapAuthenticationProvider have a + * LdapUserDetails object placed in the authentication. + * + * @author Mattias Arthursson + * + */ +public class AcegiAuthenticationSource implements AuthenticationSource { + private static final Log log = LogFactory + .getLog(AcegiAuthenticationSource.class); + + /** + * Get the principals of the logged in user, in this case the distinguished + * name. + * + * @return the distinguished name of the logged in user. + */ + public String getPrincipal() { + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); + if (authentication != null) { + Object principal = authentication.getPrincipal(); + if (principal instanceof LdapUserDetails) { + LdapUserDetails details = (LdapUserDetails) principal; + return details.getDn(); + } else if (authentication instanceof AnonymousAuthenticationToken) { + if (log.isDebugEnabled()) { + log + .debug("Anonymous Authentication, returning empty String as Principal"); + } + return ""; + } else { + throw new IllegalArgumentException( + "The principal property of the authentication object -" + + "needs to be a LdapUserDetails."); + } + } else { + log.warn("No Authentication object set in SecurityContext - " + + "returning empty String as Principal"); + return ""; + } + } + + /* + * @see org.springframework.ldap.core.AuthenticationSource#getCredentials() + */ + public String getCredentials() { + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); + + if (authentication != null) { + return (String) authentication.getCredentials(); + } else { + log.warn("No Authentication object set in SecurityContext - " + + "returning empty String as Credentials"); + return ""; + } + } + +} diff --git a/mvn-build/acegi-support/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java b/mvn-build/acegi-support/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java new file mode 100644 index 00000000..dd9d89d4 --- /dev/null +++ b/mvn-build/acegi-support/src/test/java/org/springframework/ldap/authentication/AcegiAuthenticationSourceTest.java @@ -0,0 +1,144 @@ +/* + * Copyright 2005-2007 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.authentication; + +import junit.framework.TestCase; + +import org.acegisecurity.Authentication; +import org.acegisecurity.GrantedAuthority; +import org.acegisecurity.context.SecurityContextHolder; +import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken; +import org.acegisecurity.userdetails.User; +import org.acegisecurity.userdetails.ldap.LdapUserDetails; +import org.easymock.MockControl; +import org.springframework.ldap.authentication.AcegiAuthenticationSource; + +public class AcegiAuthenticationSourceTest extends TestCase { + + private MockControl authenticationControl; + + private Authentication authenticationMock; + + private MockControl ldapUserDetailsControl; + + private LdapUserDetails ldapUserDetailsMock; + + private AcegiAuthenticationSource tested; + + protected void setUp() throws Exception { + super.setUp(); + + authenticationControl = MockControl.createControl(Authentication.class); + authenticationMock = (Authentication) authenticationControl.getMock(); + + ldapUserDetailsControl = MockControl + .createControl(LdapUserDetails.class); + ldapUserDetailsMock = (LdapUserDetails) ldapUserDetailsControl + .getMock(); + + tested = new AcegiAuthenticationSource(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + + authenticationControl = null; + authenticationMock = null; + + ldapUserDetailsControl = null; + ldapUserDetailsMock = null; + + tested = null; + } + + protected void replay() { + authenticationControl.replay(); + ldapUserDetailsControl.replay(); + } + + protected void verify() { + authenticationControl.verify(); + ldapUserDetailsControl.verify(); + } + + public void testGetPrincipalAndCredentials() { + authenticationControl.expectAndReturn( + authenticationMock.getPrincipal(), ldapUserDetailsMock); + authenticationControl.expectAndReturn(authenticationMock + .getCredentials(), "secret"); + + ldapUserDetailsControl.expectAndDefaultReturn(ldapUserDetailsMock + .getDn(), "cn=Manager"); + + SecurityContextHolder.getContext() + .setAuthentication(authenticationMock); + + replay(); + + String principal = tested.getPrincipal(); + String credentials = tested.getCredentials(); + + verify(); + + assertEquals("secret", credentials); + assertEquals("cn=Manager", principal); + } + + public void testGetPrincipalAndCredentials_nullAuthentication() { + SecurityContextHolder.getContext().setAuthentication(null); + + replay(); + + assertEquals("", tested.getPrincipal()); + assertEquals("", tested.getCredentials()); + verify(); + } + + public void testGetPrincipal_InvalidPrincipal() { + authenticationControl.expectAndReturn( + authenticationMock.getPrincipal(), new User("dummy", "dummy", + true, true, true, true, new GrantedAuthority[0])); + + SecurityContextHolder.getContext() + .setAuthentication(authenticationMock); + + replay(); + + try { + tested.getPrincipal(); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + verify(); + } + + public void testGetPrincipalWithAnonymousAuthenticationToken() { + + SecurityContextHolder.getContext().setAuthentication( + new AnonymousAuthenticationToken("dummy", "dummy", + new GrantedAuthority[] { new DummyAuthoroty() })); + + assertEquals("", tested.getPrincipal()); + } + + private static class DummyAuthoroty implements GrantedAuthority { + public String getAuthority() { + return null; + } + } +}