PropelAuth Rust Library
Add authentication and authorization to your application.
This library is meant to be used with a PropelAuth account. You can sign up and get started for free.
Initialize
First, you'll need to initialize the library. You can either call PropelAuth::init
or PropelAuth::fetch_and_init (which will fetch any unspecified metadata).
let auth = fetch_and_init.await.expect;
Usage / Protecting APIs
Want us to add support for another framework? Reach out at support@propelauth.com
Axum
To use Axum, make sure to enable the axum feature in your Cargo.toml.
Then, add PropelAuthLayer to your Router:
let auth_layer = new;
let app = new
.route
.route
.layer; // <-- here
You can then take User in as an argument, which will look for an access token in the Authorization header.
// User will automatically return a 401 (Unauthorized) if a valid access token wasn't provided
async
You can also check which organizations the user is in, and which roles and permissions they have.
// If the user isn't in the provided organization, a 403 is returned
async
You can also get the full auth struct and make API calls with it:
// Extension(auth) is useful for making API requests
async
Actix
To use Actix, make sure to enable the actix4 feature in your Cargo.toml.
Add your PropelAuth to your Router:
let auth = fetch_and_init
//...
new
You can then take User in as an argument, which will look for an access token in the Authorization header.
// User will automatically return a 401 (Unauthorized) if a valid access token wasn't provided
async
You can also check which organizations the user is in, and which roles and permissions they have.
// If the user isn't in the provided organization, a 403 is returned
async
You can also get the full auth struct and make API calls with it:
async
Rustls instead of OpenSSL
If you'd rather use a pure Rust TLS implementation rather than OpenSSL disable the default features and enable rustls as so:
= { = "0.12.1", = ["rustls"], = false }
Other
After initializing auth, you can verify access tokens by passing in the Authorization header (formatted Bearer TOKEN):
let result = auth.verify.validate_authorization_header;
match result
You can also check which organizations the user is in, and which roles and permissions they have.
let org = auth.validate_org_membership?;
// Alternatively, if you already have a user from validate_authorization_header
let org = user.validate_org_membership?;
And finally, you can make API calls directly from auth.user() and auth.org()
Where do the access tokens come from?
They come from your frontend. You can read more about integrating your frontend here.