-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve PermissionsAllowed user experience with shortcuts
- Loading branch information
1 parent
8e9154b
commit f7912f0
Showing
3 changed files
with
144 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.quarkus.security; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used to annotate CDI bean methods that check if a user holds a permission specified by the {@link #value()}. | ||
* Quarkus Security will augment every {@link io.quarkus.security.identity.SecurityIdentity} with this permission checker. | ||
* Such a permission checker grants access to methods secured with the {@link PermissionsAllowed} security annotation. | ||
* Following example shows how it works: | ||
* <pre> | ||
* {@code | ||
* @Path("hello") | ||
* public class HelloResource { | ||
* | ||
* @PermissionsAllowed("speak") | ||
* @GET | ||
* public String sayHello() { | ||
* return "Hello World!"; | ||
* } | ||
* | ||
* @PermissionChecker("speak") | ||
* public boolean canSpeak(SecurityIdentity identity) { | ||
* return "speaker".equals(identity.getPrincipal().getName()); | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
* Parameters of permission checker methods can include any of secured method parameters. | ||
* Consider following secured method: | ||
* <pre> | ||
* {@code | ||
* @PermissionsAllowed("update") | ||
* public String updateString(String a, String b, String c, String d) { | ||
* ... | ||
* } | ||
* } | ||
* </pre> | ||
* Permission checker that grants access to the {@code updateString} method can inject | ||
* any arguments it requires and optionally even {@link io.quarkus.security.identity.SecurityIdentity}: | ||
* <pre> | ||
* {@code | ||
* @PermissionChecker("update") | ||
* public boolean canUpdate(String c, String a, SecurityIdentity identity) { | ||
* ... | ||
* } | ||
* } | ||
* </pre> | ||
* Permission checker method parameters are matched with the secured method parameters in exactly same fashion | ||
* as are constructor parameters of a custom permission. Please see {@link PermissionsAllowed#params()} for more information. | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface PermissionChecker { | ||
|
||
/** | ||
* Specifies a permission this checker grants. | ||
* | ||
* @see PermissionsAllowed#value() | ||
* @return name of the permission this checker grants | ||
*/ | ||
String value(); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.security; | ||
|
||
import io.quarkus.security.identity.SecurityIdentity; | ||
|
||
import java.security.Permission; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Special type of the {@link PermissionsAllowed#permission()} that does not require {@link SecurityIdentity} | ||
* augmentation. When this permission is set to the {@link PermissionsAllowed#permission()}, Quarkus Security augments | ||
* all the {@link SecurityIdentity} with a permission checker for you. | ||
* This way, access to methods secured with the {@link PermissionsAllowed} annotation will only be granted | ||
* when {@link #isGranted(SecurityIdentity)} returns true. | ||
*/ | ||
public abstract class QuarkusPermission extends Permission { | ||
|
||
private static final String QUARKUS_PERMISSION = "io.quarkus.security.permission#name"; | ||
|
||
/** | ||
* Permission name is set to the {@link #QUARKUS_PERMISSION}. | ||
* Subclasses can declare constructors that accept permission name and/or arguments of a secured method. | ||
* | ||
* @see PermissionsAllowed#params() for more information about additional Permission arguments | ||
*/ | ||
protected QuarkusPermission() { | ||
this(QUARKUS_PERMISSION); | ||
} | ||
|
||
/** | ||
* Subclasses can declare constructors that accept permission name and/or arguments of a secured method. | ||
* | ||
* @see PermissionsAllowed#params() for more information about additional Permission arguments | ||
*/ | ||
protected QuarkusPermission(String permissionName) { | ||
super(permissionName); | ||
} | ||
|
||
/** | ||
* Determines whether access to secured resource should be granted. | ||
* | ||
* @param securityIdentity {@link SecurityIdentity} | ||
* @return true if access should be granted and false otherwise | ||
*/ | ||
public abstract boolean isGranted(SecurityIdentity securityIdentity); | ||
|
||
/** | ||
* @throws IllegalStateException for this permission can only be set to the {@link PermissionsAllowed#permission()} | ||
*/ | ||
@Override | ||
public final boolean implies(Permission requiredPermission) { | ||
// possessed permission implies required permission | ||
// this is required permission, not the possessed one | ||
throw new IllegalStateException("QuarkusPermission should never be assigned to a SecurityIdentity. " | ||
+ "This permission can only be set to the @PermissionsAllowed#permission attribute."); | ||
} | ||
|
||
@Override | ||
public String getActions() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
return this == object; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(toString()); | ||
} | ||
|
||
} |