Search in sources :

Example 1 with OAuth2ErrorResponse

use of io.gravitee.am.gateway.handler.oauth2.service.response.OAuth2ErrorResponse in project gravitee-access-management by gravitee-io.

the class UmaExceptionHandler method handle.

@Override
public void handle(RoutingContext routingContext) {
    if (routingContext.failed()) {
        Throwable throwable = routingContext.failure();
        if (isInvalidRequest(throwable)) {
            OAuth2ErrorResponse oAuth2ErrorResponse = new OAuth2ErrorResponse("invalid_request");
            oAuth2ErrorResponse.setDescription(throwable.getMessage());
            routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).putHeader(HttpHeaders.CACHE_CONTROL, "no-store").putHeader(HttpHeaders.PRAGMA, "no-cache").setStatusCode(HttpStatusCode.BAD_REQUEST_400).end(Json.encodePrettily(oAuth2ErrorResponse));
        } else if (throwable instanceof AbstractNotFoundException) {
            OAuth2Exception oAuth2Exception = new io.gravitee.am.gateway.handler.oauth2.exception.ResourceNotFoundException(throwable.getMessage());
            OAuth2ErrorResponse oAuth2ErrorResponse = new OAuth2ErrorResponse(oAuth2Exception.getOAuth2ErrorCode());
            oAuth2ErrorResponse.setDescription(oAuth2Exception.getMessage());
            routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).putHeader(HttpHeaders.CACHE_CONTROL, "no-store").putHeader(HttpHeaders.PRAGMA, "no-cache").setStatusCode(oAuth2Exception.getHttpStatusCode()).end(Json.encodePrettily(oAuth2ErrorResponse));
        } else {
            super.handle(routingContext);
        }
    }
}
Also used : io.gravitee.am.service.exception(io.gravitee.am.service.exception) OAuth2ErrorResponse(io.gravitee.am.gateway.handler.oauth2.service.response.OAuth2ErrorResponse) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception)

Example 2 with OAuth2ErrorResponse

use of io.gravitee.am.gateway.handler.oauth2.service.response.OAuth2ErrorResponse in project gravitee-access-management by gravitee-io.

the class ExceptionHandler method handle.

@Override
public void handle(RoutingContext routingContext) {
    if (routingContext.failed()) {
        Throwable throwable = routingContext.failure();
        if (throwable instanceof OAuth2Exception) {
            OAuth2Exception oAuth2Exception = (OAuth2Exception) throwable;
            OAuth2ErrorResponse oAuth2ErrorResponse = new OAuth2ErrorResponse(oAuth2Exception.getOAuth2ErrorCode());
            oAuth2ErrorResponse.setDescription(oAuth2Exception.getMessage());
            routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).putHeader(HttpHeaders.CACHE_CONTROL, "no-store").putHeader(HttpHeaders.PRAGMA, "no-cache").setStatusCode(oAuth2Exception.getHttpStatusCode()).end(Json.encodePrettily(oAuth2ErrorResponse));
        } else if (throwable instanceof UmaException) {
            UmaException umaException = (UmaException) throwable;
            UMAErrorResponse umaErrorResponse = new UMAErrorResponse(umaException.getError()).setTicket(umaException.getTicket()).setRedirectUser(umaException.getRedirectUser()).setInterval(umaException.getInterval()).setRequiredClaims(this.from(umaException));
            routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).putHeader(HttpHeaders.CACHE_CONTROL, "no-store").putHeader(HttpHeaders.PRAGMA, "no-cache").setStatusCode(umaException.getStatus()).end(Json.encodePrettily(umaErrorResponse));
        } else if (throwable instanceof PolicyChainException) {
            PolicyChainException policyChainException = (PolicyChainException) throwable;
            OAuth2ErrorResponse oAuth2ErrorResponse = new OAuth2ErrorResponse(policyChainException.key());
            oAuth2ErrorResponse.setDescription(policyChainException.getMessage());
            routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).putHeader(HttpHeaders.CACHE_CONTROL, "no-store").putHeader(HttpHeaders.PRAGMA, "no-cache").setStatusCode(policyChainException.statusCode()).end(Json.encodePrettily(oAuth2ErrorResponse));
        } else if (throwable instanceof HttpException) {
            routingContext.response().setStatusCode(((HttpException) throwable).getStatusCode()).end();
        } else {
            logger.error("An exception occurs while handling incoming request", throwable);
            if (routingContext.statusCode() != -1) {
                routingContext.response().setStatusCode(routingContext.statusCode()).end();
            } else {
                routingContext.response().setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end();
            }
        }
    }
}
Also used : UMAErrorResponse(io.gravitee.am.gateway.handler.oauth2.service.response.UMAErrorResponse) UmaException(io.gravitee.am.common.exception.uma.UmaException) HttpException(io.vertx.ext.web.handler.HttpException) OAuth2ErrorResponse(io.gravitee.am.gateway.handler.oauth2.service.response.OAuth2ErrorResponse) PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception)

