blob: 33786622004fd66fd1c6bbd34fc42eb13dd07b05 (
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
53
54
|
package org.openslx.imagemaster.session;
import org.apache.log4j.Logger;
import org.openslx.imagemaster.db.DbSatellite;
import org.openslx.imagemaster.db.DbUser;
import org.openslx.imagemaster.db.LdapUser;
import org.openslx.imagemaster.thrift.iface.AuthenticationError;
import org.openslx.imagemaster.thrift.iface.AuthenticationException;
public class Authenticator
{
private static Logger log = Logger.getLogger( Authenticator.class );
/**
* Authenticate the user against whatever backend
*
* @param username
* @param password
* @return
* @throws AuthenticationException
*/
public static User authenticate( String username, String password ) throws AuthenticationException
{
// DbUser user = DbUser.forLogin( username );
// if ( user == null || !Sha512Crypt.verifyPassword( password, user.password ) ) {
// log.debug( "Login failed: " + username );
// throw new AuthenticationException( AuthenticationError.INVALID_CREDENTIALS, "Invalid username or password!" );
// }
// log.debug( "Login successful: " + username );
String prefix = "";
if (username.split( "@" ).length == 2) {
// we are in userid@organization format
// --> get prefix
DbSatellite satellite = DbSatellite.fromOrganization( username.split( "@" )[1] );
if (satellite == null)
throw new AuthenticationException( AuthenticationError.INVALID_CREDENTIALS, "Unkown Organization" );
prefix = satellite.getPrefix();
}
LdapUser user = LdapUser.forLogin( prefix + "_" + username.split( "@" )[0], password ); // throws exception if credentials are invalid
if ( user == null ) {
log.debug( "Login failed: " + username );
throw new AuthenticationException( AuthenticationError.GENERIC_ERROR, "Could not login because of a weird error." );
}
log.debug( "Login succesful: " + username );
// if successfull: update/insert into db
DbUser.insertOrUpdate( user );
return user;
}
//
}
|