Expand description
§Firebase JWT Authentication Middleware for Axum
A production-ready Firebase JWT token validation middleware for Axum web applications. This crate provides secure token validation with automatic public key caching, comprehensive error handling, and built-in security features.
§Features
- Secure JWT validation with Firebase-specific claim verification
- Automatic public key caching with configurable expiration
- Production-ready error handling with detailed error types
- Security hardening including token length limits and timing validation
- Retry logic with exponential backoff for key fetching
- Comprehensive logging for monitoring and debugging
§Quick Start
use axum::{routing::get, Router, Extension, Json};
use axum::middleware::from_fn_with_state;
use serde_json::json;
use axum_firebase_middleware::{FirebaseClaims, FirebaseConfig, firebase_auth_middleware};
// Your protected handler
async fn protected_handler(
Extension(claims): Extension<FirebaseClaims>
) -> Json<serde_json::Value> {
Json(json!({
"user_id": claims.user_id,
"email": claims.email
}))
}
#[tokio::main]
async fn main() {
let config = FirebaseConfig::new("your-firebase-project-id".to_string())
.expect("Failed to create Firebase config");
let app = Router::new()
.route("/protected", get(protected_handler))
.route_layer(from_fn_with_state(config.clone(), firebase_auth_middleware))
.with_state(config);
// Run your server...
}Structs§
- Firebase
Auth Provider - Firebase authentication provider information.
- Firebase
Claims - Firebase JWT claims structure containing user authentication information.
- Firebase
Config - Firebase authentication configuration.
- Public
KeyCache - Public key cache with automatic refresh and retry logic.
Enums§
- Firebase
Auth Error - Comprehensive error types for Firebase authentication failures.
Functions§
- firebase_
auth_ middleware - Axum middleware for Firebase JWT authentication.