-
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
2811eec
commit 05fa0bb
Showing
26 changed files
with
1,403 additions
and
110 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) { | ||
public 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 ""; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...yment/src/test/java/io/quarkus/resteasy/reactive/server/test/security/OtherBeanParam.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,30 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
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 OtherBeanParam { | ||
|
||
@HeaderParam("CustomAuthorization") | ||
private String customAuthorizationHeader; | ||
|
||
@Context | ||
SecurityContext securityContext; | ||
|
||
@Context | ||
public UriInfo uriInfo; | ||
|
||
@QueryParam("query") | ||
public String query; | ||
|
||
public SecurityContext getSecurityContext() { | ||
return securityContext; | ||
} | ||
|
||
public String customAuthorizationHeader() { | ||
return customAuthorizationHeader; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...test/java/io/quarkus/resteasy/reactive/server/test/security/OtherBeanParamPermission.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,48 @@ | ||
package io.quarkus.resteasy.reactive.server.test.security; | ||
|
||
import java.security.BasicPermission; | ||
import java.security.Permission; | ||
|
||
public class OtherBeanParamPermission extends BasicPermission { | ||
|
||
static final OtherBeanParamPermission READ = new OtherBeanParamPermission("read", null, null, null); | ||
|
||
private final String permissionName; | ||
private final String customAuthorization; | ||
private final String userName; | ||
private final String queryParam; | ||
|
||
public OtherBeanParamPermission(String permissionName, String customAuthorizationHeader, String name, String query) { | ||
super(permissionName); | ||
this.permissionName = permissionName; | ||
this.customAuthorization = customAuthorizationHeader; | ||
this.userName = name; | ||
this.queryParam = query; | ||
} | ||
|
||
@Override | ||
public boolean implies(Permission permission) { | ||
if (permission instanceof OtherBeanParamPermission that) { | ||
boolean permissionNameMatches = permissionName.equals(that.permissionName); | ||
boolean queryParamAllowedForPermissionName = checkQueryParams(that.queryParam); | ||
boolean usernameWhitelisted = isUserNameWhitelisted(that.userName); | ||
boolean customAuthorizationMatches = checkCustomAuthorization(that.customAuthorization); | ||
return permissionNameMatches && queryParamAllowedForPermissionName && usernameWhitelisted | ||
&& customAuthorizationMatches; | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean checkCustomAuthorization(String customAuthorization) { | ||
return "customAuthorization".equals(customAuthorization); | ||
} | ||
|
||
private static boolean isUserNameWhitelisted(String userName) { | ||
return "admin".equals(userName); | ||
} | ||
|
||
private static boolean checkQueryParams(String queryParam) { | ||
return "myQueryParam".equals(queryParam); | ||
} | ||
|
||
} |
Oops, something went wrong.