use of io.gravitee.am.common.exception.authentication.AuthenticationException in project gravitee-access-management by gravitee-io.
the class HttpAuthenticationProvider method loadUserByUsername.
@Override
public Maybe<User> loadUserByUsername(Authentication authentication) {
try {
// prepare request
final HttpResourceConfiguration resourceConfiguration = configuration.getAuthenticationResource();
final HttpMethod authenticationHttpMethod = HttpMethod.valueOf(resourceConfiguration.getHttpMethod().toString());
final List<HttpHeader> authenticationHttpHeaders = resourceConfiguration.getHttpHeaders();
final String authenticationBody = resourceConfiguration.getHttpBody();
final Object principal = authentication.getPrincipal();
final String encodedCredentials = passwordEncoder.encode((String) authentication.getCredentials());
final Object credentials = SanitizeUtils.sanitize(encodedCredentials, authenticationBody, authenticationHttpHeaders);
// prepare context
TemplateEngine templateEngine = authentication.getContext().getTemplateEngine();
templateEngine.getTemplateContext().setVariable(PRINCIPAL_CONTEXT_KEY, principal);
templateEngine.getTemplateContext().setVariable(CREDENTIALS_CONTEXT_KEY, credentials);
// process request
final String authenticationURI = templateEngine.getValue(resourceConfiguration.getBaseURL(), String.class);
final Single<HttpResponse<Buffer>> requestHandler = processRequest(templateEngine, authenticationURI, authenticationHttpMethod, authenticationHttpHeaders, authenticationBody);
return requestHandler.toMaybe().map(httpResponse -> {
final List<HttpResponseErrorCondition> errorConditions = resourceConfiguration.getHttpResponseErrorConditions();
Map<String, Object> userAttributes = processResponse(templateEngine, errorConditions, httpResponse);
return createUser(authentication.getContext(), userAttributes);
}).onErrorResumeNext(ex -> {
if (ex instanceof AuthenticationException) {
return Maybe.error(ex);
}
LOGGER.error("An error has occurred while calling the remote HTTP identity provider {}", ex);
return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while calling the remote HTTP identity provider", ex));
});
} catch (Exception ex) {
LOGGER.error("An error has occurred while authenticating the user {}", ex);
return Maybe.error(new InternalAuthenticationServiceException("An error has occurred while authenticating the user", ex));
}
}
use of io.gravitee.am.common.exception.authentication.AuthenticationException in project gravitee-access-management by gravitee-io.
the class LoginFailureHandler method handle.
@Override
public void handle(RoutingContext routingContext) {
if (routingContext.failed()) {
Throwable throwable = routingContext.failure();
if (throwable instanceof PolicyChainException) {
PolicyChainException policyChainException = (PolicyChainException) throwable;
handleException(routingContext, policyChainException.key(), policyChainException.getMessage());
} else if (throwable instanceof AccountPasswordExpiredException) {
handleException(routingContext, ((AccountPasswordExpiredException) throwable).getErrorCode(), throwable.getMessage());
} else if (throwable instanceof AuthenticationException) {
handleException(routingContext, "invalid_user", "Invalid or unknown user");
} else {
// technical exception will be managed by the generic error handler, continue
routingContext.next();
}
}
}
Aggregations