| Crates.io | revoke-auth |
| lib.rs | revoke-auth |
| version | 0.5.0 |
| created_at | 2025-07-21 16:23:13.704877+00 |
| updated_at | 2025-07-21 16:23:13.704877+00 |
| description | High-performance microservices infrastructure framework built with pure Rust |
| homepage | |
| repository | https://github.com/revoke/revoke |
| max_upload_size | |
| id | 1762240 |
| size | 69,540 |
Revoke Auth is a high-performance JWT authentication library for Axum web applications, built with pure Rust. It provides seamless JWT token validation with JWKS (JSON Web Key Set) support and custom claims extraction.
Add this to your Cargo.toml:
[dependencies]
revoke-auth = "0.5.0"
axum = "0.8"
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
First, implement the JwtClaims trait for your custom claims structure:
use revoke_auth::JwtClaims;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UserClaims {
pub sub: String, // Subject (user ID)
pub email: String, // User email
pub role: String, // User role
pub exp: usize, // Expiration time
pub iat: usize, // Issued at
}
impl JwtClaims for UserClaims {
fn sub(&self) -> &str {
&self.sub
}
}
Configure your application with JWKS store and authentication config:
use revoke_auth::{JwksStore, AuthConfig, Auth};
use axum::{Router, routing::get, response::Json};
#[derive(Clone)]
pub struct AppState {
pub jwks_store: JwksStore,
pub auth_config: AuthConfig,
}
impl AsRef<JwksStore> for AppState {
fn as_ref(&self) -> &JwksStore {
&self.jwks_store
}
}
impl AsRef<AuthConfig> for AppState {
fn as_ref(&self) -> &AuthConfig {
&self.auth_config
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize JWKS store with your identity provider's JWKS URL
let jwks_store = JwksStore::new(
"https://your-auth-provider.com/.well-known/jwks.json".to_string()
).await?;
// Configure authentication parameters
let auth_config = AuthConfig {
issuer: "https://your-auth-provider.com".to_string(),
audience: Some("your-audience".to_string()),
};
let state = AppState {
jwks_store,
auth_config,
};
let app = Router::new()
.route("/protected", get(protected_handler))
.with_state(state);
// Start server
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
axum::serve(listener, app).await?;
Ok(())
}
Use the Auth<T> extractor in your route handlers:
use axum::{response::Json, http::StatusCode};
use serde_json::{json, Value};
// Protected route that requires authentication
async fn protected_handler(
Auth(claims): Auth<UserClaims>
) -> Result<Json<Value>, StatusCode> {
Ok(Json(json!({
"message": "Access granted",
"user_id": claims.sub(),
"email": claims.email,
"role": claims.role
})))
}
Send requests with JWT tokens in the Authorization header:
curl -H "Authorization: Bearer your-jwt-token-here" \
http://localhost:3000/protected
| Field | Type | Description | Required |
|---|---|---|---|
issuer |
String |
JWT issuer URL | Yes |
audience |
Option<String> |
Expected audience claim | No |
The library returns HTTP status codes for different error conditions:
401 Unauthorized: Invalid or missing JWT token401 Unauthorized: Token validation failed (expired, wrong issuer, etc.)401 Unauthorized: JWKS key not found for tokenA trait that must be implemented by your custom claims structure:
pub trait JwtClaims: DeserializeOwned + Send + Sync + 'static {
fn sub(&self) -> &str;
}
An Axum extractor for extracting authenticated claims from requests.
Manages JWKS key fetching and caching:
impl JwksStore {
pub async fn new(url: String) -> Result<Self>;
pub async fn refresh(&self) -> Result<()>;
pub async fn get_key(&self, kid: &str) -> Option<DecodingKey>;
}
Configuration for JWT validation:
pub struct AuthConfig {
pub issuer: String,
pub audience: Option<String>,
}
This project is licensed under either Apache License 2.0 or MIT license at your option.