summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/session/Authenticator.java
blob: c11e59765ff91f03c718d1c5bc7316ef8575cc7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
	}
	//
}