Skip to content

Commit

Permalink
Merge pull request quarkusio#22562 from geoand/rr-custom-security-map…
Browse files Browse the repository at this point in the history
…pers

Allow user defined exception mappers to override built-in security exception mapping
  • Loading branch information
geoand committed Jan 4, 2022
2 parents 51e7d91 + 13236c1 commit 5ba71b2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.security.AuthenticationCompletionException;
import io.quarkus.security.AuthenticationFailedException;
import io.quarkus.security.AuthenticationRedirectException;
import io.quarkus.security.ForbiddenException;
import io.quarkus.security.UnauthorizedException;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
import io.quarkus.vertx.http.runtime.VertxHttpRecorder;
Expand All @@ -170,6 +168,8 @@ public class ResteasyReactiveProcessor {
DotName.createSimple(HttpServerResponse.class.getName()),
DotName.createSimple(RoutingContext.class.getName()));

private static final int SECURITY_EXCEPTION_MAPPERS_PRIORITY = Priorities.USER + 1;

@BuildStep
public FeatureBuildItem buildSetup() {
return new FeatureBuildItem(Feature.RESTEASY_REACTIVE);
Expand Down Expand Up @@ -815,23 +815,15 @@ public void securityExceptionMappers(BuildProducer<ExceptionMapperBuildItem> exc
exceptionMapperBuildItemBuildProducer.produce(new ExceptionMapperBuildItem(
AuthenticationCompletionExceptionMapper.class.getName(),
AuthenticationCompletionException.class.getName(),
Priorities.USER, false));
exceptionMapperBuildItemBuildProducer.produce(new ExceptionMapperBuildItem(
AuthenticationFailedExceptionMapper.class.getName(),
AuthenticationFailedException.class.getName(),
Priorities.USER + 1, false));
SECURITY_EXCEPTION_MAPPERS_PRIORITY, false));
exceptionMapperBuildItemBuildProducer.produce(new ExceptionMapperBuildItem(
AuthenticationRedirectExceptionMapper.class.getName(),
AuthenticationRedirectException.class.getName(),
Priorities.USER, false));
SECURITY_EXCEPTION_MAPPERS_PRIORITY, false));
exceptionMapperBuildItemBuildProducer.produce(new ExceptionMapperBuildItem(
ForbiddenExceptionMapper.class.getName(),
ForbiddenException.class.getName(),
Priorities.USER + 1, false));
exceptionMapperBuildItemBuildProducer.produce(new ExceptionMapperBuildItem(
UnauthorizedExceptionMapper.class.getName(),
UnauthorizedException.class.getName(),
Priorities.USER + 1, false));
SECURITY_EXCEPTION_MAPPERS_PRIORITY, false));
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkus.resteasy.reactive.server.test.security;

import static io.restassured.RestAssured.when;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.security.UnauthorizedException;
import io.quarkus.test.QuarkusUnitTest;

public class CustomExceptionMapperTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(HelloResource.class, CustomExceptionMappers.class));

@Test
public void shouldDenyUnannotated() {
when().get("hello")
.then()
.statusCode(999);
}

@Path("hello")
@RolesAllowed("test")
public static final class HelloResource {

@GET
public String hello() {
return "hello world";
}
}

public static final class CustomExceptionMappers {

@ServerExceptionMapper(UnauthorizedException.class)
public Response forbidden() {
return Response.status(999).build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.resteasy.reactive.server.runtime.exceptionmappers;

import javax.ws.rs.Priorities;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
Expand All @@ -10,7 +11,7 @@

public class AuthenticationFailedExceptionMapper {

@ServerExceptionMapper(AuthenticationFailedException.class)
@ServerExceptionMapper(value = AuthenticationFailedException.class, priority = Priorities.USER + 1)
public Uni<Response> handle(RoutingContext routingContext) {
return SecurityExceptionMapperUtil.handleWithAuthenticator(routingContext);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.resteasy.reactive.server.runtime.exceptionmappers;

import javax.ws.rs.Priorities;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
Expand All @@ -10,7 +11,7 @@

public class UnauthorizedExceptionMapper {

@ServerExceptionMapper(UnauthorizedException.class)
@ServerExceptionMapper(value = UnauthorizedException.class, priority = Priorities.USER + 1)
public Uni<Response> handle(RoutingContext routingContext) {
return SecurityExceptionMapperUtil.handleWithAuthenticator(routingContext);
}
Expand Down

0 comments on commit 5ba71b2

Please sign in to comment.