Search in sources :

Example 6 with HttpHeader

use of io.gravitee.common.http.HttpHeader in project gravitee-access-management by gravitee-io.

the class HttpUserProvider method delete.

@Override
public Completable delete(String id) {
    try {
        // prepare context
        DefaultUser deleteUser = new DefaultUser();
        deleteUser.setId(id);
        AuthenticationContext authenticationContext = new SimpleAuthenticationContext();
        TemplateEngine templateEngine = authenticationContext.getTemplateEngine();
        templateEngine.getTemplateContext().setVariable(USER_CONTEXT_KEY, deleteUser);
        // prepare request
        final HttpUsersResourceConfiguration usersResourceConfiguration = configuration.getUsersResource();
        final HttpResourceConfiguration deleteResourceConfiguration = usersResourceConfiguration.getPaths().getDeleteResource();
        final String deleteUserURI = usersResourceConfiguration.getBaseURL() + deleteResourceConfiguration.getBaseURL();
        final HttpMethod deleteUserHttpMethod = HttpMethod.valueOf(deleteResourceConfiguration.getHttpMethod().toString());
        final List<HttpHeader> deleteUserHttpHeaders = deleteResourceConfiguration.getHttpHeaders();
        final String updateUserBody = deleteResourceConfiguration.getHttpBody();
        final Single<HttpResponse<Buffer>> requestHandler = processRequest(templateEngine, deleteUserURI, deleteUserHttpMethod, deleteUserHttpHeaders, updateUserBody);
        return requestHandler.flatMapCompletable(httpResponse -> {
            final List<HttpResponseErrorCondition> errorConditions = deleteResourceConfiguration.getHttpResponseErrorConditions();
            try {
                processResponse(templateEngine, errorConditions, httpResponse);
                return Completable.complete();
            } catch (Exception ex) {
                return Completable.error(ex);
            }
        }).onErrorResumeNext(ex -> {
            if (ex instanceof AbstractManagementException) {
                return Completable.error(ex);
            }
            LOGGER.error("An error has occurred while deleting user {} from the remote HTTP identity provider", id, ex);
            return Completable.error(new TechnicalManagementException("An error has occurred while deleting user from the remote HTTP identity provider", ex));
        });
    } catch (Exception ex) {
        LOGGER.error("An error has occurred while deleting the user {}", id, ex);
        return Completable.error(new TechnicalManagementException("An error has occurred while deleting the user", ex));
    }
}
Also used : HttpResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpResourceConfiguration) java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) HttpResponseErrorCondition(io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Single(io.reactivex.Single) Constructor(java.lang.reflect.Constructor) HttpIdentityProviderConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpIdentityProviderConfiguration) HttpHeader(io.gravitee.common.http.HttpHeader) TemplateEngine(io.gravitee.el.TemplateEngine) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) SanitizeUtils(io.gravitee.am.identityprovider.http.utils.SanitizeUtils) HttpUsersResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpUsersResourceConfiguration) HttpRequest(io.vertx.reactivex.ext.web.client.HttpRequest) io.gravitee.am.identityprovider.api(io.gravitee.am.identityprovider.api) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JsonObject(io.vertx.core.json.JsonObject) Logger(org.slf4j.Logger) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) Import(org.springframework.context.annotation.Import) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) WebClient(io.vertx.reactivex.ext.web.client.WebClient) JsonArray(io.vertx.core.json.JsonArray) MultiMap(io.vertx.reactivex.core.MultiMap) MediaType(io.gravitee.common.http.MediaType) PasswordEncoder(io.gravitee.am.service.authentication.crypto.password.PasswordEncoder) HttpMethod(io.vertx.core.http.HttpMethod) HttpIdentityProviderResponse(io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse) HttpUserProviderConfiguration(io.gravitee.am.identityprovider.http.user.spring.HttpUserProviderConfiguration) Buffer(io.vertx.reactivex.core.buffer.Buffer) StringUtils(org.springframework.util.StringUtils) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) HttpResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpResourceConfiguration) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) TemplateEngine(io.gravitee.el.TemplateEngine) HttpUsersResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpUsersResourceConfiguration) HttpHeader(io.gravitee.common.http.HttpHeader) HttpMethod(io.vertx.core.http.HttpMethod) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Example 7 with HttpHeader

use of io.gravitee.common.http.HttpHeader in project gravitee-access-management by gravitee-io.

the class HttpUserProvider method update.

