Search in sources :

Example 1 with InvalidCredentialsException

use of com.google.gerrit.server.auth.InvalidCredentialsException 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)

Example 2 with InvalidCredentialsException

use of com.google.gerrit.server.auth.InvalidCredentialsException 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().isPresent() || !req.getPassword().isPresent()) {
        throw new MissingCredentialsException();
    }
    String username = lowerCaseUsername ? req.getUsername().map(u -> u.toLowerCase(Locale.US)).get() : req.getUsername().get();
    try {
        final DirContext ctx;
        if (authConfig.getAuthType() == AuthType.LDAP_BIND) {
            ctx = helper.authenticate(username, req.getPassword().get());
        } 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.close(helper.authenticate(m.getDN(), req.getPassword().get()));
            }
            return new AuthUser(AuthUser.UUID.create(username), username);
        } finally {
            helper.close(ctx);
        }
    } catch (AccountException e) {
        logger.atSevere().withCause(e).log("Cannot query LDAP to authenticate user");
        throw new InvalidCredentialsException("Cannot query LDAP for account", e);
    } catch (IOException | NamingException e) {
        logger.atSevere().withCause(e).log("Cannot query LDAP to authenticate user");
        throw new AuthException("Cannot query LDAP for account", e);
    } catch (LoginException e) {
        logger.atSevere().withCause(e).log("Cannot authenticate server via JAAS");
        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) IOException(java.io.IOException) 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)2 AuthException (com.google.gerrit.server.auth.AuthException)2 AuthUser (com.google.gerrit.server.auth.AuthUser)2 InvalidCredentialsException (com.google.gerrit.server.auth.InvalidCredentialsException)2 MissingCredentialsException (com.google.gerrit.server.auth.MissingCredentialsException)2 NamingException (javax.naming.NamingException)2 DirContext (javax.naming.directory.DirContext)2 LoginException (javax.security.auth.login.LoginException)2 IOException (java.io.IOException)1