rocket-grants
Extension for
rocketto authorize requests.
To check user access to specific endpoints, you can use built-in proc-macro or do it manually.
How to use
- Declare your own authorities 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 ty and expr attributes. But these are independent features.
expr allows you to include some checks in the macro based on function params, it can be combined with authorities by using all/any.
ty allows you to use a custom type for the authority (then the fairing needs to be configured).
Take a look at an enum-role example
use ;
use User;
async
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.1.*supported version ofrocketis0.5.*