@Override
public Single<User> update(String id, User updateUser) {
    try {
        // prepare request
        final HttpUsersResourceConfiguration usersResourceConfiguration = configuration.getUsersResource();
        final HttpResourceConfiguration updateResourceConfiguration = usersResourceConfiguration.getPaths().getUpdateResource();
        final String updateUserURI = usersResourceConfiguration.getBaseURL() + updateResourceConfiguration.getBaseURL();
        final HttpMethod updateUserHttpMethod = HttpMethod.valueOf(updateResourceConfiguration.getHttpMethod().toString());
        final List<HttpHeader> updateUserHttpHeaders = updateResourceConfiguration.getHttpHeaders();
        final String updateUserBody = updateResourceConfiguration.getHttpBody();
        // prepare context
        AuthenticationContext authenticationContext = new SimpleAuthenticationContext();
        TemplateEngine templateEngine = authenticationContext.getTemplateEngine();
        ((DefaultUser) updateUser).setId(id);
        // sanitize password
        if (!StringUtils.isEmpty(updateUser.getCredentials())) {
            ((DefaultUser) updateUser).setCredentials(SanitizeUtils.sanitize(passwordEncoder.encode(updateUser.getCredentials()), updateUserBody, updateUserHttpHeaders));
        }
        templateEngine.getTemplateContext().setVariable(USER_CONTEXT_KEY, updateUser);
        // process request
        final Single<HttpResponse<Buffer>> requestHandler = processRequest(templateEngine, updateUserURI, updateUserHttpMethod, updateUserHttpHeaders, updateUserBody);
        return requestHandler.map(httpResponse -> {
            final List<HttpResponseErrorCondition> errorConditions = updateResourceConfiguration.getHttpResponseErrorConditions();
            Map<String, Object> userAttributes = processResponse(templateEngine, errorConditions, httpResponse);
            return convert(updateUser.getUsername(), userAttributes);
        }).onErrorResumeNext(ex -> {
            if (ex instanceof AbstractManagementException) {
                return Single.error(ex);
            }
            LOGGER.error("An error has occurred while updating user {} from the remote HTTP identity provider", updateUser.getUsername(), ex);
            return Single.error(new TechnicalManagementException("An error has occurred while updating user from the remote HTTP identity provider", ex));
        });
    } catch (Exception ex) {
        LOGGER.error("An error has occurred while updating the user {}", updateUser.getUsername(), ex);
        return Single.error(new TechnicalManagementException("An error has occurred while updating the user", ex));
    }
}
Also used : HttpResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpResourceConfiguration) java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) HttpResponseErrorCondition(io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Single(io.reactivex.Single) Constructor(java.lang.reflect.Constructor) HttpIdentityProviderConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpIdentityProviderConfiguration) HttpHeader(io.gravitee.common.http.HttpHeader) TemplateEngine(io.gravitee.el.TemplateEngine) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) SanitizeUtils(io.gravitee.am.identityprovider.http.utils.SanitizeUtils) HttpUsersResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpUsersResourceConfiguration) HttpRequest(io.vertx.reactivex.ext.web.client.HttpRequest) io.gravitee.am.identityprovider.api(io.gravitee.am.identityprovider.api) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JsonObject(io.vertx.core.json.JsonObject) Logger(org.slf4j.Logger) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) Import(org.springframework.context.annotation.Import) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) WebClient(io.vertx.reactivex.ext.web.client.WebClient) JsonArray(io.vertx.core.json.JsonArray) MultiMap(io.vertx.reactivex.core.MultiMap) MediaType(io.gravitee.common.http.MediaType) PasswordEncoder(io.gravitee.am.service.authentication.crypto.password.PasswordEncoder) HttpMethod(io.vertx.core.http.HttpMethod) HttpIdentityProviderResponse(io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse) HttpUserProviderConfiguration(io.gravitee.am.identityprovider.http.user.spring.HttpUserProviderConfiguration) Buffer(io.vertx.reactivex.core.buffer.Buffer) StringUtils(org.springframework.util.StringUtils) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) HttpResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpResourceConfiguration) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) TemplateEngine(io.gravitee.el.TemplateEngine) HttpUsersResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpUsersResourceConfiguration) HttpHeader(io.gravitee.common.http.HttpHeader) MultiMap(io.vertx.reactivex.core.MultiMap) HttpMethod(io.vertx.core.http.HttpMethod) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Example 8 with HttpHeader

use of io.gravitee.common.http.HttpHeader in project gravitee-access-management by gravitee-io.

the class HttpAuthenticationProviderTestConfiguration method httpIdentityProviderConfiguration.

@Bean
public HttpIdentityProviderConfiguration httpIdentityProviderConfiguration() {
    HttpIdentityProviderConfiguration configuration = new HttpIdentityProviderConfiguration();
    HttpResourceConfiguration httpResourceConfiguration = new HttpResourceConfiguration();
    httpResourceConfiguration.setBaseURL("http://localhost:19999/api/authentication");
    httpResourceConfiguration.setHttpMethod(HttpMethod.POST);
    HttpHeader httpHeader = new HttpHeader();
    httpHeader.setName("Content-Type");
    httpHeader.setValue("application/json");
    httpResourceConfiguration.setHttpHeaders(Collections.singletonList(httpHeader));
    JsonObject jsonObject = new JsonObject();
    jsonObject.put("username", "{#principal}");
    jsonObject.put("password", "{#credentials}");
    httpResourceConfiguration.setHttpBody(jsonObject.encode());
    HttpResponseErrorCondition errorCondition = new HttpResponseErrorCondition();
    errorCondition.setValue("{#authenticationResponse.status == 401}");
    errorCondition.setException("io.gravitee.am.common.exception.authentication.BadCredentialsException");
    HttpResponseErrorCondition errorCondition2 = new HttpResponseErrorCondition();
    errorCondition2.setValue("{#authenticationResponse.status == 404}");
    errorCondition2.setException("io.gravitee.am.common.exception.authentication.UsernameNotFoundException");
    httpResourceConfiguration.setHttpResponseErrorConditions(Arrays.asList(errorCondition, errorCondition2));
    configuration.setAuthenticationResource(httpResourceConfiguration);
    return configuration;
}
Also used : HttpIdentityProviderConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpIdentityProviderConfiguration) HttpHeader(io.gravitee.common.http.HttpHeader) JsonObject(io.vertx.core.json.JsonObject) HttpResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpResourceConfiguration) HttpResponseErrorCondition(io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition) Bean(org.springframework.context.annotation.Bean)

