Search in sources :

Example 1 with AuthUser

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);
    }
}
Also used : AuthException(com.google.gerrit.server.auth.AuthException) AuthUser(com.google.gerrit.server.auth.AuthUser) DirContext(javax.naming.directory.DirContext) AccountException(com.google.gerrit.server.account.AccountException) InvalidCredentialsException(com.google.gerrit.server.auth.InvalidCredentialsException) LoginException(javax.security.auth.login.LoginException) MissingCredentialsException(com.google.gerrit.server.auth.MissingCredentialsException) NamingException(javax.naming.NamingException)

Aggregations

AccountException (com.google.gerrit.server.account.AccountException)1 AuthException (com.google.gerrit.server.auth.AuthException)1 AuthUser (com.google.gerrit.server.auth.AuthUser)1 InvalidCredentialsException (com.google.gerrit.server.auth.InvalidCredentialsException)1 MissingCredentialsException (com.google.gerrit.server.auth.MissingCredentialsException)1 NamingException (javax.naming.NamingException)1 DirContext (javax.naming.directory.DirContext)1 LoginException (javax.security.auth.login.LoginException)1