/*
* Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license
* distributed with this file and available online at
* http://www.uportal.org/license.html
*/
package org.jasig.cas.authentication;
import java.util.Map;
import org.jasig.cas.authentication.handler.AuthenticationException;
import org.jasig.cas.authentication.handler.AuthenticationHandler;
import org.jasig.cas.authentication.handler.BadCredentialsAuthenticationException;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver;
import org.jasig.cas.authentication.principal.Principal;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
* Authentication Manager that provides a direct mapping between credentials
* provided and the authentication handler used to authenticate the user.
*
* @author Scott Battaglia
* @version $Revision: 23038 $ $Date: 2011-02-26 23:16:55 -0500 (Sat, 26 Feb 2011) $
* @since 3.1
*/
public final class DirectMappingAuthenticationManagerImpl extends AbstractAuthenticationManager {
@NotNull
@Size(min=1)
private Map<Class< ? extends Credentials>, DirectAuthenticationHandlerMappingHolder> credentialsMapping;
/**
* @throws IllegalArgumentException if a mapping cannot be found.
* @see org.jasig.cas.authentication.AuthenticationManager#authenticate(org.jasig.cas.authentication.principal.Credentials)
*/
@Override
protected Pair<AuthenticationHandler, Principal> authenticateAndObtainPrincipal(final Credentials credentials) throws AuthenticationException {
final Class< ? extends Credentials> credentialsClass = credentials.getClass();
final DirectAuthenticationHandlerMappingHolder d = this.credentialsMapping.get(credentialsClass);
Assert.notNull(d, "no mapping found for: " + credentialsClass.getName());
if (!d.getAuthenticationHandler().authenticate(credentials)) {
throw new BadCredentialsAuthenticationException();
}
final Principal p = d.getCredentialsToPrincipalResolver().resolvePrincipal(credentials);
return new Pair<AuthenticationHandler,Principal>(d.getAuthenticationHandler(), p);
}
public final void setCredentialsMapping(
final Map<Class< ? extends Credentials>, DirectAuthenticationHandlerMappingHolder> credentialsMapping) {
this.credentialsMapping = credentialsMapping;
}
public static final class DirectAuthenticationHandlerMappingHolder {
private AuthenticationHandler authenticationHandler;
private CredentialsToPrincipalResolver credentialsToPrincipalResolver;
public DirectAuthenticationHandlerMappingHolder() {
// nothing to do
}
public final AuthenticationHandler getAuthenticationHandler() {
return this.authenticationHandler;
}
public void setAuthenticationHandler(
final AuthenticationHandler authenticationHandler) {
this.authenticationHandler = authenticationHandler;
}
public CredentialsToPrincipalResolver getCredentialsToPrincipalResolver() {
return this.credentialsToPrincipalResolver;
}
public void setCredentialsToPrincipalResolver(
final CredentialsToPrincipalResolver credentialsToPrincipalResolver) {
this.credentialsToPrincipalResolver = credentialsToPrincipalResolver;
}
}
}