Crate axum_firebase_middleware

Crate axum_firebase_middleware 

Source
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§

FirebaseAuthProvider
Firebase authentication provider information.
FirebaseClaims
Firebase JWT claims structure containing user authentication information.
FirebaseConfig
Firebase authentication configuration.
PublicKeyCache
Public key cache with automatic refresh and retry logic.

Enums§

FirebaseAuthError
Comprehensive error types for Firebase authentication failures.

Functions§

firebase_auth_middleware
Axum middleware for Firebase JWT authentication.