| Crates.io | axum-jwt-oidc |
| lib.rs | axum-jwt-oidc |
| version | 0.1.1 |
| created_at | 2025-07-05 14:50:39.076293+00 |
| updated_at | 2025-07-05 15:09:44.137847+00 |
| description | Axum middleware for OIDC JWT token validation and claims extraction |
| homepage | |
| repository | https://github.com/soya-miyoshi/axum-jwt-oidc |
| max_upload_size | |
| id | 1739191 |
| size | 75,695 |
Axum middleware for OIDC JWT token validation and claims extraction. This middleware integrates with the async-oidc-jwt-validator crate to provide seamless JWT validation in your Axum applications.
use axum::{Router, routing::get, Extension};
use axum_jwt_oidc::{OidcAuthLayer, OidcConfig, OidcValidator, Validation};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
struct CustomClaims {
sub: String,
email: Option<String>,
// Add your custom claims here
}
#[tokio::main]
async fn main() {
// Initialize OIDC validator
let config = OidcConfig::new(
"https://your-oidc-provider.com".to_string(),
"your-client-id".to_string(),
"https://your-oidc-provider.com/.well-known/jwks.json".to_string(),
);
let oidc_validator = OidcValidator::new(config);
// Configure validation rules
let validation = Validation::default();
// Create the authentication layer
let auth_layer = OidcAuthLayer::<CustomClaims>::new(oidc_validator, validation);
// Build your router with the middleware
let app = Router::new()
.route("/protected", get(protected_handler))
.layer(auth_layer);
// Run your server
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn protected_handler(
Extension(claims): Extension<CustomClaims>,
) -> &'static str {
// Access validated claims here
println!("User ID: {}", claims.sub);
"Protected content"
}
Authorization header (Bearer token)If validation fails, the request continues without claims in the extensions. You can implement your own authorization logic based on the presence or absence of claims.
Add this to your Cargo.toml:
[dependencies]
axum-jwt-oidc = "0.1.1"
Licensed under * MIT license LICENSE-MIT
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See CONTRIBUTING.md.