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);
}
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)));
});
}
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)));
});
}
Aggregations