libauth-rs
A unified authentication library for Rust with support for multiple auth providers (Clerk, Stytch, MSAL) and Axum middleware integration.
Features
- Multiple Auth Providers: Support for Clerk, Stytch, and Microsoft Azure AD / MSAL
- Feature-Gated: Only compile and include the providers you need
- Axum Middleware: Drop-in authentication middleware for Axum web applications
- Provider-Specific Authorization: Built-in support for role-based and permission-based authorization
- Issuer-Based Routing: Automatically route JWT tokens to the correct provider based on issuer
- Per-Provider Routers: Create separate Axum routers for each authentication provider
- Type-Safe: Strongly typed authentication primitives
- Async-First: Built on async/await for high performance
- API-Agnostic: Works with any HTTP framework or API style (REST, GraphQL, etc.)
Installation
Add to your Cargo.toml:
[]
= { = ".", = ["clerk", "axum"] }
Feature Flags
authentication- Core authentication functionalityauthorization- Authorization and permissions (WIP)clerk- Clerk authentication providerstytch- Stytch authentication providermsal- Microsoft Azure AD / MSAL authentication provideraxum- Axum middleware and extractorsall-providers- Enable all authentication providers
Features not yet implemented
scim- SCIM support
Usage
With Axum Middleware
use ;
use *;
async
// Public handler - authentication is optional
async
// Protected handler - authentication is required
async
Environment Variables
Clerk
CLERK_SECRET_KEY=sk_test_...
CLERK_PUBLISHABLE_KEY=pk_test_...
Stytch
STYTCH_PROJECT_ID=project-test-...
STYTCH_SECRET=secret-test-...
MSAL (Azure AD)
AZURE_CLIENT_ID=your-client-id
AZURE_CLIENT_SECRET=your-client-secret
AZURE_TENANT_ID=your-tenant-id
Multiple Providers
You can configure multiple providers and the library will automatically route tokens to the correct provider:
let config = default;
let mut providers: = vec!;
// Add Clerk
if let Ok = new.await
// Add Stytch
if let Ok = new.await
// Add MSAL
if let Ok = new.await
let auth_layer = new;
Without Axum (Manual Usage)
You can also use the providers directly without the middleware:
use *;
async
User Context
The UserContext struct contains all the authenticated user information:
Architecture
┌─────────────────────────────────────────┐
│ Your Axum App │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ AuthLayer (Middleware) │
│ • Extracts Authorization header │
│ • Routes to correct provider │
│ • Injects UserContext into request │
└─────────────────────────────────────────┘
│
┌─────────┴──────────┐
▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐
│ Clerk │ │Stytch │ │ MSAL │
│Provider│ │Provider│ │Provider│
└────────┘ └────────┘ └────────┘
Provider-Specific Authorization
libauth-rs supports provider-specific authorization, allowing each provider to implement its own authorization logic:
use AuthContext;
async
See AUTHORIZATION.md for complete documentation on authorization features.
Per-Provider Routers
You can create separate Axum routers for each authentication provider:
// Clerk-specific routes
let clerk_router = new
.route
.layer;
// MSAL-specific routes
let msal_router = new
.route
.layer;
// Combine routers
let app = new
.nest
.nest;
API-Agnostic Design
This library is designed to be API-agnostic - it doesn't care whether you're building a REST API, GraphQL API, or any other type of service. The middleware simply:
- Extracts authentication tokens from HTTP headers
- Validates them with the appropriate provider based on JWT issuer
- Injects the user context and provider reference into the request
You can then use the AuthExtension, AuthContext, or OptionalAuth extractors in any handler, regardless of what kind of API you're building.
License
MIT
Contributing
Contributions welcome! Please open an issue or PR.