summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/session/Authenticator.java
blob: c11e59765ff91f03c718d1c5bc7316ef8575cc7a (plain) (tree)
1
2
3
4
5
6
7
8
9

                                        

                             
                               
                                                        
                                                     
                                                             
                                                          


                                                   
 


                                                     

                          
 


                                                                            
                                                         
           


                          
                                       
                                           
           
                                                                                                                                    
         
                                        
 
                                                        
 

                               
                                                                   
                                            
                                                                                                                                 
                                                                

                                                                 
                                                                                                                                    
                 
                                                            
 
                                         


          
package org.openslx.imagemaster.session;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.AuthorizationError;
import org.openslx.bwlp.thrift.iface.InvocationError;
import org.openslx.bwlp.thrift.iface.TAuthorizationException;
import org.openslx.bwlp.thrift.iface.TInvocationException;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.imagemaster.db.mappers.DbUser;
import org.openslx.imagemaster.db.models.LocalUser;

/**
 * Authenticates a user against a backend (ldap here)
 */
public class Authenticator
{

	private static Logger log = Logger.getLogger( Authenticator.class );

	/**
	 * Authenticate the user against whatever backend
	 * 
	 * @param username
	 * @param password
	 * @return
	 * @throws TInvocationException
	 * @throws TAuthenticationException
	 */
	public static UserInfo authenticate( String username, String password ) throws TAuthorizationException, TInvocationException
	{
		String login = username;

		log.info( "Logging in with: " + login );

		LocalUser user;
		try {
			user = DbUser.forUserId( login, password );
		} catch ( SQLException e ) {
			throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Could not connect to database" );
		} // throws exception if credentials are invalid
		if ( user == null ) {
			log.debug( "Login failed: " + username );
			throw new TAuthorizationException( AuthorizationError.INVALID_CREDENTIALS, "Invalid Username or password" );
		}
		log.debug( "Login succesful: " + username );

		return user.toUserInfo();
	}
	//
}