authbox
A lightweight, modular authentication framework for Rust built around traits, async support, and pluggable components.
It provides
- Password hashing (Argon2)
- JWT authentication (access + refresh tokens)
- Refresh token rotation
- Refresh token blacklisting / revocation
- Async-ready API (Tokio)
- Pluggable architecture
- Fully testable design
Features
- User registration & login flow
- Secure password hashing using Argon2
- JWT access + refresh token support
- Refresh token rotation
- Refresh token revocation support
- Custom user store support (DB or in-memory)
- Custom token managers
- Custom password hashers
- Fully async (
tokio+async-trait) - Trait-based architecture for flexibility
Installation
Quick Start
1. Import prelude
use *;
2. Create JWT manager
You can use the default DefaultJwtManager
or create your own by implementing the TokenManager trait.
Example using the default implementation:
let tokens = new;
3. Create password hasher
You can use the default DefaultHasher
or create your own by implementing the PasswordHasher trait.
Example using the default implementation:
let hasher = DefaultHasher;
4. Create a token blacklist store
The blacklist store is used for refresh token revocation.
You can use:
- Redis
- PostgreSQL
- SQLite
- In-memory storage
- Any custom backend
by implementing the TokenBlacklistStore trait.
Example in-memory implementation:
use async_trait;
use HashSet;
use ;
;
5. Implement a user type
Your custom user type must implement the AuthUser trait.
Example:
6. Implement a user store
You can use:
- PostgreSQL
- MySQL
- SQLite
- MongoDB
- Redis
- In-memory storage
or any custom backend.
Your store must implement the UserStore<U> trait.
Example:
use HashMap;
7. Create AuthService
let store = new;
let tokens = new;
let hasher = DefaultHasher;
let blacklist = new;
let mut auth = AuthService ;
Register User
let user = auth
.
.await;
println!;
Login User
let login = auth
.
.await?;
if let Some = login
Refresh Tokens
let new_tokens = auth
.refresh_token
.await?;
println!;
Old refresh tokens are automatically blacklisted after rotation.
Token Structure
Architecture
authbox is built using traits for maximum flexibility.
AuthService
Handles authentication business logic:
- register
- login
- refresh token rotation
TokenManager
Handles JWT operations:
- generate
- verify
- refresh
PasswordHasher
Handles password security:
- hash
- verify
UserStore
Handles persistence:
- create user
- find user
TokenBlacklistStore
Handles refresh token revocation:
- blacklist token
- check blacklist status
Recommended Production Stack
Redis is recommended for refresh token blacklisting because it supports automatic TTL expiration and very fast lookups.
Roadmap
Roadmap
- Database adapters (SQLx, Diesel, MongoDB, and any backend storage supported via trait-based architecture)
- Token blacklist / revocation system (pluggable via
TokenBlacklistStore) - RBAC (Role-based access control)
- OAuth2 integration
- Middleware for Axum / Actix
- Redis session support
- Email verification
- Password reset flow
License
Apache-2.0