Crates.io | jwt-actix4 |
lib.rs | jwt-actix4 |
version | 0.3.0 |
source | src |
created_at | 2021-06-05 13:01:10.557275 |
updated_at | 2022-04-15 15:52:32.445031 |
description | JWT authentication middleware for Actix 4 |
homepage | https://snowgoons.ro |
repository | https://gitlab.com/snowgoonspub/jwt-actix4 |
max_upload_size | |
id | 406464 |
size | 19,406 |
JWT bearer authentication that works with actix-web 4.
Resource | Where |
---|---|
Documentation | https://jwt-actix4.snowgoons.ro/jwt_actix/ |
GitLab | https://gitlab.com/snowgoonspub/jwt-actix4 |
There are nice looking crates out there to implement JET bearer auth token
validation with actix-web
. Unfortunately, they do not work with the current
actix-web
version 4 beta, for reasons that no doubt will be addressed in
due course.
At the moment though, because of tokio
dependency hell I needed something
that does work, and also ideally something that was pretty simple to use.
Hence, this crate.
The crate provides a simple middleware, JwtAuth
, that you can insert into
your ActixWeb pipeline:
use jwt_actix::{JwtAuth, CheckJwtValid};
...
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.wrap(JwtAuth::new_from_env(CheckJwtValid).unwrap())
}).bind(addr)?
.run()
.await
}
On every request, the middleware will check for a JWT Bearer auth token in the request. If one is found and it validates correctly, it will call a validation function you provide to check if the request should be processed.
A default validation function, CheckJwtValid
, can also be used which simply
permits the request if the token is valid (i.e. the signature checks out) and
rejects it if not.
The middleware expects to download a JWKS keystore file for the certificates it needs to validate signatures.
There are two constructor functions for the middleware: new_from_env
and
new_from_url
. The latter expects you to provide the URL for a JWKS
keystore; the former will look for it at runtime in the environment
variable JWKS_URL
.
The author of this code is Tim Walls. His homepage on the internet is snowgoons.ro.
This is released under the BSD 3-Clause Open Source License. No warranties are given, express or implied.