axum-jwt

Crates.ioaxum-jwt
lib.rsaxum-jwt
version0.1.3
created_at2025-08-01 03:57:46.080279+00
updated_at2025-09-24 11:43:30.257381+00
descriptionAxum JWT extractors and middleware
homepage
repositoryhttps://github.com/nanoqsh/axum-jwt
max_upload_size
id1776080
size52,531
Nano (nanoqsh)

documentation

https://docs.rs/axum-jwt

README

axum-jwt

JSON Web Token extractors and middleware for axum framework

About

The library provides extractors for performing JWT authentication. Under the hood, tokens are parsed using the jsonwebtoken crate. For more details, see the documentation.

Example

In this example, the request token is validated and the username is extracted:

use {
    axum::{Router, routing},
    axum_jwt::{Claims, Decoder, jsonwebtoken::DecodingKey},
    serde::Deserialize,
    std::io::Error,
    tokio::net::TcpListener,
};

#[derive(Deserialize)]
struct User {
    sub: String,
}

async fn hello(Claims(u): Claims<User>) -> String {
    format!("Hello, {}!", u.sub)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let decoder = Decoder::from_key(DecodingKey::from_secret(b"secret"));

    let app = Router::new()
        .route("/", routing::get(hello))
        .with_state(decoder);

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

In case of failed authentication, for example if the token is invalid or expired, a 401 Unauthorized status code is returned.

Commit count: 31

cargo fmt