SEC-552: Replaced authorites populators in CAS and OpenID with a plain UserDetailsService
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
/* Copyright 2004, 2005, 2006 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.springframework.security.providers.openid;
|
||||
|
||||
import org.springframework.security.AuthenticationException;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.security.providers.AuthoritiesPopulator;
|
||||
import org.springframework.security.userdetails.User;
|
||||
import org.springframework.security.userdetails.UserDetails;
|
||||
|
||||
|
||||
/**
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Robin Bramley, Opsera Ltd
|
||||
*/
|
||||
public class MockAuthoritiesPopulator implements AuthoritiesPopulator {
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public UserDetails getUserDetails(String ssoUserId)
|
||||
throws AuthenticationException {
|
||||
return new User(ssoUserId, "password", true, true, true, true,
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A"), new GrantedAuthorityImpl("ROLE_B")});
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,13 @@ import junit.framework.TestCase;
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.AuthenticationServiceException;
|
||||
import org.springframework.security.BadCredentialsException;
|
||||
import org.springframework.security.AuthenticationException;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.security.userdetails.UserDetails;
|
||||
import org.springframework.security.userdetails.User;
|
||||
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.userdetails.UserDetailsService;
|
||||
|
||||
|
||||
/**
|
||||
@@ -38,7 +44,7 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
*/
|
||||
public void testAuthenticateCancel() {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
|
||||
Authentication preAuth = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.CANCELLED, USERNAME, "");
|
||||
|
||||
@@ -57,7 +63,7 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
*/
|
||||
public void testAuthenticateError() {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
|
||||
Authentication preAuth = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.ERROR, USERNAME, "");
|
||||
|
||||
@@ -76,7 +82,7 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
*/
|
||||
public void testAuthenticateFailure() {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
|
||||
Authentication preAuth = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.FAILURE, USERNAME, "");
|
||||
|
||||
@@ -95,7 +101,7 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
*/
|
||||
public void testAuthenticateSetupNeeded() {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
|
||||
Authentication preAuth = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.SETUP_NEEDED, USERNAME, "");
|
||||
|
||||
@@ -114,7 +120,7 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
*/
|
||||
public void testAuthenticateSuccess() {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
|
||||
Authentication preAuth = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.SUCCESS, USERNAME, "");
|
||||
|
||||
@@ -149,7 +155,7 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
*/
|
||||
public void testDoesntSupport() {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
|
||||
assertFalse(provider.supports(UsernamePasswordAuthenticationToken.class));
|
||||
}
|
||||
@@ -159,7 +165,7 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
*/
|
||||
public void testIgnoresUserPassAuthToken() {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(USERNAME, "password");
|
||||
assertEquals(null, provider.authenticate(token));
|
||||
@@ -170,17 +176,17 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
*/
|
||||
public void testSupports() {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
|
||||
assertTrue(provider.supports(OpenIDAuthenticationToken.class));
|
||||
}
|
||||
|
||||
public void testValidation() throws Exception {
|
||||
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
|
||||
provider.setAuthoritiesPopulator(new MockAuthoritiesPopulator());
|
||||
provider.setUserDetailsService(new MockUserDetailsService());
|
||||
provider.afterPropertiesSet();
|
||||
|
||||
provider.setAuthoritiesPopulator(null);
|
||||
provider.setUserDetailsService(null);
|
||||
|
||||
try {
|
||||
provider.afterPropertiesSet();
|
||||
@@ -189,4 +195,12 @@ public class OpenIDAuthenticationProviderTests extends TestCase {
|
||||
//expected
|
||||
}
|
||||
}
|
||||
|
||||
static class MockUserDetailsService implements UserDetailsService {
|
||||
public UserDetails loadUserByUsername(String ssoUserId)
|
||||
throws AuthenticationException {
|
||||
return new User(ssoUserId, "password", true, true, true, true,
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A"), new GrantedAuthorityImpl("ROLE_B")});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user