Example 3 with OAuth2ErrorResponse

use of io.gravitee.am.gateway.handler.oauth2.service.response.OAuth2ErrorResponse in project gravitee-access-management by gravitee-io.

the class AuthorizationRequestFailureHandler method handle.

@Override
public void handle(RoutingContext routingContext) {
    if (routingContext.failed()) {
        try {
            AuthorizationRequest request = resolveInitialAuthorizeRequest(routingContext);
            Client client = routingContext.get(ConstantKeys.CLIENT_CONTEXT_KEY);
            String defaultErrorURL = UriBuilderRequest.resolveProxyRequest(routingContext.request(), routingContext.get(CONTEXT_PATH) + ERROR_ENDPOINT);
            Throwable throwable = routingContext.failure();
            if (throwable instanceof OAuth2Exception) {
                OAuth2Exception oAuth2Exception = (OAuth2Exception) throwable;
                // Manage exception
                processOAuth2Exception(request, oAuth2Exception, client, defaultErrorURL, routingContext, h -> {
                    if (h.failed()) {
                        logger.error("An error has occurred while handling authorization error response", h.cause());
                        routingContext.response().setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end();
                        return;
                    }
                    // redirect user to the error page with error code and description
                    doRedirect(routingContext, h.result());
                });
            } else if (throwable instanceof HttpException) {
                // in case of http status exception, go to the default error page
                request.setRedirectUri(defaultErrorURL);
                HttpException httpStatusException = (HttpException) throwable;
                doRedirect(routingContext, buildRedirectUri(httpStatusException.getMessage(), httpStatusException.getPayload(), request, routingContext));
            } else if (throwable instanceof PolicyChainException) {
                PolicyChainException policyChainException = (PolicyChainException) throwable;
                OAuth2ErrorResponse oAuth2ErrorResponse = new OAuth2ErrorResponse(policyChainException.key());
                oAuth2ErrorResponse.setDescription(policyChainException.getMessage());
                routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).putHeader(HttpHeaders.CACHE_CONTROL, "no-store").putHeader(HttpHeaders.PRAGMA, "no-cache").setStatusCode(policyChainException.statusCode()).end(Json.encodePrettily(oAuth2ErrorResponse));
            } else {
                logger.error("An exception has occurred while handling authorization request", throwable);
                cleanSession(routingContext);
                if (routingContext.statusCode() != -1) {
                    routingContext.response().setStatusCode(routingContext.statusCode()).end();
                } else {
                    routingContext.response().setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end();
                }
            }
        } catch (Exception e) {
            logger.error("Unable to handle authorization error response", e);
            doRedirect(routingContext, routingContext.get(CONTEXT_PATH) + ERROR_ENDPOINT);
        }
    }
}
Also used : AuthorizationRequest(io.gravitee.am.gateway.handler.oauth2.service.request.AuthorizationRequest) HttpException(io.vertx.ext.web.handler.HttpException) OAuth2ErrorResponse(io.gravitee.am.gateway.handler.oauth2.service.response.OAuth2ErrorResponse) Client(io.gravitee.am.model.oidc.Client) PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception) JWTOAuth2Exception(io.gravitee.am.gateway.handler.oauth2.exception.JWTOAuth2Exception) RedirectMismatchException(io.gravitee.am.gateway.handler.oauth2.exception.RedirectMismatchException) URISyntaxException(java.net.URISyntaxException) PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception) HttpException(io.vertx.ext.web.handler.HttpException) InvalidRequestObjectException(io.gravitee.am.common.exception.oauth2.InvalidRequestObjectException) JWTOAuth2Exception(io.gravitee.am.gateway.handler.oauth2.exception.JWTOAuth2Exception)

Aggregations

OAuth2Exception (io.gravitee.am.common.exception.oauth2.OAuth2Exception)3 OAuth2ErrorResponse (io.gravitee.am.gateway.handler.oauth2.service.response.OAuth2ErrorResponse)3 PolicyChainException (io.gravitee.am.gateway.policy.PolicyChainException)2 HttpException (io.vertx.ext.web.handler.HttpException)2 InvalidRequestObjectException (io.gravitee.am.common.exception.oauth2.InvalidRequestObjectException)1 UmaException (io.gravitee.am.common.exception.uma.UmaException)1 JWTOAuth2Exception (io.gravitee.am.gateway.handler.oauth2.exception.JWTOAuth2Exception)1 RedirectMismatchException (io.gravitee.am.gateway.handler.oauth2.exception.RedirectMismatchException)1 AuthorizationRequest (io.gravitee.am.gateway.handler.oauth2.service.request.AuthorizationRequest)1 UMAErrorResponse (io.gravitee.am.gateway.handler.oauth2.service.response.UMAErrorResponse)1 Client (io.gravitee.am.model.oidc.Client)1 io.gravitee.am.service.exception (io.gravitee.am.service.exception)1 URISyntaxException (java.net.URISyntaxException)1