use of io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition in project gravitee-access-management by gravitee-io.
the class HttpUserProvider method create.
@Override
public Single<User> create(User user) {
try {
// prepare request
final HttpUsersResourceConfiguration usersResourceConfiguration = configuration.getUsersResource();
final HttpResourceConfiguration createResourceConfiguration = usersResourceConfiguration.getPaths().getCreateResource();
final String createUserURI = usersResourceConfiguration.getBaseURL() + createResourceConfiguration.getBaseURL();
final HttpMethod createUserHttpMethod = HttpMethod.valueOf(createResourceConfiguration.getHttpMethod().toString());
final List<HttpHeader> createUserHttpHeaders = createResourceConfiguration.getHttpHeaders();
final String createUserBody = createResourceConfiguration.getHttpBody();
// prepare context
AuthenticationContext authenticationContext = new SimpleAuthenticationContext();
TemplateEngine templateEngine = authenticationContext.getTemplateEngine();
// sanitize password
if (!StringUtils.isEmpty(user.getCredentials())) {
((DefaultUser) user).setCredentials(SanitizeUtils.sanitize(passwordEncoder.encode(user.getCredentials()), createUserBody, createUserHttpHeaders));
}
templateEngine.getTemplateContext().setVariable(USER_CONTEXT_KEY, user);
// process request
final Single<HttpResponse<Buffer>> requestHandler = processRequest(templateEngine, createUserURI, createUserHttpMethod, createUserHttpHeaders, createUserBody);
return requestHandler.map(httpResponse -> {
final List<HttpResponseErrorCondition> errorConditions = createResourceConfiguration.getHttpResponseErrorConditions();
Map<String, Object> userAttributes = processResponse(templateEngine, errorConditions, httpResponse);
return convert(user.getUsername(), userAttributes);
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error has occurred while creating user {} from the remote HTTP identity provider", user.getUsername(), ex);
return Single.error(new TechnicalManagementException("An error has occurred while creating user from the remote HTTP identity provider", ex));
});
} catch (Exception ex) {
LOGGER.error("An error has occurred while creating the user {}", user.getUsername(), ex);
return Single.error(new TechnicalManagementException("An error has occurred while creating the user", ex));
}
}
use of io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition in project gravitee-access-management by gravitee-io.
the class HttpUserProvider method findByUser.
private Maybe<User> findByUser(HttpUsersResourceConfiguration usersResourceConfiguration, HttpResourceConfiguration readResourceConfiguration, User user) {
try {
// prepare context
AuthenticationContext authenticationContext = new SimpleAuthenticationContext();
TemplateEngine templateEngine = authenticationContext.getTemplateEngine();
templateEngine.getTemplateContext().setVariable(USER_CONTEXT_KEY, user);
// prepare request
final String readUserURI = usersResourceConfiguration.getBaseURL() + readResourceConfiguration.getBaseURL();
final HttpMethod readUserHttpMethod = HttpMethod.valueOf(readResourceConfiguration.getHttpMethod().toString());
final List<HttpHeader> readUserHttpHeaders = readResourceConfiguration.getHttpHeaders();
final String readUserBody = readResourceConfiguration.getHttpBody();
final Single<HttpResponse<Buffer>> requestHandler = processRequest(templateEngine, readUserURI, readUserHttpMethod, readUserHttpHeaders, readUserBody);
return requestHandler.toMaybe().map(httpResponse -> {
final List<HttpResponseErrorCondition> errorConditions = readResourceConfiguration.getHttpResponseErrorConditions();
Map<String, Object> userAttributes = processResponse(templateEngine, errorConditions, httpResponse);
return convert(user.getUsername(), userAttributes);
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Maybe.error(ex);
}
LOGGER.error("An error has occurred while searching user {} from the remote HTTP identity provider", user.getUsername() != null ? user.getUsername() : user.getEmail(), ex);
return Maybe.error(new TechnicalManagementException("An error has occurred while searching user from the remote HTTP identity provider", ex));
});
} catch (Exception ex) {
LOGGER.error("An error has occurred while searching the user {}", user.getUsername() != null ? user.getUsername() : user.getEmail(), ex);
return Maybe.error(new TechnicalManagementException("An error has occurred while searching the user", ex));
}
}
use of io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition in project gravitee-access-management by gravitee-io.
the class HttpAuthenticationProvider method processResponse.
private Map<String, Object> processResponse(TemplateEngine templateEngine, List<HttpResponseErrorCondition> errorConditions, HttpResponse<Buffer> httpResponse) throws Exception {
String responseBody = httpResponse.bodyAsString();
templateEngine.getTemplateContext().setVariable(AUTHENTICATION_RESPONSE_CONTEXT_KEY, new HttpIdentityProviderResponse(httpResponse, responseBody));
// process response
Exception lastException = null;
if (errorConditions != null) {
Iterator<HttpResponseErrorCondition> iter = errorConditions.iterator();
while (iter.hasNext() && lastException == null) {
HttpResponseErrorCondition errorCondition = iter.next();
if (templateEngine.getValue(errorCondition.getValue(), Boolean.class)) {
Class<? extends Exception> clazz = (Class<? extends Exception>) Class.forName(errorCondition.getException());
if (errorCondition.getMessage() != null) {
String errorMessage = templateEngine.getValue(errorCondition.getMessage(), String.class);
Constructor<?> constructor = clazz.getConstructor(String.class);
lastException = clazz.cast(constructor.newInstance(new Object[] { errorMessage }));
} else {
lastException = clazz.newInstance();
}
}
}
}
// if remote API call failed, throw exception
if (lastException != null) {
throw lastException;
}
if (responseBody == null) {
throw new InternalAuthenticationServiceException("Unable to find user information");
}
return responseBody.startsWith("[") ? new JsonArray(responseBody).getJsonObject(0).getMap() : new JsonObject(responseBody).getMap();
}
use of io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition in project gravitee-access-management by gravitee-io.
the class HttpAuthenticationProvider method loadByUsername0.
private Maybe<User> loadByUsername0(AuthenticationContext authenticationContext, User user) {
// prepare request
final HttpAuthResourcePathsConfiguration authResourceConfiguration = configuration.getAuthenticationResource().getPaths();
if (authResourceConfiguration == null) {
return Maybe.empty();
}
if (authResourceConfiguration.getLoadPreAuthUserResource() == null) {
return Maybe.empty();
}
final HttpResourceConfiguration readResourceConfiguration = authResourceConfiguration.getLoadPreAuthUserResource();
if (readResourceConfiguration.getBaseURL() == null) {
LOGGER.warn("Missing pre-authenticated user resource base URL");
return Maybe.empty();
}
if (readResourceConfiguration.getHttpMethod() == null) {
LOGGER.warn("Missing pre-authenticated user resource HTTP method");
return Maybe.empty();
}
try {
// prepare context
TemplateEngine templateEngine = authenticationContext.getTemplateEngine();
templateEngine.getTemplateContext().setVariable(USER_CONTEXT_KEY, user);
// prepare request
final String readUserURI = readResourceConfiguration.getBaseURL();
final HttpMethod readUserHttpMethod = HttpMethod.valueOf(readResourceConfiguration.getHttpMethod().toString());
final List<HttpHeader> readUserHttpHeaders = readResourceConfiguration.getHttpHeaders();
final String readUserBody = readResourceConfiguration.getHttpBody();
final Single<HttpResponse<Buffer>> requestHandler = processRequest(templateEngine, readUserURI, readUserHttpMethod, readUserHttpHeaders, readUserBody);
return requestHandler.toMaybe().map(httpResponse -> {
final List<HttpResponseErrorCondition> errorConditions = readResourceConfiguration.getHttpResponseErrorConditions();
Map<String, Object> userAttributes = processResponse(templateEngine, errorConditions, httpResponse);
return createUser(authenticationContext, userAttributes);
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Maybe.error(ex);
}
LOGGER.error("An error has occurred when loading pre-authenticated user {} from the remote HTTP identity provider", user.getUsername() != null ? user.getUsername() : user.getEmail(), ex);
return Maybe.error(new TechnicalManagementException("An error has occurred when loading pre-authenticated user from the remote HTTP identity provider", ex));
});
} catch (Exception ex) {
LOGGER.error("An error has occurred when loading pre-authenticated user {}", user.getUsername() != null ? user.getUsername() : user.getEmail(), ex);
return Maybe.error(new TechnicalManagementException("An error has occurred when when loading pre-authenticated user", ex));
}
}
use of io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition in project gravitee-access-management by gravitee-io.
the class HttpUserProvider method processResponse.
private Map<String, Object> processResponse(TemplateEngine templateEngine, List<HttpResponseErrorCondition> errorConditions, HttpResponse<Buffer> httpResponse) throws Exception {
String responseBody = httpResponse.bodyAsString();
templateEngine.getTemplateContext().setVariable(USER_API_RESPONSE_CONTEXT_KEY, new HttpIdentityProviderResponse(httpResponse, responseBody));
// process response
Exception lastException = null;
if (errorConditions != null) {
Iterator<HttpResponseErrorCondition> iter = errorConditions.iterator();
while (iter.hasNext() && lastException == null) {
HttpResponseErrorCondition errorCondition = iter.next();
if (templateEngine.getValue(errorCondition.getValue(), Boolean.class)) {
Class<? extends Exception> clazz = (Class<? extends Exception>) Class.forName(errorCondition.getException());
if (errorCondition.getMessage() != null) {
String errorMessage = templateEngine.getValue(errorCondition.getMessage(), String.class);
Constructor<?> constructor = clazz.getConstructor(String.class);
lastException = clazz.cast(constructor.newInstance(new Object[] { errorMessage }));
} else {
lastException = clazz.newInstance();
}
}
}
}
// if remote API call failed, throw exception
if (lastException != null) {
throw lastException;
}
if (responseBody == null) {
return Collections.emptyMap();
}
return responseBody.startsWith("[") ? new JsonArray(responseBody).getJsonObject(0).getMap() : new JsonObject(responseBody).getMap();
}
Aggregations