-
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.
Support @PermissionsAllowed with @BeanParam parameters
- Loading branch information
1 parent
c1531d0
commit 48e28a4
Showing
22 changed files
with
1,082 additions
and
21 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
11 changes: 11 additions & 0 deletions
11
...ployment/src/test/java/io/quarkus/resteasy/reactive/server/test/security/MyBeanParam.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,11 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import jakarta.ws.rs.BeanParam; | ||
|
||
import org.jboss.resteasy.reactive.RestHeader; | ||
import org.jboss.resteasy.reactive.RestQuery; | ||
|
||
public record MyBeanParam(@RestQuery String queryParam, @BeanParam Headers headers) { | ||
record Headers(@RestHeader String authorization) { | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...loyment/src/test/java/io/quarkus/resteasy/reactive/server/test/security/MyPermission.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,47 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import java.security.Permission; | ||
import java.util.Objects; | ||
|
||
public class MyPermission extends Permission { | ||
|
||
static final MyPermission EMPTY = new MyPermission("my-perm", null, null); | ||
|
||
private final String authorization; | ||
private final String queryParam; | ||
|
||
public MyPermission(String permissionName, String authorization, String queryParam) { | ||
super(permissionName); | ||
this.authorization = authorization; | ||
this.queryParam = queryParam; | ||
} | ||
|
||
@Override | ||
public boolean implies(Permission permission) { | ||
if (permission instanceof MyPermission myPermission) { | ||
return myPermission.authorization != null && "query1".equals(myPermission.queryParam); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
MyPermission that = (MyPermission) o; | ||
return Objects.equals(authorization, that.authorization) | ||
&& Objects.equals(queryParam, that.queryParam); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(authorization, queryParam); | ||
} | ||
|
||
@Override | ||
public String getActions() { | ||
return ""; | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...va/io/quarkus/resteasy/reactive/server/test/security/PermissionsAllowedBeanParamTest.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,107 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import jakarta.ws.rs.BeanParam; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.resteasy.reactive.RestCookie; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.PermissionsAllowed; | ||
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.specification.RequestSpecification; | ||
|
||
public class PermissionsAllowedBeanParamTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(TestIdentityProvider.class, TestIdentityController.class, SimpleBeanParam.class, | ||
SimpleResource.class, SimpleBeanParamPermission.class, MyPermission.class, MyBeanParam.class)); | ||
|
||
@BeforeAll | ||
public static void setupUsers() { | ||
TestIdentityController.resetRoles() | ||
.add("admin", "admin", SimpleBeanParamPermission.EMPTY, MyPermission.EMPTY) | ||
.add("user", "user"); | ||
} | ||
|
||
@Test | ||
public void testSimpleBeanParam() { | ||
getSimpleBeanParamReq() | ||
.post("/simple/param") | ||
.then().statusCode(401); | ||
getSimpleBeanParamReq() | ||
.auth().preemptive().basic("user", "user") | ||
.post("/simple/param") | ||
.then().statusCode(403); | ||
getSimpleBeanParamReq() | ||
.auth().preemptive().basic("admin", "admin") | ||
.post("/simple/param") | ||
.then().statusCode(200).body(Matchers.equalTo("OK")); | ||
} | ||
|
||
@Test | ||
public void testRecordBeanParam() { | ||
RestAssured | ||
.given() | ||
.auth().preemptive().basic("user", "user") | ||
.queryParam("queryParam", "query1") | ||
.get("/simple/record-param") | ||
.then().statusCode(403); | ||
RestAssured | ||
.given() | ||
.auth().preemptive().basic("admin", "admin") | ||
.queryParam("queryParam", "query1") | ||
.get("/simple/record-param") | ||
.then().statusCode(200) | ||
.body(Matchers.equalTo("OK")); | ||
RestAssured | ||
.given() | ||
.auth().preemptive().basic("admin", "admin") | ||
.queryParam("queryParam", "wrong-query-param") | ||
.get("/simple/record-param") | ||
.then().statusCode(403); | ||
} | ||
|
||
private static RequestSpecification getSimpleBeanParamReq() { | ||
return RestAssured | ||
.with() | ||
.header("header", "one-header") | ||
.queryParam("query", "one-query") | ||
.queryParam("queryList", "one") | ||
.queryParam("queryList", "two") | ||
.queryParam("int", "666") | ||
.cookie("cookie", "cookie") | ||
.body("OK"); | ||
} | ||
|
||
@Path("/simple") | ||
public static class SimpleResource { | ||
|
||
@PermissionsAllowed(value = "perm1", permission = SimpleBeanParamPermission.class, params = { "cookie", | ||
"beanParam.query", | ||
"beanParam.protectedQuery", "beanParam.publicQuery", "beanParam.header", "beanParam.queryList", | ||
"beanParam.securityContext", "beanParam.uriInfo", "beanParam.privateQuery" }) | ||
@Path("/param") | ||
@POST | ||
public String simpleBeanParam(@BeanParam SimpleBeanParam beanParam, String payload, @RestCookie String cookie) { | ||
return payload; | ||
} | ||
|
||
@PermissionsAllowed(value = "perm2", permission = MyPermission.class, params = { "beanParam.queryParam", | ||
"beanParam.headers.authorization" }) | ||
@Path("/record-param") | ||
@GET | ||
public String recordBeanParam(@BeanParam MyBeanParam beanParam) { | ||
return "OK"; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ment/src/test/java/io/quarkus/resteasy/reactive/server/test/security/SimpleBeanParam.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,39 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.ws.rs.HeaderParam; | ||
import jakarta.ws.rs.QueryParam; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
import jakarta.ws.rs.core.UriInfo; | ||
|
||
public class SimpleBeanParam { | ||
@QueryParam("query") | ||
String query; | ||
|
||
@QueryParam("query") | ||
private String privateQuery; | ||
|
||
@QueryParam("query") | ||
protected String protectedQuery; | ||
|
||
@QueryParam("query") | ||
public String publicQuery; | ||
|
||
@HeaderParam("header") | ||
String header; | ||
|
||
@QueryParam("queryList") | ||
List<String> queryList; | ||
|
||
@Context | ||
SecurityContext securityContext; | ||
|
||
@Context | ||
UriInfo uriInfo; | ||
|
||
String getPrivateQuery() { | ||
return privateQuery; | ||
} | ||
} |
Oops, something went wrong.