-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce runtime named HTTP Security Policies
- Loading branch information
1 parent
2f2ce09
commit c47168c
Showing
11 changed files
with
238 additions
and
42 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
29 changes: 29 additions & 0 deletions
29
...ttp/deployment/src/test/java/io/quarkus/vertx/http/security/CustomNamedHttpSecPolicy.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,29 @@ | ||
package io.quarkus.vertx.http.security; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.vertx.http.runtime.security.HttpSecurityPolicy; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
@ApplicationScoped | ||
public class CustomNamedHttpSecPolicy implements HttpSecurityPolicy { | ||
@Override | ||
public Uni<CheckResult> checkPermission(RoutingContext request, Uni<SecurityIdentity> identity, | ||
AuthorizationRequestContext requestContext) { | ||
if (isRequestAuthorized(request)) { | ||
return Uni.createFrom().item(CheckResult.PERMIT); | ||
} | ||
return Uni.createFrom().item(CheckResult.DENY); | ||
} | ||
|
||
private static boolean isRequestAuthorized(RoutingContext request) { | ||
return request.request().headers().contains("hush-hush"); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "custom123"; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...deployment/src/test/java/io/quarkus/vertx/http/security/CustomNamedHttpSecPolicyTest.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,82 @@ | ||
package io.quarkus.vertx.http.security; | ||
|
||
import java.util.function.Supplier; | ||
|
||
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.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class CustomNamedHttpSecPolicyTest { | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
TestIdentityController.resetRoles().add("test", "test", "test"); | ||
} | ||
|
||
private static final String APP_PROPS = "" + | ||
"quarkus.http.auth.permission.authenticated.paths=admin\n" + | ||
"quarkus.http.auth.permission.authenticated.policy=custom123\n"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityController.class, TestIdentityProvider.class, AdminPathHandler.class, | ||
CustomNamedHttpSecPolicy.class) | ||
.addAsResource(new StringAsset(APP_PROPS), "application.properties"); | ||
} | ||
}); | ||
|
||
@Test | ||
public void testAdminPath() { | ||
RestAssured | ||
.given() | ||
.when() | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(401); | ||
RestAssured | ||
.given() | ||
.when() | ||
.header("hush-hush", "ignored") | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(200) | ||
.body(Matchers.equalTo(":/admin")); | ||
RestAssured | ||
.given() | ||
.auth() | ||
.preemptive() | ||
.basic("test", "test") | ||
.when() | ||
.header("hush-hush", "ignored") | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(200) | ||
.body(Matchers.equalTo("test:/admin")); | ||
RestAssured | ||
.given() | ||
.auth() | ||
.preemptive() | ||
.basic("test", "test") | ||
.when() | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(403); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.