Skip to main content

Crate axum_jwt_bridge

Crate axum_jwt_bridge 

Source
Expand description

§axum-jwt-bridge

JWT encode/decode for Axum microservices, compatible with Laravel’s tymon/jwt-auth and any HS256 JWT issuer.

Extracts user_id: u32 from the sub claim. Role-based authorization is the consuming application’s responsibility.

§Usage

use axum::{routing::get, Extension, Router};
use axum_jwt_bridge::{AuthUser, JwtConfig};

async fn handler(user: AuthUser) -> String {
    format!("user_id = {}", user.user_id)
}

#[tokio::main]
async fn main() {
    // Ensure JWT_SECRET (and optionally BASE_URL, AUTH_PATH,
    // USER_MODEL_PATH) are set in the environment before calling from_env.
    let config = JwtConfig::from_env().unwrap();

    let app: Router = Router::new()
        .route("/me", get(handler))
        .layer(Extension(config));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

§Custom extra claims

use serde::{Deserialize, Serialize};
use axum_jwt_bridge::{Claims, verify_jwt_as, generate_jwt_with, JwtConfig};

#[derive(Debug, Clone, Serialize, Deserialize)]
struct MyExtra {
    #[serde(default)]
    tenant_id: Option<String>,
}

let config = JwtConfig::new("secret");
let token = generate_jwt_with(42, &config, MyExtra { tenant_id: Some("acme".into()) }).unwrap();
let claims: Claims<MyExtra> = verify_jwt_as(&token, &config).unwrap();
assert_eq!(claims.extra.tenant_id.as_deref(), Some("acme"));

§Environment variables (JwtConfig::from_env)

This Mod does not load .env files.

VariableRequiredDefaultNotes
JWT_SECRETone of—HS256 shared secret
JWT_KEY_NAMEthese—EdDSA: SSH-convention key name — ~/.ssh/{name} / ~/.ssh/{name}.pub
JWT_KEY_PATHthree—EdDSA: exact PEM file path, used as-is
BASE_URLno(unset — no iss claim)Combined with AUTH_PATH to form iss
AUTH_PATHno/api/loginOnly used if BASE_URL is set
JWT_TTL_DAYSno14Token lifetime in days
USER_MODEL_PATHno(unset — no prv)Sets prv claim and enables its check
JWT_VALIDATE_ISSUERnofalsetrue or 1 to enable
JWT_AUDIENCEno(unset)Comma-separated audience URIs

For JWT_KEY_NAME / JWT_KEY_PATH, the PEM itself isn’t read here — it’s read from disk lazily, at sign/verify time. See AuthType::PubPriKey / AuthType::PubPriPath for details.

§Laravel migration

To validate tokens still being issued by a Laravel tymon/jwt-auth server, share the same JWT_SECRET and set USER_MODEL_PATH (which auto-enables prv validation):

use axum_jwt_bridge::JwtConfig;

// Code-based setup:
let config = JwtConfig::laravel_compat("your-jwt-secret", "App\\Models\\User");

// Or via environment (JWT_SECRET + USER_MODEL_PATH):
let config = JwtConfig::from_env().unwrap();

When your Rust services start issuing tokens too, the same config signs them with the same prv hash, so Laravel can validate them without any changes.

Re-exports§

pub use claims::Claims;
pub use claims::NoExtraClaims;
pub use config::AuthType;
pub use config::JwtConfig;
pub use config::MultiJwtConfig;
pub use config::ProviderStrategy;
pub use error::AuthError;
pub use middleware::AuthUser;
pub use middleware::OptionalAuthUser;
pub use token::generate_jti;
pub use token::generate_jwt;
pub use token::generate_jwt_with;
pub use token::verify_jwt;
pub use token::verify_jwt_as;
pub use token::verify_jwt_any;
pub use token::verify_jwt_any_as;

Modules§

claims
config
error
middleware
token