Search in sources :

Example 1 with UsernameNotFoundException

use of io.gravitee.am.common.exception.authentication.UsernameNotFoundException in project gravitee-access-management by gravitee-io.

the class InlineAuthenticationProviderTest method shouldLoadUserByUsername_authentication_usernameNotFound.

@Test
public void shouldLoadUserByUsername_authentication_usernameNotFound() {
    Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn("username");
    when(userDetailsService.loadUserByUsername("username")).thenReturn(Maybe.error(new UsernameNotFoundException("username")));
    TestObserver<User> testObserver = inlineAuthenticationProvider.loadUserByUsername(authentication).test();
    testObserver.assertError(UsernameNotFoundException.class);
}
Also used : UsernameNotFoundException(io.gravitee.am.common.exception.authentication.UsernameNotFoundException) User(io.gravitee.am.identityprovider.api.User) Authentication(io.gravitee.am.identityprovider.api.Authentication) Test(org.junit.Test)

Example 2 with UsernameNotFoundException

use of io.gravitee.am.common.exception.authentication.UsernameNotFoundException in project gravitee-access-management by gravitee-io.

the class JdbcAuthenticationProvider method loadUserByUsername.

@Override
public Maybe<User> loadUserByUsername(Authentication authentication) {
    final String username = authentication.getPrincipal().toString();
    final String presentedPassword = authentication.getCredentials().toString();
    return selectUserByMultipleField(username).toList().flatMapPublisher(users -> {
        if (users.isEmpty()) {
            return Flowable.error(new UsernameNotFoundException(username));
        }
        return Flowable.fromIterable(users);
    }).filter(result -> {
        // check password
        String password = String.valueOf(result.get(configuration.getPasswordAttribute()));
        if (password == null) {
            LOGGER.debug("Authentication failed: password is null");
            return false;
        }
        if (configuration.isUseDedicatedSalt()) {
            String hash = String.valueOf(result.get(configuration.getPasswordSaltAttribute()));
            if (!passwordEncoder.matches(presentedPassword, password, hash)) {
                LOGGER.debug("Authentication failed: password does not match stored value");
                return false;
            }
        } else {
            if (!passwordEncoder.matches(presentedPassword, password)) {
                LOGGER.debug("Authentication failed: password does not match stored value");
                return false;
            }
        }
        return true;
    }).toList().flatMapMaybe(users -> {
        if (users.isEmpty()) {
            return Maybe.error(new BadCredentialsException("Bad credentials"));
        }
        if (users.size() > 1) {
            return Maybe.error(new BadCredentialsException("Bad credentials"));
        }
        return Maybe.just(createUser(authentication.getContext(), users.get(0)));
    });
}
Also used : StandardClaims(io.gravitee.am.common.oidc.StandardClaims) JdbcAuthenticationProviderConfiguration(io.gravitee.am.identityprovider.jdbc.authentication.spring.JdbcAuthenticationProviderConfiguration) Completable(io.reactivex.Completable) ColumnMapRowMapper(io.gravitee.am.identityprovider.jdbc.utils.ColumnMapRowMapper) ParametersUtils(io.gravitee.am.identityprovider.jdbc.utils.ParametersUtils) Maybe(io.reactivex.Maybe) Autowired(org.springframework.beans.factory.annotation.Autowired) Import(org.springframework.context.annotation.Import) HashMap(java.util.HashMap) BadCredentialsException(io.gravitee.am.common.exception.authentication.BadCredentialsException) List(java.util.List) Flowable(io.reactivex.Flowable) Map(java.util.Map) io.gravitee.am.identityprovider.api(io.gravitee.am.identityprovider.api) UsernameNotFoundException(io.gravitee.am.common.exception.authentication.UsernameNotFoundException) JdbcAbstractProvider(io.gravitee.am.identityprovider.jdbc.JdbcAbstractProvider) Statement(io.r2dbc.spi.Statement) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) UsernameNotFoundException(io.gravitee.am.common.exception.authentication.UsernameNotFoundException) BadCredentialsException(io.gravitee.am.common.exception.authentication.BadCredentialsException)

Example 3 with UsernameNotFoundException

use of io.gravitee.am.common.exception.authentication.UsernameNotFoundException in project gravitee-access-management by gravitee-io.

