This crate builds upon the jwt-compact crate
to provide a jwt authentication middleware for the actix-web framework.
The jwt implementation supports the revocation for tokens via access and refresh tokens.
It provides multiple cryptographic signing and verifying algorithms such as HS256, HS384, HS512, EdDSA and ES256.
For more infos on that mater please refer to the Supported algorithms section of the jwt-compact crate.
query parameters, http headers and cookiespublic key required)access token (customizable)access and refresh tokensUseJWT trait for protecting a App or Scope (Resource is currently experimental #91611)This crate tightly integrates into the actix-web ecosystem, this makes it easy to Automatic extract the jwt claims from a valid token. ```rust
struct UserClaims { id: u32, role: Role, }
enum Role { Admin, RegularUser, }
async fn hello(userclaims: UserClaims) -> impl Responder {
format!(
"Hello user with id: {}, i see you are a {:?}!",
userclaims.id, user_claims.role
)
}
``
For this your custom claim type has to implement the [FromRequest](actix_web::FromRequest) trait
or it has to be annotated with the#[derive(actix-jwt-auth-middleware::FromRequest)]` macro which implements this trait for your type.
```rust
struct User { id: u32, }
async fn main() -> Result<(), Box
HttpServer::new(move || {
let authority = Authority::<User, Ed25519, _, _>::new()
.refresh_authorizer(|| async move { Ok(()) })
.token_signer(Some(
TokenSigner::new()
.signing_key(key_pair.secret_key().clone())
.algorithm(Ed25519)
.build()
.expect(""),
))
.verifying_key(key_pair.public_key())
.build()
.expect("");
App::new()
.service(login)
.use_jwt(authority, web::scope("").service(hello))
})
.bind(("127.0.0.1", 8080))?
.run()
.await?;
Ok(())
}
async fn login(tokensigner: web::Data
async fn hello(user: User) -> impl Responder {
format!("Hello there, i see your user id is {}.", user.id)
}
``
For more examples please referee to theexamples` directory.
License: MIT