Mozambigue
A generic, extensible Rust library for JWT (JSON Web Token) validation with JWKS (JSON Web Key Set) caching support.
Designed for flexibility: Mozambigue provides a trait-based architecture that makes it easy to add support for different JWT providers while maintaining type safety and zero serialization overhead.
Features
- ✅ Generic architecture - Trait-based system supporting multiple JWT providers
- ✅ Zero serialization overhead - Direct field access via
StandardClaimstrait - ✅ JWT signature verification (RSA and Octet keys)
- ✅ Automatic JWKS fetching from OpenID configuration endpoints
- ✅ Configurable JWKS caching with TTL
- ✅ Secure audience validation - Validates against configured expected audiences
- ✅ Issuer and expiration validation
Currently Supported Providers
- Kubernetes - Service account token validation with namespace and service account extraction
Ready to Add
The architecture is ready for additional providers:
- Standard OIDC (email, name, profile)
- Auth0 (roles, permissions, metadata)
- Google Sign-In
- Azure AD / Entra ID
- Keycloak (realm roles, client roles)
- Any custom OIDC provider
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
Kubernetes Service Account Tokens
use ;
async
Generic Usage (Custom Provider)
use ;
use Deserialize;
// 1. Define your claims structure
// 2. Implement StandardClaims
// 3. Define your identity type
// 4. Create your extractor
;
// 5. Use it!
async
Architecture
Provider-Based Structure
mozambigue/
├── Generic Infrastructure
│ ├── JwtVerifier<E> - Generic verifier
│ ├── IdentityExtractor - Trait for extractors
│ ├── StandardClaims - Trait for claims access
│ └── VerifyJwt - Verification trait
│
└── providers/
└── kubernetes/ - Kubernetes implementation
├── KubernetesClaims
├── KubernetesIdentity
├── KubernetesExtractor
└── KubernetesJwtVerifier
How It Works
- Token Parsing: Parse JWT to extract issuer (without validation)
- JWKS Fetching: Fetch JWKS from
{issuer}/.well-known/openid-configuration(cached) - Signature Verification: Verify signature using key from JWKS
- Claims Validation: Validate issuer, expiration, and audience
- Identity Extraction: Provider-specific extraction via
IdentityExtractortrait
Examples
Kubernetes: Custom Configuration
use ;
use Duration;
let config = new
.with_cache_ttl; // 30 minutes
let verifier = new.await?;
let identity = verifier.verify.await?;
Multiple Audiences
use JwtVerifierConfig;
let config = new_with_audiences?
.with_cache_ttl;
let verifier = new.await?;
Custom HTTP Client
let custom_client = builder
.timeout
.build?;
let config = new
.with_http_client;
let verifier = new.await?;
Using Explicit Provider Path
use JwtVerifier;
use ;
let verifier = new.await?;
let identity: KubernetesIdentity = verifier.verify.await?;
JWKS Caching
Efficient caching reduces network calls:
- Configurable TTL (default: 1 hour)
- Automatic cache expiration
- Thread-safe with
Arc<RwLock<HashMap>> - Per-issuer caching
Implementing Custom Providers
Want to add support for Auth0, Google, or your custom OIDC provider? It's easy:
- Define your claims structure with provider-specific fields
- Implement
StandardClaimsfor standard field access - Define your identity type with extracted information
- Implement
IdentityExtractorwith your extraction logic
See the Kubernetes provider for a complete example.
Examples
See the examples directory:
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.