the class MongoAuthenticationProvider method loadUserByUsername.

public Maybe<User> loadUserByUsername(Authentication authentication) {
    String username = ((String) authentication.getPrincipal()).toLowerCase();
    return findUserByMultipleField(username).toList().flatMapPublisher(users -> {
        if (users.isEmpty()) {
            return Flowable.error(new UsernameNotFoundException(username));
        }
        return Flowable.fromIterable(users);
    }).filter(user -> {
        String password = user.getString(this.configuration.getPasswordField());
        String presentedPassword = authentication.getCredentials().toString();
        if (password == null) {
            LOGGER.debug("Authentication failed: password is null");
            return false;
        }
        if (configuration.isUseDedicatedSalt()) {
            String hash = user.getString(configuration.getPasswordSaltAttribute());
            if (!passwordEncoder.matches(presentedPassword, password, hash)) {
                LOGGER.debug("Authentication failed: password does not match stored value");
                return false;
            }
        } else {
            if (!passwordEncoder.matches(presentedPassword, password)) {
                LOGGER.debug("Authentication failed: password does not match stored value");
                return false;
            }
        }
        return true;
    }).toList().flatMapMaybe(users -> {
        if (users.isEmpty()) {
            return Maybe.error(new BadCredentialsException("Bad credentials"));
        }
        if (users.size() > 1) {
            return Maybe.error(new BadCredentialsException("Bad credentials"));
        }
        return Maybe.just(this.createUser(authentication.getContext(), users.get(0)));
    });
}
Also used : Document(org.bson.Document) StandardClaims(io.gravitee.am.common.oidc.StandardClaims) Logger(org.slf4j.Logger) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Import(org.springframework.context.annotation.Import) HashMap(java.util.HashMap) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) MongoClient(com.mongodb.reactivestreams.client.MongoClient) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) BadCredentialsException(io.gravitee.am.common.exception.authentication.BadCredentialsException) MongoIdentityProviderConfiguration(io.gravitee.am.identityprovider.mongo.MongoIdentityProviderConfiguration) List(java.util.List) Flowable(io.reactivex.Flowable) PasswordEncoder(io.gravitee.am.service.authentication.crypto.password.PasswordEncoder) Map(java.util.Map) io.gravitee.am.identityprovider.api(io.gravitee.am.identityprovider.api) ObjectId(org.bson.types.ObjectId) UsernameNotFoundException(io.gravitee.am.common.exception.authentication.UsernameNotFoundException) Observable(io.reactivex.Observable) MongoAuthenticationProviderConfiguration(io.gravitee.am.identityprovider.mongo.authentication.spring.MongoAuthenticationProviderConfiguration) UsernameNotFoundException(io.gravitee.am.common.exception.authentication.UsernameNotFoundException) BadCredentialsException(io.gravitee.am.common.exception.authentication.BadCredentialsException)

Aggregations

UsernameNotFoundException (io.gravitee.am.common.exception.authentication.UsernameNotFoundException)3 BadCredentialsException (io.gravitee.am.common.exception.authentication.BadCredentialsException)2 StandardClaims (io.gravitee.am.common.oidc.StandardClaims)2 io.gravitee.am.identityprovider.api (io.gravitee.am.identityprovider.api)2 Flowable (io.reactivex.Flowable)2 Maybe (io.reactivex.Maybe)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Import (org.springframework.context.annotation.Import)2 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)1 Authentication (io.gravitee.am.identityprovider.api.Authentication)1 User (io.gravitee.am.identityprovider.api.User)1 JdbcAbstractProvider (io.gravitee.am.identityprovider.jdbc.JdbcAbstractProvider)1 JdbcAuthenticationProviderConfiguration (io.gravitee.am.identityprovider.jdbc.authentication.spring.JdbcAuthenticationProviderConfiguration)1 ColumnMapRowMapper (io.gravitee.am.identityprovider.jdbc.utils.ColumnMapRowMapper)1 ParametersUtils (io.gravitee.am.identityprovider.jdbc.utils.ParametersUtils)1 MongoIdentityProviderConfiguration (io.gravitee.am.identityprovider.mongo.MongoIdentityProviderConfiguration)1