forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RR - use exception mappers on auth failure exceptions for proactive auth
quarkusio#29896 (cherry picked from commit 8ed165f)
- Loading branch information
1 parent
0ad8c23
commit d1e1a6b
Showing
14 changed files
with
447 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...st/java/io/quarkus/resteasy/test/security/ProactiveAuthCompletionExceptionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.quarkus.resteasy.test.security; | ||
|
||
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import javax.annotation.Priority; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Priorities; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.AuthenticationCompletionException; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.filter.cookie.CookieFilter; | ||
|
||
public class ProactiveAuthCompletionExceptionMapperTest { | ||
|
||
private static final String AUTHENTICATION_COMPLETION_EX = "AuthenticationCompletionException"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityProvider.class, TestIdentityController.class) | ||
.addAsResource(new StringAsset("quarkus.http.auth.form.enabled=true\n"), "application.properties"); | ||
} | ||
}); | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
TestIdentityController.resetRoles().add("a d m i n", "a d m i n", "a d m i n"); | ||
} | ||
|
||
@Test | ||
public void testAuthCompletionExMapper() { | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | ||
RestAssured | ||
.given() | ||
.filter(new CookieFilter()) | ||
.redirects().follow(false) | ||
.when() | ||
.formParam("j_username", "a d m i n") | ||
.formParam("j_password", "a d m i n") | ||
.cookie("quarkus-redirect-location", "https://quarkus.io/guides") | ||
.post("/j_security_check") | ||
.then() | ||
.assertThat() | ||
.statusCode(401) | ||
.body(Matchers.equalTo(AUTHENTICATION_COMPLETION_EX)); | ||
} | ||
|
||
@Path("/hello") | ||
public static class HelloResource { | ||
|
||
@GET | ||
public String hello() { | ||
return "Hello"; | ||
} | ||
|
||
} | ||
|
||
@Priority(Priorities.USER) | ||
@Provider | ||
public static class CustomAuthCompletionExceptionMapper implements ExceptionMapper<AuthenticationCompletionException> { | ||
|
||
@Override | ||
public Response toResponse(AuthenticationCompletionException e) { | ||
return Response.status(UNAUTHORIZED).entity(AUTHENTICATION_COMPLETION_EX).build(); | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...o/quarkus/resteasy/test/security/ProactiveAuthHttpPolicyCustomForbiddenExHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.quarkus.resteasy.test.security; | ||
|
||
import static io.quarkus.resteasy.test.security.ProactiveAuthHttpPolicyCustomForbiddenExHandlerTest.CustomForbiddenFailureHandler.CUSTOM_FORBIDDEN_EXCEPTION_HANDLER; | ||
import static javax.ws.rs.core.Response.Status.FORBIDDEN; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.event.Observes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.ForbiddenException; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class ProactiveAuthHttpPolicyCustomForbiddenExHandlerTest { | ||
|
||
private static final String PROPERTIES = "quarkus.http.auth.basic=true\n" + | ||
"quarkus.http.auth.policy.user-policy.roles-allowed=user\n" + | ||
"quarkus.http.auth.permission.roles.paths=/secured\n" + | ||
"quarkus.http.auth.permission.roles.policy=user-policy"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityProvider.class, TestIdentityController.class) | ||
.addAsResource(new StringAsset(PROPERTIES), "application.properties"); | ||
} | ||
}); | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
TestIdentityController.resetRoles().add("a d m i n", "a d m i n", "a d m i n"); | ||
} | ||
|
||
@Test | ||
public void testDeniedAccessAdminResource() { | ||
RestAssured.given() | ||
.auth().basic("a d m i n", "a d m i n") | ||
.when().get("/secured") | ||
.then() | ||
.statusCode(403) | ||
.body(equalTo(CUSTOM_FORBIDDEN_EXCEPTION_HANDLER)); | ||
} | ||
|
||
@Path("/secured") | ||
public static class SecuredResource { | ||
|
||
@GET | ||
public String get() { | ||
throw new IllegalStateException(); | ||
} | ||
|
||
} | ||
|
||
@ApplicationScoped | ||
public static final class CustomForbiddenFailureHandler { | ||
|
||
public static final String CUSTOM_FORBIDDEN_EXCEPTION_HANDLER = CustomForbiddenFailureHandler.class.getName(); | ||
|
||
public void init(@Observes Router router) { | ||
router.route().failureHandler(new Handler<RoutingContext>() { | ||
@Override | ||
public void handle(RoutingContext event) { | ||
if (event.failure() instanceof ForbiddenException) { | ||
event.response().setStatusCode(FORBIDDEN.getStatusCode()).end(CUSTOM_FORBIDDEN_EXCEPTION_HANDLER); | ||
} else { | ||
event.next(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...us/resteasy/reactive/server/test/security/ProactiveAuthCompletionExceptionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.resteasy.reactive.server.ServerExceptionMapper; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.AuthenticationCompletionException; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.filter.cookie.CookieFilter; | ||
|
||
public class ProactiveAuthCompletionExceptionMapperTest { | ||
|
||
private static final String AUTHENTICATION_COMPLETION_EX = "AuthenticationCompletionException"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityProvider.class, TestIdentityController.class, | ||
CustomAuthCompletionExceptionMapper.class) | ||
.addAsResource(new StringAsset("quarkus.http.auth.form.enabled=true\n"), "application.properties"); | ||
} | ||
}); | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
TestIdentityController.resetRoles().add("a d m i n", "a d m i n", "a d m i n"); | ||
} | ||
|
||
@Test | ||
public void testAuthCompletionExMapper() { | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | ||
RestAssured | ||
.given() | ||
.filter(new CookieFilter()) | ||
.redirects().follow(false) | ||
.when() | ||
.formParam("j_username", "a d m i n") | ||
.formParam("j_password", "a d m i n") | ||
.cookie("quarkus-redirect-location", "https://quarkus.io/guides") | ||
.post("/j_security_check") | ||
.then() | ||
.assertThat() | ||
.statusCode(401) | ||
.body(Matchers.equalTo(AUTHENTICATION_COMPLETION_EX)); | ||
} | ||
|
||
public static final class CustomAuthCompletionExceptionMapper { | ||
|
||
@ServerExceptionMapper(value = AuthenticationCompletionException.class) | ||
public Response unauthorized() { | ||
return Response.status(UNAUTHORIZED).entity(AUTHENTICATION_COMPLETION_EX).build(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.