use of com.google.gerrit.server.auth.AuthUser in project gerrit by GerritCodeReview.
the class LdapAuthBackend method authenticate.
@Override
public AuthUser authenticate(AuthRequest req) throws MissingCredentialsException, InvalidCredentialsException, UnknownUserException, UserNotAllowedException, AuthException {
if (req.getUsername() == null) {
throw new MissingCredentialsException();
}
final String username = lowerCaseUsername ? req.getUsername().toLowerCase(Locale.US) : req.getUsername();
try {
final DirContext ctx;
if (authConfig.getAuthType() == AuthType.LDAP_BIND) {
ctx = helper.authenticate(username, req.getPassword());
} else {
ctx = helper.open();
}
try {
final Helper.LdapSchema schema = helper.getSchema(ctx);
final LdapQuery.Result m = helper.findAccount(schema, ctx, username, false);
if (authConfig.getAuthType() == AuthType.LDAP) {
// We found the user account, but we need to verify
// the password matches it before we can continue.
//
helper.authenticate(m.getDN(), req.getPassword()).close();
}
return new AuthUser(AuthUser.UUID.create(username), username);
} finally {
try {
ctx.close();
} catch (NamingException e) {
log.warn("Cannot close LDAP query handle", e);
}
}
} catch (AccountException e) {
log.error("Cannot query LDAP to authenticate user", e);
throw new InvalidCredentialsException("Cannot query LDAP for account", e);
} catch (NamingException e) {
log.error("Cannot query LDAP to authenticate user", e);
throw new AuthException("Cannot query LDAP for account", e);
} catch (LoginException e) {
log.error("Cannot authenticate server via JAAS", e);
throw new AuthException("Cannot query LDAP for account", e);
}
}
Aggregations