/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.search.server;
import org.apache.log4j.Logger;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.AuthorityUtils;
import com.perforce.p4java.extension.exception.ServerAccessException;
import com.perforce.p4java.extension.exception.ServerConnectionException;
import com.perforce.p4java.extension.exception.ServerErrorException;
import com.perforce.p4java.extension.server.PerforcePrincipalCredential;
import com.perforce.p4java.extension.server.Server;
import com.perforce.search.model.impl.ErrorModels;
/**
* Used by the Spring Security AuthenticationManager to authenticate requests
* against the P4D credential store.
*/
public class PerforceAuthenticationProvider implements AuthenticationProvider {
private static final Logger log = Logger
.getLogger(PerforceAuthenticationProvider.class);
private ServerConnectionPoolFacade pool;
/**
* @see org.springframework.security.authentication.AuthenticationProvider#authenticate(org.springframework.security.core.Authentication)
*/
public Authentication authenticate(final Authentication authentication)
throws AuthenticationException {
// TODO: clean the name up for perforce?
String username = authentication.getName();
String password = (String) authentication.getCredentials();
Server server = null;
try {
server = pool.acquireTicketed(PerforcePrincipalCredential
.createUser(username, password));
} catch (ServerAccessException e) {
log.error(e);
throw new BadCredentialsException(
ErrorModels.Error_AccessException.getMessage(), e);
} catch (ServerConnectionException e) {
log.error(e);
throw new BadCredentialsException(
ErrorModels.Error_ConnectionException.getMessage(), e);
} catch (ServerErrorException e) {
log.error(e);
throw new BadCredentialsException(
ErrorModels.Error_ServerErrorException.getMessage(), e);
} finally {
if (server != null) {
pool.release(server);
}
}
// TODO: need authorities
return new UsernamePasswordAuthenticationToken(username, password,
AuthorityUtils.createAuthorityList("ROLE_USER"));
}
/**
* @see org.springframework.security.authentication.AuthenticationProvider#supports(java.lang.Class)
*/
public boolean supports(final Class<? extends Object> authentication) {
return (UsernamePasswordAuthenticationToken.class
.isAssignableFrom(authentication));
}
/**
* @return the pool
*/
public ServerConnectionPoolFacade getPool() {
return pool;
}
/**
* @param pool
* the pool to set
*/
public void setPool(final ServerConnectionPoolFacade pool) {
this.pool = pool;
}
}