jwtiny
Minimal JSON Web Token (JWT) validation for Rust.
jwtiny validates JWT tokens efficiently in production Rust applications. The validator follows a reusable pattern: configure it once at application startup, then verify tokens with minimal allocations. The validator can be shared across requests, which reduces memory footprint and improves performance.
The library supports RSA algorithms (RS256, RS384, RS512) with an aws-lc-rs backend, and provides JWKS support for remote key fetching over HTTPS (rustls) with caching. It's been tested with Axum, Poem, Rocket, and Warp.
Installation
Add jwtiny to your Cargo.toml:
Quick Start
Static Key Validation
When you have a static public key, configure the validator like this:
use ;
let validator = new
.algorithms
.validate
.key
.build;
let claims = validator.verify.await?;
That's it! The validator's ready to use. You can call verify() as many times as you need—the library is designed for reuse.
JWKS Validation (Remote Key Fetching)
For production systems, you'll often want to fetch keys from a JWKS endpoint. Here's how the library sets this up:
use ;
use Cache;
use Duration;
let client = new;
let cache = builder
.time_to_live
.max_capacity
.build;
let validator = new
.algorithms
.issuer
.validate
.jwks
.cache
.build;
let claims = validator.verify.await?;
The cache reduces network requests and improves performance. Set the TTL to match the key rotation schedule of the identity provider.
For testing, this works fine with JWKServe as well.
Custom Claims
If you need custom claim structures, use the #[claims] macro:
use ;
let validator = new
.algorithms
.validate
.key
.build;
let claims = validator..await?;
The macro handles the standard claims (iss, sub, aud, exp, nbf, iat, jti) automatically, so you only need to define your custom fields.
API Reference
TokenValidator
Configure the validator once, then reuse it for multiple verifications:
let validator = new
.algorithms // RS256, RS384, RS512, or rsa_all()
.issuer
.validate
.key // Static key (mutually exclusive with jwks)
.jwks // JWKS (mutually exclusive with key)
.cache // Optional: cache JWKS keys
.build;
// Verify tokens (reusable)
let claims = validator.verify.await?;
let custom = validator..await?;
AlgorithmPolicy
Control which algorithms are accepted:
rs256_only // RS256 only
rs512_only // RS512 only
rsa_all // All RSA (default)
ClaimsValidation
Configure temporal and audience validation:
default
.require_audience
.max_age
.clock_skew
.no_exp_validation
.no_nbf_validation
.no_iat_validation
By default, the validator checks expiration (exp), not-before (nbf), and issued-at (iat), with a max age of 30 minutes and no clock skew. In distributed systems, adding clock skew tolerance can help handle time synchronisation differences.
Error Handling
All validation errors are returned as jwtiny::Error:
match validator.verify.await
Examples
Complete working examples for various web frameworks:
- Axum:
examples/axum/ - Poem:
examples/poem/ - Rocket:
examples/rocket/ - Warp:
examples/warp/
Run an example:
License
MIT