17 releases (3 stable)

1.2.0 Dec 25, 2023
1.1.0 Nov 11, 2023
1.0.0 Jan 24, 2023
0.6.1 Nov 22, 2022
0.3.1 Nov 24, 2021

#84 in HTTP server

Download history 56/week @ 2023-10-30 75/week @ 2023-11-06 81/week @ 2023-11-13 85/week @ 2023-11-20 163/week @ 2023-11-27 27/week @ 2023-12-04 49/week @ 2023-12-11 30/week @ 2023-12-18 200/week @ 2023-12-25 159/week @ 2024-01-01 149/week @ 2024-01-08 60/week @ 2024-01-15 32/week @ 2024-01-22 148/week @ 2024-01-29 37/week @ 2024-02-05 107/week @ 2024-02-12

324 downloads per month

MIT license

41KB
791 lines

Actix 4 compatible JWT authentication

In order to make use of this crate, you can add it to your Cargo.toml

This crate is build with actix-4.

actix-4-jwt-auth = "1.2.0"

Or when you like to use the latest as found on github:

actix-4-jwt-auth = {git = "https://github.com/spectare/actix-4-jwt-auth", branch = "main"}

Works with extractors

    #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
    pub struct FoundClaims {
        pub iss: String,
        pub sub: String,
        pub aud: String,
        pub name: String,
        pub email: Option<String>,
        pub email_verified: Option<bool>,
    }

    #[get("/authenticated_user")]
    async fn authenticated_user(user: AuthenticatedUser<FoundClaims>) -> String {
        format!("Welcome {}!", user.claims.name)
    }

Is a Actix endpoint URL that extracts the AuthenticatedUser from the JWT based Authorization Bearer header.

You can wire your application like

      let authority = "https://a.valid.openid-connect.idp/".to_string();

      let oidc = Oidc::new(OidcConfig::Issuer(authority.clone().into())).await.unwrap();

      let biscuit_validator = OidcBiscuitValidator { options: ValidationOptions {
              issuer: Validation::Validate(authority),
              ..ValidationOptions::default()
          }
      };

      HttpServer::new(move || {
        App::new()
                .app_data(oidc.clone())
                .wrap(biscuit_validator.clone())
                // .wrap(OidcBiscuitValidator::default()) //without issuer verification
                .service(authenticated_user),
        })
      .bind("0.0.0.0:8080".to_string())?
      .run()
      .await

This will find the token from Authorization header value if you use Oidc::new

You can override the token lookup location (custom header or cookie) by importing TokenLookup enum

use actix_4_jwt_auth::{Oidc, OidcConfig, TokenLookup};

If you want you use custom header:

let token_lookup = TokenLookup::Header("x-custom-auth-header".into());

or use custom cookie:

let token_lookup = TokenLookup::Cookie("x-custom-auth-cookie".into());

and pass token_lookup as Oidc::new_with_token_lookup's second parameter

let oidc = Oidc::new_with_token_lookup(OidcConfig::Issuer(authority.clone().into()), token_lookup).await.unwrap();

More documentation is found on docs.rs

Dependencies

~25–39MB
~791K SLoC