137 changed files with 2397 additions and 4366 deletions
@ -1,27 +1,25 @@
@@ -1,27 +1,25 @@
|
||||
package org.springframework.security; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
public class GrantedAuthoritiesContainerImpl implements MutableGrantedAuthoritiesContainer { |
||||
private List authorities; |
||||
private List<GrantedAuthority> authorities; |
||||
|
||||
public void setGrantedAuthorities(GrantedAuthority[] newAuthorities) { |
||||
this.authorities = new ArrayList(newAuthorities.length); |
||||
authorities.addAll(Arrays.asList(newAuthorities)); |
||||
} |
||||
public void setGrantedAuthorities(List<GrantedAuthority> newAuthorities) { |
||||
authorities = Collections.unmodifiableList(newAuthorities); |
||||
} |
||||
|
||||
public GrantedAuthority[] getGrantedAuthorities() { |
||||
Assert.notNull(authorities, "Granted authorities have not been set"); |
||||
return (GrantedAuthority[]) authorities.toArray(new GrantedAuthority[authorities.size()]); |
||||
} |
||||
|
||||
public String toString() { |
||||
StringBuffer sb = new StringBuffer(); |
||||
sb.append("Authorities: ").append(authorities); |
||||
return sb.toString(); |
||||
} |
||||
public List<GrantedAuthority> getGrantedAuthorities() { |
||||
Assert.notNull(authorities, "Granted authorities have not been set"); |
||||
return authorities; |
||||
} |
||||
|
||||
public String toString() { |
||||
StringBuffer sb = new StringBuffer(); |
||||
sb.append("Authorities: ").append(authorities); |
||||
return sb.toString(); |
||||
} |
||||
} |
||||
|
||||
@ -1,133 +0,0 @@
@@ -1,133 +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.x509; |
||||
|
||||
import org.springframework.security.SpringSecurityMessageSource; |
||||
import org.springframework.security.Authentication; |
||||
import org.springframework.security.AuthenticationException; |
||||
import org.springframework.security.BadCredentialsException; |
||||
|
||||
import org.springframework.security.providers.AuthenticationProvider; |
||||
import org.springframework.security.providers.x509.cache.NullX509UserCache; |
||||
|
||||
import org.springframework.security.userdetails.UserDetails; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.beans.factory.InitializingBean; |
||||
|
||||
import org.springframework.context.MessageSource; |
||||
import org.springframework.context.MessageSourceAware; |
||||
import org.springframework.context.support.MessageSourceAccessor; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
|
||||
/** |
||||
* Processes an X.509 authentication request.<p>The request will typically originate from {@link |
||||
* org.springframework.security.ui.x509.X509ProcessingFilter}).</p> |
||||
* |
||||
* @author Luke Taylor |
||||
* @deprecated superceded by the preauth provider. Use the X.509 authentication support in org.springframework.security.ui.preauth.x509 instead |
||||
* or namespace support via the <x509 /> element. |
||||
* @version $Id$ |
||||
*/ |
||||
public class X509AuthenticationProvider implements AuthenticationProvider, InitializingBean, MessageSourceAware { |
||||
//~ Static fields/initializers =====================================================================================
|
||||
|
||||
private static final Log logger = LogFactory.getLog(X509AuthenticationProvider.class); |
||||
|
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); |
||||
private X509AuthoritiesPopulator x509AuthoritiesPopulator; |
||||
private X509UserCache userCache = new NullX509UserCache(); |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void afterPropertiesSet() throws Exception { |
||||
Assert.notNull(userCache, "An x509UserCache must be set"); |
||||
Assert.notNull(x509AuthoritiesPopulator, "An X509AuthoritiesPopulator must be set"); |
||||
Assert.notNull(this.messages, "A message source must be set"); |
||||
} |
||||
|
||||
/** |
||||
* If the supplied authentication token contains a certificate then this will be passed to the configured |
||||
* {@link X509AuthoritiesPopulator} to obtain the user details and authorities for the user identified by the |
||||
* certificate.<p>If no certificate is present (for example, if the filter is applied to an HttpRequest for |
||||
* which client authentication hasn't been configured in the container) then a BadCredentialsException will be |
||||
* raised.</p> |
||||
* |
||||
* @param authentication the authentication request. |
||||
* |
||||
* @return an X509AuthenticationToken containing the authorities of the principal represented by the certificate. |
||||
* |
||||
* @throws AuthenticationException if the {@link X509AuthoritiesPopulator} rejects the certficate. |
||||
* @throws BadCredentialsException if no certificate was presented in the authentication request. |
||||
*/ |
||||
public Authentication authenticate(Authentication authentication) |
||||
throws AuthenticationException { |
||||
if (!supports(authentication.getClass())) { |
||||
return null; |
||||
} |
||||
|
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("X509 authentication request: " + authentication); |
||||
} |
||||
|
||||
X509Certificate clientCertificate = (X509Certificate) authentication.getCredentials(); |
||||
|
||||
if (clientCertificate == null) { |
||||
throw new BadCredentialsException(messages.getMessage("X509AuthenticationProvider.certificateNull", |
||||
"Certificate is null")); |
||||
} |
||||
|
||||
UserDetails user = userCache.getUserFromCache(clientCertificate); |
||||
|
||||
if (user == null) { |
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("Authenticating with certificate " + clientCertificate); |
||||
} |
||||
user = x509AuthoritiesPopulator.getUserDetails(clientCertificate); |
||||
userCache.putUserInCache(clientCertificate, user); |
||||
} |
||||
|
||||
X509AuthenticationToken result = new X509AuthenticationToken(user, clientCertificate, user.getAuthorities()); |
||||
|
||||
result.setDetails(authentication.getDetails()); |
||||
|
||||
return result; |
||||
} |
||||
|
||||
public void setMessageSource(MessageSource messageSource) { |
||||
this.messages = new MessageSourceAccessor(messageSource); |
||||
} |
||||
|
||||
public void setX509AuthoritiesPopulator(X509AuthoritiesPopulator x509AuthoritiesPopulator) { |
||||
this.x509AuthoritiesPopulator = x509AuthoritiesPopulator; |
||||
} |
||||
|
||||
public void setX509UserCache(X509UserCache cache) { |
||||
this.userCache = cache; |
||||
} |
||||
|
||||
public boolean supports(Class authentication) { |
||||
return X509AuthenticationToken.class.isAssignableFrom(authentication); |
||||
} |
||||
} |
||||
@ -1,77 +0,0 @@
@@ -1,77 +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.x509; |
||||
|
||||
import org.springframework.security.GrantedAuthority; |
||||
|
||||
import org.springframework.security.providers.AbstractAuthenticationToken; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
|
||||
/** |
||||
* <code>Authentication</code> implementation for X.509 client-certificate authentication. |
||||
* |
||||
* @author Luke Taylor |
||||
* @deprecated superceded by the preauth provider. Use the X.509 authentication support in org.springframework.security.ui.preauth.x509 instead. |
||||
* @version $Id$ |
||||
*/ |
||||
public class X509AuthenticationToken extends AbstractAuthenticationToken { |
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
private Object principal; |
||||
private X509Certificate credentials; |
||||
|
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
/** |
||||
* Used for an authentication request. The {@link org.springframework.security.Authentication#isAuthenticated()} will return |
||||
* <code>false</code>. |
||||
* |
||||
* @param credentials the certificate |
||||
*/ |
||||
public X509AuthenticationToken(X509Certificate credentials) { |
||||
super(null); |
||||
this.credentials = credentials; |
||||
} |
||||
|
||||
/** |
||||
* Used for an authentication response object. The {@link org.springframework.security.Authentication#isAuthenticated()} |
||||
* will return <code>true</code>. |
||||
* |
||||
* @param principal the principal, which is generally a |
||||
* <code>UserDetails</code> |
||||
* @param credentials the certificate |
||||
* @param authorities the authorities |
||||
*/ |
||||
public X509AuthenticationToken(Object principal, X509Certificate credentials, GrantedAuthority[] authorities) { |
||||
super(authorities); |
||||
this.principal = principal; |
||||
this.credentials = credentials; |
||||
setAuthenticated(true); |
||||
} |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public Object getCredentials() { |
||||
return credentials; |
||||
} |
||||
|
||||
public Object getPrincipal() { |
||||
return principal; |
||||
} |
||||
} |
||||
@ -1,55 +0,0 @@
@@ -1,55 +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.x509; |
||||
|
||||
import org.springframework.security.AuthenticationException; |
||||
|
||||
import org.springframework.security.userdetails.UserDetails; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
|
||||
/** |
||||
* Populates the <code>UserDetails</code> associated with the X.509 |
||||
* certificate presented by a client. |
||||
* <p> |
||||
* Although the certificate will already have been validated by the web container, |
||||
* implementations may choose to perform additional application-specific checks on |
||||
* the certificate content here. If an implementation chooses to reject the certificate, |
||||
* it should throw a {@link org.springframework.security.BadCredentialsException}. |
||||
* </p> |
||||
* |
||||
* @author Luke Taylor |
||||
* @deprecated |
||||
* @version $Id$ |
||||
*/ |
||||
public interface X509AuthoritiesPopulator { |
||||
//~ Methods ========================================================================================================
|
||||
|
||||
/** |
||||
* Obtains the granted authorities for the specified user.<p>May throw any |
||||
* <code>AuthenticationException</code> or return <code>null</code> if the authorities are unavailable.</p> |
||||
* |
||||
* @param userCertificate the X.509 certificate supplied |
||||
* |
||||
* @return the details of the indicated user (at minimum the granted authorities and the username) |
||||
* |
||||
* @throws AuthenticationException if the user details are not available or the certificate isn't valid for the |
||||
* application's purpose. |
||||
*/ |
||||
UserDetails getUserDetails(X509Certificate userCertificate) |
||||
throws AuthenticationException; |
||||
} |
||||
@ -1,44 +0,0 @@
@@ -1,44 +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.x509; |
||||
|
||||
import org.springframework.security.userdetails.UserDetails; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
|
||||
/** |
||||
* Provides a cache of {@link UserDetails} objects for the |
||||
* {@link X509AuthenticationProvider}. |
||||
* <p> |
||||
* Similar in function to the {@link org.springframework.security.providers.dao.UserCache} |
||||
* used by the Dao provider, but the cache is keyed with the user's certificate |
||||
* rather than the user name. |
||||
* </p> |
||||
* |
||||
* @author Luke Taylor |
||||
* @deprecated |
||||
* @version $Id$ |
||||
*/ |
||||
public interface X509UserCache { |
||||
//~ Methods ========================================================================================================
|
||||
|
||||
UserDetails getUserFromCache(X509Certificate userCertificate); |
||||
|
||||
void putUserInCache(X509Certificate key, UserDetails user); |
||||
|
||||
void removeUserFromCache(X509Certificate key); |
||||
} |
||||
@ -1,109 +0,0 @@
@@ -1,109 +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.x509.cache; |
||||
|
||||
import net.sf.ehcache.CacheException; |
||||
import net.sf.ehcache.Element; |
||||
import net.sf.ehcache.Ehcache; |
||||
|
||||
import org.springframework.security.providers.x509.X509UserCache; |
||||
|
||||
import org.springframework.security.userdetails.UserDetails; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.beans.factory.InitializingBean; |
||||
|
||||
import org.springframework.dao.DataRetrievalFailureException; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
|
||||
/** |
||||
* Caches <code>User</code> objects using a Spring IoC defined <a |
||||
* href="http://ehcache.sourceforge.net">EHCACHE</a>. |
||||
* |
||||
* @author Luke Taylor |
||||
* @author Ben Alex |
||||
* @deprecated use the X509 preauthenticated |
||||
* @version $Id$ |
||||
*/ |
||||
public class EhCacheBasedX509UserCache implements X509UserCache, InitializingBean { |
||||
//~ Static fields/initializers =====================================================================================
|
||||
|
||||
private static final Log logger = LogFactory.getLog(EhCacheBasedX509UserCache.class); |
||||
|
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
private Ehcache cache; |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void afterPropertiesSet() throws Exception { |
||||
Assert.notNull(cache, "cache is mandatory"); |
||||
} |
||||
|
||||
public UserDetails getUserFromCache(X509Certificate userCert) { |
||||
Element element = null; |
||||
|
||||
try { |
||||
element = cache.get(userCert); |
||||
} catch (CacheException cacheException) { |
||||
throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage()); |
||||
} |
||||
|
||||
if (logger.isDebugEnabled()) { |
||||
String subjectDN = "unknown"; |
||||
|
||||
if ((userCert != null) && (userCert.getSubjectDN() != null)) { |
||||
subjectDN = userCert.getSubjectDN().toString(); |
||||
} |
||||
|
||||
logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN); |
||||
} |
||||
|
||||
if (element == null) { |
||||
return null; |
||||
} else { |
||||
return (UserDetails) element.getValue(); |
||||
} |
||||
} |
||||
|
||||
public void putUserInCache(X509Certificate userCert, UserDetails user) { |
||||
Element element = new Element(userCert, user); |
||||
|
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("Cache put: " + userCert.getSubjectDN()); |
||||
} |
||||
|
||||
cache.put(element); |
||||
} |
||||
|
||||
public void removeUserFromCache(X509Certificate userCert) { |
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("Cache remove: " + userCert.getSubjectDN()); |
||||
} |
||||
|
||||
cache.remove(userCert); |
||||
} |
||||
|
||||
public void setCache(Ehcache cache) { |
||||
this.cache = cache; |
||||
} |
||||
} |
||||
@ -1,42 +0,0 @@
@@ -1,42 +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.x509.cache; |
||||
|
||||
import org.springframework.security.providers.x509.X509UserCache; |
||||
|
||||
import org.springframework.security.userdetails.UserDetails; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
|
||||
/** |
||||
* "Cache" that doesn't do any caching. |
||||
* |
||||
* @author Luke Taylor |
||||
* @deprecated |
||||
* @version $Id$ |
||||
*/ |
||||
public class NullX509UserCache implements X509UserCache { |
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public UserDetails getUserFromCache(X509Certificate certificate) { |
||||
return null; |
||||
} |
||||
|
||||
public void putUserInCache(X509Certificate certificate, UserDetails user) {} |
||||
|
||||
public void removeUserFromCache(X509Certificate certificate) {} |
||||
} |
||||
@ -1,5 +0,0 @@
@@ -1,5 +0,0 @@
|
||||
<html> |
||||
<body> |
||||
Deprecated and will be removed in a future version. Use a caching UserDetailsService instead. |
||||
</body> |
||||
</html> |
||||
@ -1,6 +0,0 @@
@@ -1,6 +0,0 @@
|
||||
<html> |
||||
<body> |
||||
This package is now deprecated and will be removed in a future version. |
||||
Use the X.509 authentication support in org.springframework.security.ui.preauth.x509 instead. |
||||
</body> |
||||
</html> |
||||
@ -1,119 +0,0 @@
@@ -1,119 +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.x509.populator; |
||||
|
||||
import org.springframework.security.SpringSecurityMessageSource; |
||||
import org.springframework.security.AuthenticationException; |
||||
import org.springframework.security.BadCredentialsException; |
||||
import org.springframework.security.AuthenticationServiceException; |
||||
|
||||
import org.springframework.security.providers.x509.X509AuthoritiesPopulator; |
||||
|
||||
import org.springframework.security.userdetails.UserDetails; |
||||
import org.springframework.security.userdetails.UserDetailsService; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.beans.factory.InitializingBean; |
||||
|
||||
import org.springframework.context.MessageSource; |
||||
import org.springframework.context.MessageSourceAware; |
||||
import org.springframework.context.support.MessageSourceAccessor; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
import java.util.regex.Pattern; |
||||
import java.util.regex.Matcher; |
||||
|
||||
/** |
||||
* Populates the X509 authorities via an {@link org.springframework.security.userdetails.UserDetailsService}. |
||||
* |
||||
* @author Luke Taylor |
||||
* @deprecated This package is now deprecated. Use the X.509 authentication support in |
||||
* org.springframework.security.ui.preauth.x509 instead. |
||||
* @version $Id$ |
||||
*/ |
||||
public class DaoX509AuthoritiesPopulator implements X509AuthoritiesPopulator, InitializingBean, MessageSourceAware { |
||||
//~ Static fields/initializers =====================================================================================
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DaoX509AuthoritiesPopulator.class); |
||||
|
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); |
||||
private Pattern subjectDNPattern; |
||||
private String subjectDNRegex = "CN=(.*?),"; |
||||
private UserDetailsService userDetailsService; |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void afterPropertiesSet() throws Exception { |
||||
Assert.notNull(userDetailsService, "An authenticationDao must be set"); |
||||
Assert.notNull(this.messages, "A message source must be set"); |
||||
|
||||
subjectDNPattern = Pattern.compile(subjectDNRegex, Pattern.CASE_INSENSITIVE); |
||||
} |
||||
|
||||
public UserDetails getUserDetails(X509Certificate clientCert) throws AuthenticationException { |
||||
String subjectDN = clientCert.getSubjectDN().getName(); |
||||
|
||||
Matcher matcher = subjectDNPattern.matcher(subjectDN); |
||||
|
||||
if (!matcher.find()) { |
||||
throw new BadCredentialsException(messages.getMessage("DaoX509AuthoritiesPopulator.noMatching", |
||||
new Object[] {subjectDN}, "No matching pattern was found in subjectDN: {0}")); |
||||
} |
||||
|
||||
if (matcher.groupCount() != 1) { |
||||
throw new IllegalArgumentException("Regular expression must contain a single group "); |
||||
} |
||||
|
||||
String userName = matcher.group(1); |
||||
|
||||
UserDetails user = this.userDetailsService.loadUserByUsername(userName); |
||||
|
||||
if (user == null) { |
||||
throw new AuthenticationServiceException( |
||||
"UserDetailsService returned null, which is an interface contract violation"); |
||||
} |
||||
|
||||
return user; |
||||
} |
||||
|
||||
public void setMessageSource(MessageSource messageSource) { |
||||
this.messages = new MessageSourceAccessor(messageSource); |
||||
} |
||||
|
||||
/** |
||||
* Sets the regular expression which will by used to extract the user name from the certificate's Subject |
||||
* DN. |
||||
* <p>It should contain a single group; for example the default expression "CN=(.?)," matches the common |
||||
* name field. So "CN=Jimi Hendrix, OU=..." will give a user name of "Jimi Hendrix".</p> |
||||
* <p>The matches are case insensitive. So "emailAddress=(.?)," will match "EMAILADDRESS=jimi@hendrix.org, |
||||
* CN=..." giving a user name "jimi@hendrix.org"</p> |
||||
* |
||||
* @param subjectDNRegex the regular expression to find in the subject |
||||
*/ |
||||
public void setSubjectDNRegex(String subjectDNRegex) { |
||||
this.subjectDNRegex = subjectDNRegex; |
||||
} |
||||
|
||||
public void setUserDetailsService(UserDetailsService userDetailsService) { |
||||
this.userDetailsService = userDetailsService; |
||||
} |
||||
} |
||||
@ -1,7 +0,0 @@
@@ -1,7 +0,0 @@
|
||||
<html> |
||||
<body> |
||||
This package is now deprecated and will be removed in a future version. |
||||
Use the X.509 authentication support in org.springframework.security.ui.preauth.x509 instead. |
||||
Authorities are loaded by a UserDetailsService. |
||||
</body> |
||||
</html> |
||||
@ -1,210 +0,0 @@
@@ -1,210 +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.ui.x509; |
||||
|
||||
import org.springframework.security.Authentication; |
||||
import org.springframework.security.AuthenticationException; |
||||
import org.springframework.security.AuthenticationManager; |
||||
|
||||
import org.springframework.security.context.SecurityContextHolder; |
||||
|
||||
import org.springframework.security.event.authentication.InteractiveAuthenticationSuccessEvent; |
||||
|
||||
import org.springframework.security.providers.x509.X509AuthenticationToken; |
||||
|
||||
import org.springframework.security.ui.AbstractProcessingFilter; |
||||
import org.springframework.security.ui.AuthenticationDetailsSource; |
||||
import org.springframework.security.ui.WebAuthenticationDetailsSource; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.beans.factory.InitializingBean; |
||||
|
||||
import org.springframework.context.ApplicationEventPublisher; |
||||
import org.springframework.context.ApplicationEventPublisherAware; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import javax.servlet.Filter; |
||||
import javax.servlet.ServletRequest; |
||||
import javax.servlet.ServletException; |
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.ServletResponse; |
||||
import javax.servlet.FilterConfig; |
||||
|
||||
|
||||
/** |
||||
* Processes the X.509 certificate submitted by a client browser when HTTPS is used with client-authentication |
||||
* enabled.<p>An {@link X509AuthenticationToken} is created with the certificate as the credentials.</p> |
||||
* <p>The configured authentication manager is expected to supply a provider which can handle this token (usually |
||||
* an instance of {@link org.springframework.security.providers.x509.X509AuthenticationProvider}).</p> |
||||
* <p>If authentication is successful, an {@link |
||||
* org.springframework.security.event.authentication.InteractiveAuthenticationSuccessEvent} will be published to the application |
||||
* context. No events will be published if authentication was unsuccessful, because this would generally be recorded |
||||
* via an <code>AuthenticationManager</code>-specific application event.</p> |
||||
* |
||||
* @author Luke Taylor |
||||
* @deprecated Use <tt>X509PreAuthenticatedProcessingFilter</tt> from the preauth.x509 package instead |
||||
* @version $Id$ |
||||
*/ |
||||
public class X509ProcessingFilter implements Filter, InitializingBean, ApplicationEventPublisherAware { |
||||
//~ Static fields/initializers =====================================================================================
|
||||
|
||||
private static final Log logger = LogFactory.getLog(X509ProcessingFilter.class); |
||||
|
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
private ApplicationEventPublisher eventPublisher; |
||||
private AuthenticationDetailsSource authenticationDetailsSource = new WebAuthenticationDetailsSource(); |
||||
private AuthenticationManager authenticationManager; |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void afterPropertiesSet() throws Exception { |
||||
Assert.notNull(authenticationManager, "An AuthenticationManager must be set"); |
||||
} |
||||
|
||||
public void destroy() {} |
||||
|
||||
/** |
||||
* This method first checks for an existing, non-null authentication in the secure context. If one is found |
||||
* it does nothing.<p>If no authentication object exists, it attempts to obtain the client authentication |
||||
* certificate from the request. If there is no certificate present then authentication is skipped. Otherwise a |
||||
* new authentication request containing the certificate will be passed to the configured {@link |
||||
* AuthenticationManager}.</p> |
||||
* <p>If authentication is successful the returned token will be stored in the secure context. Otherwise |
||||
* it will be set to null. In either case, the request proceeds through the filter chain.</p> |
||||
* |
||||
* @param request DOCUMENT ME! |
||||
* @param response DOCUMENT ME! |
||||
* @param filterChain DOCUMENT ME! |
||||
* |
||||
* @throws IOException DOCUMENT ME! |
||||
* @throws javax.servlet.ServletException DOCUMENT ME! |
||||
*/ |
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) |
||||
throws IOException, ServletException { |
||||
if (!(request instanceof HttpServletRequest)) { |
||||
throw new ServletException("Can only process HttpServletRequest"); |
||||
} |
||||
|
||||
if (!(response instanceof HttpServletResponse)) { |
||||
throw new ServletException("Can only process HttpServletResponse"); |
||||
} |
||||
|
||||
HttpServletRequest httpRequest = (HttpServletRequest) request; |
||||
HttpServletResponse httpResponse = (HttpServletResponse) response; |
||||
|
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("Checking secure context token: " + SecurityContextHolder.getContext().getAuthentication()); |
||||
} |
||||
|
||||
if (SecurityContextHolder.getContext().getAuthentication() == null) { |
||||
Authentication authResult = null; |
||||
X509Certificate clientCertificate = extractClientCertificate(httpRequest); |
||||
|
||||
try { |
||||
X509AuthenticationToken authRequest = new X509AuthenticationToken(clientCertificate); |
||||
|
||||
authRequest.setDetails(authenticationDetailsSource.buildDetails((HttpServletRequest) request)); |
||||
authResult = authenticationManager.authenticate(authRequest); |
||||
successfulAuthentication(httpRequest, httpResponse, authResult); |
||||
} catch (AuthenticationException failed) { |
||||
unsuccessfulAuthentication(httpRequest, httpResponse, failed); |
||||
} |
||||
} |
||||
|
||||
filterChain.doFilter(request, response); |
||||
} |
||||
|
||||
private X509Certificate extractClientCertificate(HttpServletRequest request) { |
||||
X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate"); |
||||
|
||||
if ((certs != null) && (certs.length > 0)) { |
||||
return certs[0]; |
||||
} |
||||
|
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("No client certificate found in request."); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
public void init(FilterConfig ignored) throws ServletException {} |
||||
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher context) { |
||||
this.eventPublisher = context; |
||||
} |
||||
|
||||
public void setAuthenticationDetailsSource(AuthenticationDetailsSource authenticationDetailsSource) { |
||||
Assert.notNull(authenticationDetailsSource, "AuthenticationDetailsSource required"); |
||||
this.authenticationDetailsSource = authenticationDetailsSource; |
||||
} |
||||
|
||||
public void setAuthenticationManager(AuthenticationManager authenticationManager) { |
||||
this.authenticationManager = authenticationManager; |
||||
} |
||||
|
||||
/** |
||||
* Puts the <code>Authentication</code> instance returned by the authentication manager into the secure |
||||
* context. |
||||
* |
||||
* @param request DOCUMENT ME! |
||||
* @param response DOCUMENT ME! |
||||
* @param authResult DOCUMENT ME! |
||||
* |
||||
* @throws IOException DOCUMENT ME! |
||||
*/ |
||||
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, |
||||
Authentication authResult) throws IOException { |
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("Authentication success: " + authResult); |
||||
} |
||||
|
||||
SecurityContextHolder.getContext().setAuthentication(authResult); |
||||
|
||||
// Fire event
|
||||
if (this.eventPublisher != null) { |
||||
eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass())); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Ensures the authentication object in the secure context is set to null when authentication fails. |
||||
* |
||||
* @param request DOCUMENT ME! |
||||
* @param response DOCUMENT ME! |
||||
* @param failed DOCUMENT ME! |
||||
*/ |
||||
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, |
||||
AuthenticationException failed) { |
||||
SecurityContextHolder.getContext().setAuthentication(null); |
||||
|
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("Updated SecurityContextHolder to contain null Authentication"); |
||||
} |
||||
|
||||
request.getSession().setAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY, failed); |
||||
} |
||||
} |
||||
@ -1,77 +0,0 @@
@@ -1,77 +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.ui.x509; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import javax.servlet.ServletException; |
||||
import javax.servlet.ServletRequest; |
||||
import javax.servlet.ServletResponse; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import org.springframework.security.AuthenticationException; |
||||
import org.springframework.security.ui.AuthenticationEntryPoint; |
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
/** |
||||
* In the X.509 authentication case (unlike CAS, for example) the certificate |
||||
* will already have been extracted from the request and a secure context |
||||
* established by the time the security-enforcement filter is invoked. |
||||
* <p> |
||||
* Therefore this class isn't actually responsible for the commencement of |
||||
* authentication, as it is in the case of other providers. It will be called if |
||||
* the certificate was rejected by Spring Security's X509AuthenticationProvider, resulting |
||||
* in a null authentication. |
||||
* </p> |
||||
* The <code>commence</code> method will always return an |
||||
* <code>HttpServletResponse.SC_FORBIDDEN</code> (403 error). |
||||
* |
||||
* @author Luke Taylor |
||||
* @deprecated Use the preauth package instead |
||||
* @version $Id$ |
||||
* |
||||
* @see org.springframework.security.ui.ExceptionTranslationFilter |
||||
*/ |
||||
public class X509ProcessingFilterEntryPoint implements AuthenticationEntryPoint { |
||||
// ~ Static fields/initializers
|
||||
// =====================================================================================
|
||||
|
||||
private static final Log logger = LogFactory.getLog(X509ProcessingFilterEntryPoint.class); |
||||
|
||||
// ~ Methods
|
||||
// ========================================================================================================
|
||||
|
||||
/** |
||||
* Returns a 403 error code to the client. |
||||
* |
||||
* @param request DOCUMENT ME! |
||||
* @param response DOCUMENT ME! |
||||
* @param authException DOCUMENT ME! |
||||
* |
||||
* @throws IOException DOCUMENT ME! |
||||
* @throws ServletException DOCUMENT ME! |
||||
*/ |
||||
public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException) |
||||
throws IOException, ServletException { |
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("X509 entry point called. Rejecting access"); |
||||
} |
||||
|
||||
HttpServletResponse httpResponse = (HttpServletResponse) response; |
||||
httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Denied"); |
||||
} |
||||
} |
||||
@ -1,6 +0,0 @@
@@ -1,6 +0,0 @@
|
||||
<html> |
||||
<body> |
||||
This package is now deprecated and will be removed in a future version. |
||||
Use the X.509 authentication support in org.springframework.security.ui.preauth.x509 instead. |
||||
</body> |
||||
</html> |
||||
@ -1,29 +1,32 @@
@@ -1,29 +1,32 @@
|
||||
package org.springframework.security.vote; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.springframework.security.Authentication; |
||||
import org.springframework.security.GrantedAuthority; |
||||
import org.springframework.security.userdetails.hierarchicalroles.RoleHierarchy; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Extended RoleVoter which uses a {@link RoleHierarchy} definition to determine the |
||||
* Extended RoleVoter which uses a {@link RoleHierarchy} definition to determine the |
||||
* roles allocated to the current user before voting. |
||||
* |
||||
* |
||||
* @author Luke Taylor |
||||
* @since 2.0.4 |
||||
*/ |
||||
public class RoleHierarchyVoter extends RoleVoter { |
||||
private RoleHierarchy roleHierarchy = null; |
||||
|
||||
|
||||
public RoleHierarchyVoter(RoleHierarchy roleHierarchy) { |
||||
Assert.notNull(roleHierarchy, "RoleHierarchy must not be null"); |
||||
this.roleHierarchy = roleHierarchy; |
||||
Assert.notNull(roleHierarchy, "RoleHierarchy must not be null"); |
||||
this.roleHierarchy = roleHierarchy; |
||||
} |
||||
|
||||
/** |
||||
* Calls the <tt>RoleHierarchy</tt> to obtain the complete set of user authorities. |
||||
*/ |
||||
GrantedAuthority[] extractAuthorities(Authentication authentication) { |
||||
return roleHierarchy.getReachableGrantedAuthorities(authentication.getAuthorities()); |
||||
} |
||||
@Override |
||||
List<GrantedAuthority> extractAuthorities(Authentication authentication) { |
||||
return roleHierarchy.getReachableGrantedAuthorities(authentication.getAuthorities()); |
||||
} |
||||
} |
||||
|
||||
@ -1,232 +1,214 @@
@@ -1,232 +1,214 @@
|
||||
package org.springframework.security.authoritymapping; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Collection; |
||||
import java.util.HashMap; |
||||
|
||||
import junit.framework.TestCase; |
||||
import java.util.List; |
||||
|
||||
import org.apache.log4j.Level; |
||||
import org.apache.log4j.Logger; |
||||
import org.junit.Test; |
||||
import org.springframework.security.GrantedAuthority; |
||||
import org.springframework.security.GrantedAuthorityImpl; |
||||
|
||||
/** |
||||
* |
||||
* |
||||
* @author Ruud Senden |
||||
*/ |
||||
public class MapBasedAttributes2GrantedAuthoritiesMapperTest extends TestCase { |
||||
|
||||
protected void setUp() throws Exception { |
||||
// Set Log4j loglevel to debug to include all logstatements in tests
|
||||
Logger.getRootLogger().setLevel(Level.DEBUG); |
||||
} |
||||
|
||||
public final void testAfterPropertiesSetNoMap() { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
try { |
||||
mapper.afterPropertiesSet(); |
||||
fail("Expected exception not thrown"); |
||||
} catch (IllegalArgumentException expected) { |
||||
// Expected exception
|
||||
} catch (Exception unexpected) { |
||||
fail("Unexpected exception: " + unexpected); |
||||
} |
||||
} |
||||
|
||||
public final void testAfterPropertiesSetEmptyMap() { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
mapper.setAttributes2grantedAuthoritiesMap(new HashMap()); |
||||
try { |
||||
mapper.afterPropertiesSet(); |
||||
fail("Expected exception not thrown"); |
||||
} catch (IllegalArgumentException expected) { |
||||
// Expected exception
|
||||
} catch (Exception unexpected) { |
||||
fail("Unexpected exception: " + unexpected); |
||||
} |
||||
} |
||||
|
||||
public final void testAfterPropertiesSetInvalidKeyTypeMap() { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
HashMap m = new HashMap(); |
||||
m.put(new Object(),"ga1"); |
||||
mapper.setAttributes2grantedAuthoritiesMap(m); |
||||
try { |
||||
mapper.afterPropertiesSet(); |
||||
fail("Expected exception not thrown"); |
||||
} catch (IllegalArgumentException expected) { |
||||
// Expected exception
|
||||
} catch (Exception unexpected) { |
||||
fail("Unexpected exception: " + unexpected); |
||||
} |
||||
} |
||||
|
||||
public final void testAfterPropertiesSetInvalidValueTypeMap1() { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
HashMap m = new HashMap(); |
||||
m.put("role1",new Object()); |
||||
mapper.setAttributes2grantedAuthoritiesMap(m); |
||||
try { |
||||
mapper.afterPropertiesSet(); |
||||
fail("Expected exception not thrown"); |
||||
} catch (IllegalArgumentException expected) { |
||||
// Expected exception
|
||||
} catch (Exception unexpected) { |
||||
fail("Unexpected exception: " + unexpected); |
||||
} |
||||
} |
||||
|
||||
public final void testAfterPropertiesSetInvalidValueTypeMap2() { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
HashMap m = new HashMap(); |
||||
m.put("role1",new Object[]{new String[]{"ga1","ga2"}, new Object()}); |
||||
mapper.setAttributes2grantedAuthoritiesMap(m); |
||||
try { |
||||
mapper.afterPropertiesSet(); |
||||
fail("Expected exception not thrown"); |
||||
} catch (IllegalArgumentException expected) { |
||||
// Expected exception
|
||||
} catch (Exception unexpected) { |
||||
fail("Unexpected exception: " + unexpected); |
||||
} |
||||
} |
||||
|
||||
public final void testAfterPropertiesSetValidMap() { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
HashMap m = getValidAttributes2GrantedAuthoritiesMap(); |
||||
mapper.setAttributes2grantedAuthoritiesMap(m); |
||||
try { |
||||
mapper.afterPropertiesSet(); |
||||
} catch (Exception unexpected) { |
||||
fail("Unexpected exception: " + unexpected); |
||||
} |
||||
} |
||||
|
||||
public final void testMapping1() { |
||||
String[] roles = { "role1" }; |
||||
String[] expectedGas = { "ga1" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping2() { |
||||
String[] roles = { "role2" }; |
||||
String[] expectedGas = { "ga2" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping3() { |
||||
String[] roles = { "role3" }; |
||||
String[] expectedGas = { "ga3", "ga4" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping4() { |
||||
String[] roles = { "role4" }; |
||||
String[] expectedGas = { "ga5", "ga6" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping5() { |
||||
String[] roles = { "role5" }; |
||||
String[] expectedGas = { "ga7", "ga8", "ga9" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping6() { |
||||
String[] roles = { "role6" }; |
||||
String[] expectedGas = { "ga10", "ga11", "ga12" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping7() { |
||||
String[] roles = { "role7" }; |
||||
String[] expectedGas = { "ga13", "ga14" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping8() { |
||||
String[] roles = { "role8" }; |
||||
String[] expectedGas = { "ga13", "ga14" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping9() { |
||||
String[] roles = { "role9" }; |
||||
String[] expectedGas = {}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping10() { |
||||
String[] roles = { "role10" }; |
||||
String[] expectedGas = {}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMapping11() { |
||||
String[] roles = { "role11" }; |
||||
String[] expectedGas = {}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testNonExistingMapping() { |
||||
String[] roles = { "nonExisting" }; |
||||
String[] expectedGas = {}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
public final void testMappingCombination() { |
||||
String[] roles = { "role1", "role2", "role3", "role4", "role5", "role6", "role7", "role8", "role9", "role10", "role11" }; |
||||
String[] expectedGas = { "ga1", "ga2", "ga3", "ga4", "ga5", "ga6", "ga7", "ga8", "ga9", "ga10", "ga11", "ga12", "ga13", "ga14"}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
private HashMap getValidAttributes2GrantedAuthoritiesMap() { |
||||
HashMap m = new HashMap(); |
||||
m.put("role1","ga1"); |
||||
m.put("role2",new GrantedAuthorityImpl("ga2")); |
||||
m.put("role3",Arrays.asList(new Object[]{"ga3",new GrantedAuthorityImpl("ga4")})); |
||||
m.put("role4","ga5,ga6"); |
||||
m.put("role5",Arrays.asList(new Object[]{"ga7","ga8",new Object[]{new GrantedAuthorityImpl("ga9")}})); |
||||
m.put("role6",new Object[]{"ga10","ga11",new Object[]{new GrantedAuthorityImpl("ga12")}}); |
||||
m.put("role7",new String[]{"ga13","ga14"}); |
||||
m.put("role8",new String[]{"ga13","ga14",null}); |
||||
m.put("role9",null); |
||||
m.put("role10",new Object[]{}); |
||||
m.put("role11",Arrays.asList(new Object[]{null})); |
||||
return m; |
||||
} |
||||
|
||||
private MapBasedAttributes2GrantedAuthoritiesMapper getDefaultMapper() { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
mapper.setAttributes2grantedAuthoritiesMap(getValidAttributes2GrantedAuthoritiesMap()); |
||||
mapper.afterPropertiesSet(); |
||||
return mapper; |
||||
} |
||||
|
||||
private void testGetGrantedAuthorities(Attributes2GrantedAuthoritiesMapper mapper, String[] roles, String[] expectedGas) { |
||||
GrantedAuthority[] result = mapper.getGrantedAuthorities(roles); |
||||
Collection resultColl = new ArrayList(result.length); |
||||
for (int i = 0; i < result.length; i++) { |
||||
resultColl.add(result[i].getAuthority()); |
||||
} |
||||
Collection expectedColl = Arrays.asList(expectedGas); |
||||
assertTrue("Role collections do not match; result: " + resultColl + ", expected: " + expectedColl, expectedColl |
||||
.containsAll(resultColl) |
||||
&& resultColl.containsAll(expectedColl)); |
||||
} |
||||
public class MapBasedAttributes2GrantedAuthoritiesMapperTest { |
||||
|
||||
protected void setUp() throws Exception { |
||||
// Set Log4j loglevel to debug to include all logstatements in tests
|
||||
Logger.getRootLogger().setLevel(Level.DEBUG); |
||||
} |
||||
|
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testAfterPropertiesSetNoMap() throws Exception { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
mapper.afterPropertiesSet(); |
||||
} |
||||
|
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testAfterPropertiesSetEmptyMap() throws Exception { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
mapper.setAttributes2grantedAuthoritiesMap(new HashMap()); |
||||
mapper.afterPropertiesSet(); |
||||
} |
||||
|
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testAfterPropertiesSetInvalidKeyTypeMap() throws Exception { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
HashMap m = new HashMap(); |
||||
m.put(new Object(),"ga1"); |
||||
mapper.setAttributes2grantedAuthoritiesMap(m); |
||||
mapper.afterPropertiesSet(); |
||||
} |
||||
|
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testAfterPropertiesSetInvalidValueTypeMap1() throws Exception { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
HashMap m = new HashMap(); |
||||
m.put("role1",new Object()); |
||||
mapper.setAttributes2grantedAuthoritiesMap(m); |
||||
mapper.afterPropertiesSet(); |
||||
} |
||||
|
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testAfterPropertiesSetInvalidValueTypeMap2() throws Exception { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
HashMap m = new HashMap(); |
||||
m.put("role1",new Object[]{new String[]{"ga1","ga2"}, new Object()}); |
||||
mapper.setAttributes2grantedAuthoritiesMap(m); |
||||
mapper.afterPropertiesSet(); |
||||
} |
||||
|
||||
@Test |
||||
public void testAfterPropertiesSetValidMap() throws Exception { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
HashMap m = getValidAttributes2GrantedAuthoritiesMap(); |
||||
mapper.setAttributes2grantedAuthoritiesMap(m); |
||||
mapper.afterPropertiesSet(); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping1() throws Exception { |
||||
String[] roles = { "role1" }; |
||||
String[] expectedGas = { "ga1" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping2() throws Exception { |
||||
String[] roles = { "role2" }; |
||||
String[] expectedGas = { "ga2" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping3() throws Exception { |
||||
String[] roles = { "role3" }; |
||||
String[] expectedGas = { "ga3", "ga4" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping4() throws Exception { |
||||
String[] roles = { "role4" }; |
||||
String[] expectedGas = { "ga5", "ga6" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping5() throws Exception { |
||||
String[] roles = { "role5" }; |
||||
String[] expectedGas = { "ga7", "ga8", "ga9" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping6() throws Exception { |
||||
String[] roles = { "role6" }; |
||||
String[] expectedGas = { "ga10", "ga11", "ga12" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping7() throws Exception { |
||||
String[] roles = { "role7" }; |
||||
String[] expectedGas = { "ga13", "ga14" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping8() throws Exception { |
||||
String[] roles = { "role8" }; |
||||
String[] expectedGas = { "ga13", "ga14" }; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping9() throws Exception { |
||||
String[] roles = { "role9" }; |
||||
String[] expectedGas = {}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping10() throws Exception { |
||||
String[] roles = { "role10" }; |
||||
String[] expectedGas = {}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMapping11() throws Exception { |
||||
String[] roles = { "role11" }; |
||||
String[] expectedGas = {}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testNonExistingMapping() throws Exception { |
||||
String[] roles = { "nonExisting" }; |
||||
String[] expectedGas = {}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
@Test |
||||
public void testMappingCombination() throws Exception { |
||||
String[] roles = { "role1", "role2", "role3", "role4", "role5", "role6", "role7", "role8", "role9", "role10", "role11" }; |
||||
String[] expectedGas = { "ga1", "ga2", "ga3", "ga4", "ga5", "ga6", "ga7", "ga8", "ga9", "ga10", "ga11", "ga12", "ga13", "ga14"}; |
||||
Attributes2GrantedAuthoritiesMapper mapper = getDefaultMapper(); |
||||
testGetGrantedAuthorities(mapper, roles, expectedGas); |
||||
} |
||||
|
||||
private HashMap getValidAttributes2GrantedAuthoritiesMap() { |
||||
HashMap m = new HashMap(); |
||||
m.put("role1","ga1"); |
||||
m.put("role2",new GrantedAuthorityImpl("ga2")); |
||||
m.put("role3",Arrays.asList(new Object[]{"ga3",new GrantedAuthorityImpl("ga4")})); |
||||
m.put("role4","ga5,ga6"); |
||||
m.put("role5",Arrays.asList(new Object[]{"ga7","ga8",new Object[]{new GrantedAuthorityImpl("ga9")}})); |
||||
m.put("role6",new Object[]{"ga10","ga11",new Object[]{new GrantedAuthorityImpl("ga12")}}); |
||||
m.put("role7",new String[]{"ga13","ga14"}); |
||||
m.put("role8",new String[]{"ga13","ga14",null}); |
||||
m.put("role9",null); |
||||
m.put("role10",new Object[]{}); |
||||
m.put("role11",Arrays.asList(new Object[]{null})); |
||||
return m; |
||||
} |
||||
|
||||
private MapBasedAttributes2GrantedAuthoritiesMapper getDefaultMapper() throws Exception { |
||||
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper(); |
||||
mapper.setAttributes2grantedAuthoritiesMap(getValidAttributes2GrantedAuthoritiesMap()); |
||||
mapper.afterPropertiesSet(); |
||||
return mapper; |
||||
} |
||||
|
||||
private void testGetGrantedAuthorities(Attributes2GrantedAuthoritiesMapper mapper, String[] roles, String[] expectedGas) { |
||||
List<GrantedAuthority> result = mapper.getGrantedAuthorities(Arrays.asList(roles)); |
||||
Collection resultColl = new ArrayList(result.size()); |
||||
for (int i = 0; i < result.size(); i++) { |
||||
resultColl.add(result.get(i).getAuthority()); |
||||
} |
||||
Collection expectedColl = Arrays.asList(expectedGas); |
||||
assertTrue("Role collections should match; result: " + resultColl + ", expected: " + expectedColl, expectedColl |
||||
.containsAll(resultColl) |
||||
&& resultColl.containsAll(expectedColl)); |
||||
} |
||||
} |
||||
|
||||
@ -1,80 +1,77 @@
@@ -1,80 +1,77 @@
|
||||
package org.springframework.security.providers.preauth; |
||||
|
||||
import org.springframework.security.GrantedAuthoritiesContainer; |
||||
import org.springframework.security.GrantedAuthorityImpl; |
||||
import org.springframework.security.GrantedAuthority; |
||||
import org.springframework.security.userdetails.UserDetails; |
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collection; |
||||
import java.util.List; |
||||
|
||||
import junit.framework.TestCase; |
||||
import org.junit.Test; |
||||
import org.springframework.security.GrantedAuthoritiesContainer; |
||||
import org.springframework.security.GrantedAuthority; |
||||
import org.springframework.security.userdetails.UserDetails; |
||||
import org.springframework.security.util.AuthorityUtils; |
||||
|
||||
/** |
||||
* |
||||
* |
||||
* @author TSARDD |
||||
* @since 18-okt-2007 |
||||
*/ |
||||
public class PreAuthenticatedGrantedAuthoritiesUserDetailsServiceTests extends TestCase { |
||||
public class PreAuthenticatedGrantedAuthoritiesUserDetailsServiceTests { |
||||
|
||||
public final void testGetUserDetailsInvalidType() { |
||||
PreAuthenticatedGrantedAuthoritiesUserDetailsService svc = new PreAuthenticatedGrantedAuthoritiesUserDetailsService(); |
||||
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken("dummy", "dummy"); |
||||
token.setDetails(new Object()); |
||||
try { |
||||
svc.loadUserDetails(token); |
||||
fail("Expected exception didn't occur"); |
||||
} catch (IllegalArgumentException expected) { |
||||
} |
||||
} |
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testGetUserDetailsInvalidType() { |
||||
PreAuthenticatedGrantedAuthoritiesUserDetailsService svc = new PreAuthenticatedGrantedAuthoritiesUserDetailsService(); |
||||
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken("dummy", "dummy"); |
||||
token.setDetails(new Object()); |
||||
svc.loadUserDetails(token); |
||||
} |
||||
|
||||
public final void testGetUserDetailsNoDetails() { |
||||
PreAuthenticatedGrantedAuthoritiesUserDetailsService svc = new PreAuthenticatedGrantedAuthoritiesUserDetailsService(); |
||||
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken("dummy", "dummy"); |
||||
token.setDetails(null); |
||||
try { |
||||
svc.loadUserDetails(token); |
||||
fail("Expected exception didn't occur"); |
||||
} catch (IllegalArgumentException expected) { |
||||
} |
||||
} |
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testGetUserDetailsNoDetails() { |
||||
PreAuthenticatedGrantedAuthoritiesUserDetailsService svc = new PreAuthenticatedGrantedAuthoritiesUserDetailsService(); |
||||
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken("dummy", "dummy"); |
||||
token.setDetails(null); |
||||
svc.loadUserDetails(token); |
||||
} |
||||
|
||||
public final void testGetUserDetailsEmptyAuthorities() { |
||||
final String userName = "dummyUser"; |
||||
final GrantedAuthority[] gas = new GrantedAuthority[] {}; |
||||
testGetUserDetails(userName, gas); |
||||
} |
||||
@Test |
||||
public void testGetUserDetailsEmptyAuthorities() { |
||||
final String userName = "dummyUser"; |
||||
testGetUserDetails(userName, AuthorityUtils.NO_AUTHORITIES); |
||||
} |
||||
|
||||
public final void testGetUserDetailsWithAuthorities() { |
||||
final String userName = "dummyUser"; |
||||
final GrantedAuthority[] gas = new GrantedAuthority[] { new GrantedAuthorityImpl("Role1"), new GrantedAuthorityImpl("Role2") }; |
||||
testGetUserDetails(userName, gas); |
||||
} |
||||
@Test |
||||
public void testGetUserDetailsWithAuthorities() { |
||||
final String userName = "dummyUser"; |
||||
testGetUserDetails(userName, AuthorityUtils.createAuthorityList("Role1", "Role2")); |
||||
} |
||||
|
||||
private void testGetUserDetails(final String userName, final GrantedAuthority[] gas) { |
||||
PreAuthenticatedGrantedAuthoritiesUserDetailsService svc = new PreAuthenticatedGrantedAuthoritiesUserDetailsService(); |
||||
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(userName, "dummy"); |
||||
token.setDetails(new GrantedAuthoritiesContainer() { |
||||
public GrantedAuthority[] getGrantedAuthorities() { |
||||
return gas; |
||||
} |
||||
}); |
||||
UserDetails ud = svc.loadUserDetails(token); |
||||
assertTrue(ud.isAccountNonExpired()); |
||||
assertTrue(ud.isAccountNonLocked()); |
||||
assertTrue(ud.isCredentialsNonExpired()); |
||||
assertTrue(ud.isEnabled()); |
||||
assertEquals(ud.getUsername(), userName); |
||||
private void testGetUserDetails(final String userName, final List<GrantedAuthority> gas) { |
||||
PreAuthenticatedGrantedAuthoritiesUserDetailsService svc = new PreAuthenticatedGrantedAuthoritiesUserDetailsService(); |
||||
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(userName, "dummy"); |
||||
token.setDetails(new GrantedAuthoritiesContainer() { |
||||
public List<GrantedAuthority> getGrantedAuthorities() { |
||||
return gas; |
||||
} |
||||
}); |
||||
UserDetails ud = svc.loadUserDetails(token); |
||||
assertTrue(ud.isAccountNonExpired()); |
||||
assertTrue(ud.isAccountNonLocked()); |
||||
assertTrue(ud.isCredentialsNonExpired()); |
||||
assertTrue(ud.isEnabled()); |
||||
assertEquals(ud.getUsername(), userName); |
||||
|
||||
//Password is not saved by
|
||||
// PreAuthenticatedGrantedAuthoritiesUserDetailsService
|
||||
//assertEquals(ud.getPassword(),password);
|
||||
//Password is not saved by
|
||||
// PreAuthenticatedGrantedAuthoritiesUserDetailsService
|
||||
//assertEquals(ud.getPassword(),password);
|
||||
|
||||
Collection expectedColl = Arrays.asList(gas); |
||||
Collection resultColl = Arrays.asList(ud.getAuthorities()); |
||||
assertTrue("GrantedAuthority collections do not match; result: " + resultColl + ", expected: " + expectedColl, expectedColl |
||||
.containsAll(resultColl) |
||||
&& resultColl.containsAll(expectedColl)); |
||||
} |
||||
Collection expectedColl = Arrays.asList(gas); |
||||
Collection resultColl = Arrays.asList(ud.getAuthorities()); |
||||
assertTrue("GrantedAuthority collections do not match; result: " + resultColl + ", expected: " + expectedColl, expectedColl |
||||
.containsAll(resultColl) |
||||
&& resultColl.containsAll(expectedColl)); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -1,131 +0,0 @@
@@ -1,131 +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.x509; |
||||
|
||||
import junit.framework.TestCase; |
||||
|
||||
import org.springframework.security.Authentication; |
||||
import org.springframework.security.AuthenticationException; |
||||
import org.springframework.security.BadCredentialsException; |
||||
import org.springframework.security.GrantedAuthority; |
||||
import org.springframework.security.GrantedAuthorityImpl; |
||||
|
||||
import org.springframework.security.providers.UsernamePasswordAuthenticationToken; |
||||
|
||||
import org.springframework.security.userdetails.User; |
||||
import org.springframework.security.userdetails.UserDetails; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
|
||||
/** |
||||
* Tests {@link X509AuthenticationProvider} |
||||
* |
||||
* @author Luke Taylor |
||||
* @version $Id$ |
||||
*/ |
||||
public class X509AuthenticationProviderTests extends TestCase { |
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
public X509AuthenticationProviderTests() { |
||||
super(); |
||||
} |
||||
|
||||
public X509AuthenticationProviderTests(String arg0) { |
||||
super(arg0); |
||||
} |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public final void setUp() throws Exception { |
||||
super.setUp(); |
||||
} |
||||
|
||||
public void testAuthenticationIsNullWithUnsupportedToken() { |
||||
X509AuthenticationProvider provider = new X509AuthenticationProvider(); |
||||
Authentication request = new UsernamePasswordAuthenticationToken("dummy", "dummy"); |
||||
Authentication result = provider.authenticate(request); |
||||
assertNull(result); |
||||
} |
||||
|
||||
public void testFailsWithNullCertificate() { |
||||
X509AuthenticationProvider provider = new X509AuthenticationProvider(); |
||||
|
||||
provider.setX509AuthoritiesPopulator(new MockAuthoritiesPopulator(false)); |
||||
|
||||
try { |
||||
provider.authenticate(new X509AuthenticationToken(null)); |
||||
fail("Should have thrown BadCredentialsException"); |
||||
} catch (BadCredentialsException e) { |
||||
//ignore
|
||||
} |
||||
} |
||||
|
||||
public void testNormalOperation() throws Exception { |
||||
X509AuthenticationProvider provider = new X509AuthenticationProvider(); |
||||
|
||||
provider.setX509AuthoritiesPopulator(new MockAuthoritiesPopulator(false)); |
||||
provider.afterPropertiesSet(); |
||||
|
||||
Authentication result = provider.authenticate(X509TestUtils.createToken()); |
||||
|
||||
assertNotNull(result); |
||||
assertNotNull(result.getAuthorities()); |
||||
} |
||||
|
||||
public void testPopulatorRejectionCausesFailure() throws Exception { |
||||
X509AuthenticationProvider provider = new X509AuthenticationProvider(); |
||||
provider.setX509AuthoritiesPopulator(new MockAuthoritiesPopulator(true)); |
||||
|
||||
try { |
||||
provider.authenticate(X509TestUtils.createToken()); |
||||
fail("Should have thrown BadCredentialsException"); |
||||
} catch (BadCredentialsException e) { |
||||
//ignore
|
||||
} |
||||
} |
||||
|
||||
public void testRequiresPopulator() throws Exception { |
||||
X509AuthenticationProvider provider = new X509AuthenticationProvider(); |
||||
|
||||
try { |
||||
provider.afterPropertiesSet(); |
||||
fail("Should have thrown IllegalArgumentException"); |
||||
} catch (IllegalArgumentException failed) { |
||||
//ignored
|
||||
} |
||||
} |
||||
|
||||
//~ Inner Classes ==================================================================================================
|
||||
|
||||
public static class MockAuthoritiesPopulator implements X509AuthoritiesPopulator { |
||||
private boolean rejectCertificate; |
||||
|
||||
public MockAuthoritiesPopulator(boolean rejectCertificate) { |
||||
this.rejectCertificate = rejectCertificate; |
||||
} |
||||
|
||||
public UserDetails getUserDetails(X509Certificate userCertificate) |
||||
throws AuthenticationException { |
||||
if (rejectCertificate) { |
||||
throw new BadCredentialsException("Invalid Certificate"); |
||||
} |
||||
|
||||
return new User("user", "password", true, true, true, true, |
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A"), new GrantedAuthorityImpl("ROLE_B")}); |
||||
} |
||||
} |
||||
} |
||||
@ -1,52 +0,0 @@
@@ -1,52 +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.x509; |
||||
|
||||
import junit.framework.TestCase; |
||||
|
||||
|
||||
/** |
||||
* Tests for {@link X509AuthenticationToken}. |
||||
* |
||||
* @author Luke Taylor |
||||
* @version $Id$ |
||||
*/ |
||||
public class X509AuthenticationTokenTests extends TestCase { |
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
public X509AuthenticationTokenTests() {} |
||||
|
||||
public X509AuthenticationTokenTests(String s) { |
||||
super(s); |
||||
} |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void setUp() throws Exception { |
||||
super.setUp(); |
||||
} |
||||
|
||||
public void testAuthenticated() throws Exception { |
||||
X509AuthenticationToken token = X509TestUtils.createToken(); |
||||
assertTrue(!token.isAuthenticated()); |
||||
token.setAuthenticated(true); |
||||
assertTrue(token.isAuthenticated()); |
||||
} |
||||
|
||||
public void testEquals() throws Exception { |
||||
assertEquals(X509TestUtils.createToken(), X509TestUtils.createToken()); |
||||
} |
||||
} |
||||
@ -1,89 +0,0 @@
@@ -1,89 +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.x509.cache; |
||||
|
||||
import net.sf.ehcache.Ehcache; |
||||
import net.sf.ehcache.CacheManager; |
||||
import net.sf.ehcache.Cache; |
||||
|
||||
import org.springframework.security.GrantedAuthority; |
||||
import org.springframework.security.GrantedAuthorityImpl; |
||||
|
||||
import org.springframework.security.providers.x509.X509TestUtils; |
||||
|
||||
import org.springframework.security.userdetails.User; |
||||
import org.springframework.security.userdetails.UserDetails; |
||||
|
||||
|
||||
import org.junit.BeforeClass; |
||||
import org.junit.AfterClass; |
||||
import org.junit.Test; |
||||
import static org.junit.Assert.*; |
||||
|
||||
|
||||
/** |
||||
* Tests for {@link EhCacheBasedX509UserCache}. |
||||
* |
||||
* @author Luke Taylor |
||||
* @version $Id$ |
||||
*/ |
||||
public class EhCacheBasedX509UserCacheTests { |
||||
private static CacheManager cacheManager; |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
@BeforeClass |
||||
public static void initCacheManaer() { |
||||
cacheManager = new CacheManager(); |
||||
cacheManager.addCache(new Cache("x509cachetests", 500, false, false, 30, 30)); |
||||
} |
||||
|
||||
@AfterClass |
||||
public static void shutdownCacheManager() { |
||||
cacheManager.removalAll(); |
||||
cacheManager.shutdown(); |
||||
} |
||||
|
||||
private Ehcache getCache() { |
||||
Ehcache cache = cacheManager.getCache("x509cachetests"); |
||||
cache.removeAll(); |
||||
|
||||
return cache; |
||||
} |
||||
|
||||
private UserDetails getUser() { |
||||
return new User("rod", "password", true, true, true, true, |
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")}); |
||||
} |
||||
|
||||
@Test |
||||
public void cacheOperationsAreSucessful() throws Exception { |
||||
EhCacheBasedX509UserCache cache = new EhCacheBasedX509UserCache(); |
||||
cache.setCache(getCache()); |
||||
cache.afterPropertiesSet(); |
||||
|
||||
// Check it gets stored in the cache
|
||||
cache.putUserInCache(X509TestUtils.buildTestCertificate(), getUser()); |
||||
assertEquals(getUser().getPassword(), cache.getUserFromCache(X509TestUtils.buildTestCertificate()).getPassword()); |
||||
|
||||
// Check it gets removed from the cache
|
||||
cache.removeUserFromCache(X509TestUtils.buildTestCertificate()); |
||||
assertNull(cache.getUserFromCache(X509TestUtils.buildTestCertificate())); |
||||
|
||||
// Check it doesn't return values for null user
|
||||
assertNull(cache.getUserFromCache(null)); |
||||
} |
||||
} |
||||
@ -1,146 +0,0 @@
@@ -1,146 +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.x509.populator; |
||||
|
||||
import junit.framework.TestCase; |
||||
|
||||
import org.springframework.security.BadCredentialsException; |
||||
import org.springframework.security.GrantedAuthority; |
||||
import org.springframework.security.GrantedAuthorityImpl; |
||||
|
||||
import org.springframework.security.providers.x509.X509TestUtils; |
||||
|
||||
import org.springframework.security.userdetails.User; |
||||
import org.springframework.security.userdetails.UserDetails; |
||||
import org.springframework.security.userdetails.UserDetailsService; |
||||
import org.springframework.security.userdetails.UsernameNotFoundException; |
||||
|
||||
import org.springframework.dao.DataAccessException; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
|
||||
/** |
||||
* Tests for {@link DaoX509AuthoritiesPopulator} |
||||
* |
||||
* @author Luke Taylor |
||||
* @version $Id$ |
||||
*/ |
||||
public class DaoX509AuthoritiesPopulatorTests extends TestCase { |
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
public DaoX509AuthoritiesPopulatorTests() { |
||||
} |
||||
|
||||
public DaoX509AuthoritiesPopulatorTests(String arg0) { |
||||
super(arg0); |
||||
} |
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public final void setUp() throws Exception { |
||||
super.setUp(); |
||||
} |
||||
|
||||
public void testDefaultCNPatternMatch() throws Exception { |
||||
X509Certificate cert = X509TestUtils.buildTestCertificate(); |
||||
DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator(); |
||||
|
||||
populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail()); |
||||
populator.afterPropertiesSet(); |
||||
populator.getUserDetails(cert); |
||||
} |
||||
|
||||
public void testEmailPatternMatch() throws Exception { |
||||
X509Certificate cert = X509TestUtils.buildTestCertificate(); |
||||
DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator(); |
||||
|
||||
populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail()); |
||||
populator.setSubjectDNRegex("emailAddress=(.*?),"); |
||||
populator.afterPropertiesSet(); |
||||
populator.getUserDetails(cert); |
||||
} |
||||
|
||||
public void testInvalidRegexFails() throws Exception { |
||||
DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator(); |
||||
populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail()); |
||||
populator.setSubjectDNRegex("CN=(.*?,"); // missing closing bracket on group
|
||||
|
||||
try { |
||||
populator.afterPropertiesSet(); |
||||
fail("Should have thrown IllegalArgumentException"); |
||||
} catch (IllegalArgumentException failed) { |
||||
// ignored
|
||||
} |
||||
} |
||||
|
||||
public void testMatchOnShoeSizeFieldInDNFails() throws Exception { |
||||
X509Certificate cert = X509TestUtils.buildTestCertificate(); |
||||
DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator(); |
||||
|
||||
populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail()); |
||||
populator.setSubjectDNRegex("shoeSize=(.*?),"); |
||||
populator.afterPropertiesSet(); |
||||
|
||||
try { |
||||
populator.getUserDetails(cert); |
||||
fail("Should have thrown BadCredentialsException."); |
||||
} catch (BadCredentialsException failed) { |
||||
// ignored
|
||||
} |
||||
} |
||||
|
||||
public void testPatternWithNoGroupFails() throws Exception { |
||||
X509Certificate cert = X509TestUtils.buildTestCertificate(); |
||||
DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator(); |
||||
|
||||
populator.setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail()); |
||||
populator.setSubjectDNRegex("CN=.*?,"); |
||||
populator.afterPropertiesSet(); |
||||
|
||||
try { |
||||
populator.getUserDetails(cert); |
||||
fail("Should have thrown IllegalArgumentException for regexp without group"); |
||||
} catch (IllegalArgumentException e) { |
||||
// ignored
|
||||
} |
||||
} |
||||
|
||||
public void testRequiresDao() throws Exception { |
||||
DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator(); |
||||
|
||||
try { |
||||
populator.afterPropertiesSet(); |
||||
fail("Should have thrown IllegalArgumentException"); |
||||
} catch (IllegalArgumentException failed) { |
||||
// ignored
|
||||
} |
||||
} |
||||
|
||||
//~ Inner Classes ==================================================================================================
|
||||
|
||||
private class MockAuthenticationDaoMatchesNameOrEmail implements UserDetailsService { |
||||
public UserDetails loadUserByUsername(String username) |
||||
throws UsernameNotFoundException, DataAccessException { |
||||
if ("Luke Taylor".equals(username) || "luke@monkeymachine".equals(username)) { |
||||
return new User("luke", "monkey", true, true, true, true, |
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE")}); |
||||
} else { |
||||
throw new UsernameNotFoundException("Could not find: " + username); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -1,68 +1,65 @@
@@ -1,68 +1,65 @@
|
||||
package org.springframework.security.ui.preauth; |
||||
|
||||
import org.springframework.security.GrantedAuthorityImpl; |
||||
import org.springframework.security.GrantedAuthority; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collection; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import junit.framework.TestCase; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.mock.web.MockHttpServletRequest; |
||||
import org.springframework.security.GrantedAuthority; |
||||
import org.springframework.security.util.AuthorityUtils; |
||||
|
||||
/** |
||||
* @author TSARDD |
||||
*/ |
||||
public class PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetailsTests extends TestCase { |
||||
public class PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetailsTests { |
||||
List<GrantedAuthority> gas = AuthorityUtils.createAuthorityList("Role1", "Role2"); |
||||
|
||||
@Test |
||||
public void testToString() { |
||||
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails( |
||||
getRequest("testUser", new String[] {})); |
||||
details.setGrantedAuthorities(gas); |
||||
String toString = details.toString(); |
||||
assertTrue("toString should contain Role1", toString.contains("Role1")); |
||||
assertTrue("toString should contain Role2", toString.contains("Role2")); |
||||
} |
||||
|
||||
@Test |
||||
public void testGetSetPreAuthenticatedGrantedAuthorities() { |
||||
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails( |
||||
getRequest("testUser", new String[] {})); |
||||
|
||||
public final void testToString() { |
||||
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails( |
||||
getRequest("testUser", new String[] {})); |
||||
GrantedAuthority[] gas = new GrantedAuthority[] { new GrantedAuthorityImpl("Role1"), new GrantedAuthorityImpl("Role2") }; |
||||
details.setGrantedAuthorities(gas); |
||||
String toString = details.toString(); |
||||
assertTrue("toString should contain Role1", toString.contains("Role1")); |
||||
assertTrue("toString should contain Role2", toString.contains("Role2")); |
||||
} |
||||
Collection expectedGas = Arrays.asList(gas); |
||||
|
||||
public final void testGetSetPreAuthenticatedGrantedAuthorities() { |
||||
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails( |
||||
getRequest("testUser", new String[] {})); |
||||
GrantedAuthority[] gas = new GrantedAuthority[] { new GrantedAuthorityImpl("Role1"), new GrantedAuthorityImpl("Role2") }; |
||||
Collection expectedGas = Arrays.asList(gas); |
||||
details.setGrantedAuthorities(gas); |
||||
Collection returnedGas = Arrays.asList(details.getGrantedAuthorities()); |
||||
assertTrue("Collections do not contain same elements; expected: " + expectedGas + ", returned: " + returnedGas, |
||||
expectedGas.containsAll(returnedGas) && returnedGas.containsAll(expectedGas)); |
||||
} |
||||
|
||||
details.setGrantedAuthorities(gas); |
||||
Collection returnedGas = Arrays.asList(details.getGrantedAuthorities()); |
||||
assertTrue("Collections do not contain same elements; expected: " + expectedGas + ", returned: " + returnedGas, |
||||
expectedGas.containsAll(returnedGas) && returnedGas.containsAll(expectedGas)); |
||||
} |
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testGetWithoutSetPreAuthenticatedGrantedAuthorities() { |
||||
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails( |
||||
getRequest("testUser", new String[] {})); |
||||
List<GrantedAuthority> gas = details.getGrantedAuthorities(); |
||||
} |
||||
|
||||
public final void testGetWithoutSetPreAuthenticatedGrantedAuthorities() { |
||||
PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails details = new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails( |
||||
getRequest("testUser", new String[] {})); |
||||
try { |
||||
GrantedAuthority[] gas = details.getGrantedAuthorities(); |
||||
fail("Expected exception didn't occur"); |
||||
} catch (IllegalArgumentException expected) { |
||||
} catch (Exception unexpected) { |
||||
fail("Unexpected exception: " + unexpected.toString()); |
||||
} |
||||
} |
||||
|
||||
private final HttpServletRequest getRequest(final String userName,final String[] aRoles) |
||||
{ |
||||
MockHttpServletRequest req = new MockHttpServletRequest() { |
||||
private Set roles = new HashSet(Arrays.asList(aRoles)); |
||||
public boolean isUserInRole(String arg0) { |
||||
return roles.contains(arg0); |
||||
} |
||||
}; |
||||
req.setRemoteUser(userName); |
||||
return req; |
||||
} |
||||
private HttpServletRequest getRequest(final String userName,final String[] aRoles) { |
||||
MockHttpServletRequest req = new MockHttpServletRequest() { |
||||
private Set roles = new HashSet(Arrays.asList(aRoles)); |
||||
public boolean isUserInRole(String arg0) { |
||||
return roles.contains(arg0); |
||||
} |
||||
}; |
||||
req.setRemoteUser(userName); |
||||
return req; |
||||
} |
||||
|
||||
} |
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue