Search in sources :

Example 1 with PolicyChainException

use of io.gravitee.am.gateway.policy.PolicyChainException in project gravitee-access-management by gravitee-io.

the class ErrorHandler method handle.

@Override
public void handle(RoutingContext routingContext) {
    if (routingContext.failed()) {
        Throwable throwable = routingContext.failure();
        // management exception (resource not found, server error, ...)
        if (throwable instanceof AbstractManagementException) {
            AbstractManagementException technicalManagementException = (AbstractManagementException) throwable;
            handleException(routingContext, technicalManagementException.getHttpStatusCode(), technicalManagementException.getMessage());
        // oauth2 exception (token invalid exception)
        } else if (throwable instanceof OAuth2Exception) {
            OAuth2Exception oAuth2Exception = (OAuth2Exception) throwable;
            handleException(routingContext, oAuth2Exception.getHttpStatusCode(), oAuth2Exception.getMessage());
        } else if (throwable instanceof PolicyChainException) {
            PolicyChainException policyChainException = (PolicyChainException) throwable;
            handleException(routingContext, policyChainException.statusCode(), policyChainException.key() + " : " + policyChainException.getMessage());
        } else if (throwable instanceof HttpException) {
            HttpException httpStatusException = (HttpException) throwable;
            handleException(routingContext, httpStatusException.getStatusCode(), httpStatusException.getPayload());
        } else {
            logger.error(throwable.getMessage(), throwable);
            if (routingContext.statusCode() != -1) {
                routingContext.response().setStatusCode(routingContext.statusCode()).end();
            } else {
                routingContext.response().setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end();
            }
        }
    }
}
Also used : AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) HttpException(io.vertx.ext.web.handler.HttpException) PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception)

Example 2 with PolicyChainException

use of io.gravitee.am.gateway.policy.PolicyChainException in project gravitee-access-management by gravitee-io.

the class ErrorHandler method handle.

@Override
public void handle(RoutingContext routingContext) {
    if (routingContext.failed()) {
        Throwable throwable = routingContext.failure();
        // management exception (resource not found, server error, ...)
        if (throwable instanceof AbstractManagementException) {
            AbstractManagementException technicalManagementException = (AbstractManagementException) throwable;
            handleException(routingContext, "technical_error", technicalManagementException.getMessage());
        // oauth2 exception (token invalid exception)
        } else if (throwable instanceof OAuth2Exception) {
            OAuth2Exception oAuth2Exception = (OAuth2Exception) throwable;
            handleException(routingContext, oAuth2Exception.getOAuth2ErrorCode(), oAuth2Exception.getMessage());
        } else if (throwable instanceof PolicyChainException) {
            PolicyChainException policyChainException = (PolicyChainException) throwable;
            handleException(routingContext, policyChainException.key(), policyChainException.getMessage());
        } else if (throwable instanceof HttpException) {
            HttpException httpStatusException = (HttpException) throwable;
            handleException(routingContext, httpStatusException.getMessage(), httpStatusException.getPayload());
        } 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 : AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) HttpException(io.vertx.ext.web.handler.HttpException) PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception)

Example 3 with PolicyChainException

use of io.gravitee.am.gateway.policy.PolicyChainException in project gravitee-access-management by gravitee-io.

the class UserConsentFailureHandler method handle.

@Override
public void handle(RoutingContext context) {
    if (context.failed()) {
        // logout the user
        // but keep the session intact with the original OAuth 2.0 authorization request in order to replay the whole login process
        context.clearUser();
        // handle exception
        Throwable throwable = context.failure();
        if (throwable instanceof PolicyChainException) {
            PolicyChainException policyChainException = (PolicyChainException) throwable;
            handleException(context, policyChainException.key(), policyChainException.getMessage());
        } else {
            handleException(context, "internal_server_error", "Unexpected error");
        }
    }
}
Also used : PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException)

Example 4 with PolicyChainException

use of io.gravitee.am.gateway.policy.PolicyChainException in project gravitee-access-management by gravitee-io.

the class UmaTokenGranterTest method grant_nominalCase_accessPolicy_deny.

@Test
public void grant_nominalCase_accessPolicy_deny() {
    AccessPolicy policy = mock(AccessPolicy.class);
    when(policy.getType()).thenReturn(AccessPolicyType.GROOVY);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(resourceService.findAccessPoliciesByResources(anyList())).thenReturn(Flowable.just(policy));
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    when(rulesEngine.fire(any(), any())).thenReturn(Completable.error(new PolicyChainException("Policy requirements have failed")));
    TestObserver<Token> testObserver = umaTokenGranter.grant(tokenRequest, client).test();
    testObserver.assertNotComplete().assertError(InvalidGrantException.class);
}
Also used : ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Token(io.gravitee.am.gateway.handler.oauth2.service.token.Token) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException) AccessPolicy(io.gravitee.am.model.uma.policy.AccessPolicy) Test(org.junit.Test)

Example 5 with PolicyChainException

use of io.gravitee.am.gateway.policy.PolicyChainException 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)

Aggregations

PolicyChainException (io.gravitee.am.gateway.policy.PolicyChainException)8 OAuth2Exception (io.gravitee.am.common.exception.oauth2.OAuth2Exception)5 HttpException (io.vertx.ext.web.handler.HttpException)5 AbstractManagementException (io.gravitee.am.service.exception.AbstractManagementException)3 OAuth2ErrorResponse (io.gravitee.am.gateway.handler.oauth2.service.response.OAuth2ErrorResponse)2 AccountPasswordExpiredException (io.gravitee.am.common.exception.authentication.AccountPasswordExpiredException)1 AuthenticationException (io.gravitee.am.common.exception.authentication.AuthenticationException)1 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 Token (io.gravitee.am.gateway.handler.oauth2.service.token.Token)1 AccessToken (io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken)1 SCIMException (io.gravitee.am.gateway.handler.scim.exception.SCIMException)1 UnauthorizedException (io.gravitee.am.gateway.handler.scim.exception.UnauthorizedException)1 Client (io.gravitee.am.model.oidc.Client)1 AccessPolicy (io.gravitee.am.model.uma.policy.AccessPolicy)1 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)1