Search in sources :

Example 1 with HttpIdentityProviderResponse

use of io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse 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();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpIdentityProviderResponse(io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse) InternalAuthenticationServiceException(io.gravitee.am.common.exception.authentication.InternalAuthenticationServiceException) JsonObject(io.vertx.core.json.JsonObject) 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) HttpResponseErrorCondition(io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition)

Example 2 with HttpIdentityProviderResponse

use of io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse 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();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpIdentityProviderResponse(io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse) JsonObject(io.vertx.core.json.JsonObject) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) HttpResponseErrorCondition(io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition)

Aggregations

HttpIdentityProviderResponse (io.gravitee.am.identityprovider.http.HttpIdentityProviderResponse)2 HttpResponseErrorCondition (io.gravitee.am.identityprovider.http.configuration.HttpResponseErrorCondition)2 AbstractManagementException (io.gravitee.am.service.exception.AbstractManagementException)2 TechnicalManagementException (io.gravitee.am.service.exception.TechnicalManagementException)2 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2 AuthenticationException (io.gravitee.am.common.exception.authentication.AuthenticationException)1 InternalAuthenticationServiceException (io.gravitee.am.common.exception.authentication.InternalAuthenticationServiceException)1