rocket-grants
Extension for
rocketto validate user permissions.
To check user access to specific services, you can use built-in proc-macro, PermissionGuard or manual.
Provides a complete analogue of the actix-web-grants and poem-grants.
How to use
- Declare your own permission extraction function
The easiest way is to declare a function with the following signature:
// You can use custom type instead of String
async
- Add fairing to your application using the extraction function defined in step 1
build.mount
.attach
Steps 1 and 2 can be replaced by integration with your custom fairing.
- Protect your endpoints in any convenient way from the examples below:
Example of proc-macro way protection
async
Here is an example using the type and secure attributes. But these are independent features.
secure allows you to include some checks in the macro based on function params.
type allows you to use a custom type for the roles and permissions (then the fairing needs to be configured).
Take a look at an enum-role example
use ;
use User;
async
Example of manual way protection
use ;
async
You can find more examples in the git repository folder and documentation.
Error customization
Custom error responses can be specified using Rocket catchers. See the Rocket documentation for catchers.
You can set up custom responses for:
401 Unauthorized - when it wasn't possible to obtain authorization data from the request in your extractor.
403 Forbidden - when the permissions did not match the specified for the endpoint.
Supported rocket versions
- For
rocket-grants: 0.5.*supported version ofrocketis0.5.*