Example 9 with HttpHeader

use of io.gravitee.common.http.HttpHeader 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));
    }
}
Also used : HttpResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpResourceConfiguration) java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) HttpAuthenticationProviderConfiguration(io.gravitee.am.identityprovider.http.authentication.spring.HttpAuthenticationProviderConfiguration) InternalAuthenticationServiceException(io.gravitee.am.common.exception.authentication.InternalAuthenticationServiceException) HttpResponseErrorCondition(io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Single(io.reactivex.Single) Constructor(java.lang.reflect.Constructor) HttpIdentityProviderConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpIdentityProviderConfiguration) HttpHeader(io.gravitee.common.http.HttpHeader) TemplateEngine(io.gravitee.el.TemplateEngine) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) SanitizeUtils(io.gravitee.am.identityprovider.http.utils.SanitizeUtils) AuthenticationException(io.gravitee.am.common.exception.authentication.AuthenticationException) HttpRequest(io.vertx.reactivex.ext.web.client.HttpRequest) io.gravitee.am.identityprovider.api(io.gravitee.am.identityprovider.api) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JsonObject(io.vertx.core.json.JsonObject) StandardClaims(io.gravitee.am.common.oidc.StandardClaims) Logger(org.slf4j.Logger) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) Import(org.springframework.context.annotation.Import) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) WebClient(io.vertx.reactivex.ext.web.client.WebClient) HttpAuthResourcePathsConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpAuthResourcePathsConfiguration) JsonArray(io.vertx.core.json.JsonArray) MultiMap(io.vertx.reactivex.core.MultiMap) MediaType(io.gravitee.common.http.MediaType) PasswordEncoder(io.gravitee.am.service.authentication.crypto.password.PasswordEncoder) HttpMethod(io.vertx.core.http.HttpMethod) HttpIdentityProviderResponse(io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse) Buffer(io.vertx.reactivex.core.buffer.Buffer) AuthenticationException(io.gravitee.am.common.exception.authentication.AuthenticationException) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) InternalAuthenticationServiceException(io.gravitee.am.common.exception.authentication.InternalAuthenticationServiceException) HttpResourceConfiguration(io.gravitee.am.identityprovider.http.configuration.HttpResourceConfiguration) InternalAuthenticationServiceException(io.gravitee.am.common.exception.authentication.InternalAuthenticationServiceException) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) AuthenticationException(io.gravitee.am.common.exception.authentication.AuthenticationException) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) TemplateEngine(io.gravitee.el.TemplateEngine) HttpHeader(io.gravitee.common.http.HttpHeader) JsonObject(io.vertx.core.json.JsonObject) MultiMap(io.vertx.reactivex.core.MultiMap) HttpMethod(io.vertx.core.http.HttpMethod)

Aggregations

HttpHeader (io.gravitee.common.http.HttpHeader)9 JsonObject (io.vertx.core.json.JsonObject)8 HttpIdentityProviderConfiguration (io.gravitee.am.identityprovider.http.configuration.HttpIdentityProviderConfiguration)7 HttpResourceConfiguration (io.gravitee.am.identityprovider.http.configuration.HttpResourceConfiguration)7 HttpResponseErrorCondition (io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition)7 io.gravitee.am.identityprovider.api (io.gravitee.am.identityprovider.api)6 HttpIdentityProviderResponse (io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse)6 SanitizeUtils (io.gravitee.am.identityprovider.http.utils.SanitizeUtils)6 PasswordEncoder (io.gravitee.am.service.authentication.crypto.password.PasswordEncoder)6 AbstractManagementException (io.gravitee.am.service.exception.AbstractManagementException)6 TechnicalManagementException (io.gravitee.am.service.exception.TechnicalManagementException)6 HttpHeaders (io.gravitee.common.http.HttpHeaders)6 MediaType (io.gravitee.common.http.MediaType)6 TemplateEngine (io.gravitee.el.TemplateEngine)6 Maybe (io.reactivex.Maybe)6 Single (io.reactivex.Single)6 HttpMethod (io.vertx.core.http.HttpMethod)6 JsonArray (io.vertx.core.json.JsonArray)6 MultiMap (io.vertx.reactivex.core.MultiMap)6 Buffer (io.vertx.reactivex.core.buffer.Buffer)6