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

                                        

                             

                                           
                                                        
                                                     
                                                             
                                                          


                                                   
 
   
                                                      
   

                          
 
                                                                                

           
                                                         
           


                          
                                       
                                           
           
                                                                                                                                    
         
                                        
 
                                                        
 

                               
                                                                   
                                            
                                                                                                                                 
                                                                

                                                                 
                                                                                                                                    
                 
                                                            
 
                                         


          
package org.openslx.imagemaster.session;

import java.sql.SQLException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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 (mysql here)
 */
public class Authenticator
{

	private static Logger log = LogManager.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();
	}
	//
}