#jwt #token #bearer #web #msal

passport_azure_ad

JWT bearer token validation and verification towards Microsoft Azure AD

1 unstable release

0.1.3 Dec 6, 2023
0.1.2 Jun 7, 2023
0.1.1 Jun 6, 2023
0.1.0 Jun 6, 2023

#320 in Authentication

44 downloads per month

MIT and GPL-3.0-or-later

26KB
567 lines

rust-passport-azure-ad

Port of passport-azure-ad to Rust

Installation

cargo add passport_azure_ad

Usage

use passport_azure_ad::{
    bearer_strategy::BearerStrategy,
    types::LogLevel,
    util,
};
use std::env;
use dotenvy::dotenv;

#[tokio::test]
async fn test_msal_bearer() {
    dotenv().ok();
    let token = env::var("BEARER_TOKEN")
        .expect("'BEARER_TOKEN' is not defined")
        .to_string();
    let client_id = env::var("AZURE_AD_CLIENT_ID")
        .expect("'AZURE_AD_CLIENT_ID' is not defined")
        .to_string();
    let tenant_id = env::var("AZURE_AD_TENANT_ID")
        .expect("'AZURE_AD_TENANT_ID' is not defined")
        .to_string();

    let bearer = BearerStrategy::build(
        Some(false),                                       // allow_multi_audiences
        None,                                              // audience
        Some(client_id),                                   // client_id
        None,                                              // clock_skew
        Some(util::open_id_config_url(tenant_id.clone())), // identity_metadata
        Some(false),                                       // ignore_expiration
        Some(false),                                       // is_b2c
        Some(vec![util::issuer_url(tenant_id)]),           // issuer
        Some(LogLevel::Trace),                             // log_level
        None,                                              // policy_name
        Some(vec![String::from("api-access")]),            // scope
        Some(true),                                        // validate_issuer
    )
    .unwrap();

    let validated = bearer.authenticate(token).await;

    assert!(validated.is_ok());
}

License

MIT

Dependencies

~8–23MB
~355K SLoC