Auth Framework
A comprehensive authentication and authorization framework for Rust applications.
Features
- Multiple Authentication Methods: OAuth, JWT, API keys, password-based authentication
- Token Management: Issuance, validation, refresh, and revocation
- Permission System: Role-based access control with fine-grained permissions
- Multi-Factor Authentication: Support for TOTP, SMS, email, and hardware keys
- Session Management: Secure session handling with expiration
- Rate Limiting: Built-in protection against brute force attacks
- Audit Logging: Comprehensive logging of authentication events
- Storage Backends: In-memory, Redis, PostgreSQL, MySQL support
- Middleware: Easy integration with web frameworks
- Distributed: Cross-node authentication validation
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Basic Usage
use ;
use JwtMethod;
use MemoryStorage;
use Duration;
use Arc;
async
OAuth Authentication
use OAuth2Method;
use OAuthProvider;
// Set up OAuth with GitHub
let oauth_method = new
.provider
.client_id
.client_secret
.redirect_uri;
auth.register_method;
// Generate authorization URL
let = oauth_method.authorization_url?;
println!;
// After user authorizes, exchange code for token
let credential = oauth_code;
let result = auth.authenticate.await?;
match result
API Key Authentication
use ApiKeyMethod;
// Set up API key authentication
let api_key_method = new
.key_prefix
.header_name;
auth.register_method;
// Create an API key for a user
let api_key = auth.create_api_key.await?;
println!;
// Authenticate with API key
let credential = api_key;
let result = auth.authenticate.await?;
Multi-Factor Authentication
// Enable MFA in configuration
let config = new
.enable_multi_factor;
// Authentication with MFA
let credential = password;
let result = auth.authenticate.await?;
match result
Permission Management
use ;
// Permission checking is built into the AuthFramework
// Create a test token first
let token = auth.create_auth_token.await?;
// Check permissions
let can_read = auth.check_permission.await?;
let can_write = auth.check_permission.await?;
let can_delete = auth.check_permission.await?;
println!;
Storage Configuration
Redis Storage
use ;
let config = new
.storage;
Custom Storage
use AuthStorage;
use AuthToken;
;
// Use your custom storage
let storage = new;
let auth = new;
Rate Limiting
use RateLimitConfig;
let config = new
.rate_limiting;
Middleware Integration
Axum Integration
use ;
async
Actix Web Integration
use ;
use BearerAuth;
async
Configuration
Full Configuration Example
use *;
let config = new
.token_lifetime
.refresh_token_lifetime
.enable_multi_factor
.storage
.rate_limiting
.security
.audit;
Security Considerations
- Secret Management: Never hardcode secrets. Use environment variables or secure vaults.
- Token Storage: Use secure storage backends in production (Redis, PostgreSQL).
- HTTPS: Always use HTTPS in production to protect tokens in transit.
- Rate Limiting: Enable rate limiting to prevent brute force attacks.
- Token Expiration: Set appropriate token lifetimes based on your security requirements.
- Audit Logging: Enable comprehensive audit logging for security monitoring.
Examples
See the examples/ directory for complete examples:
basic.rs- Basic authentication setup (working)oauth.rs- OAuth integration (working)
Additional examples (currently being updated):
api_keys.rs- API key managementmfa.rs- Multi-factor authenticationpermissions.rs- Advanced permission managementmiddleware.rs- Web framework integrationbenchmarks.rs- Performance benchmarkssecurity_audit.rs- Security features demonstration
Note: Some examples are currently being updated to match the latest API.
The basic.rs and oauth.rs examples are fully functional.
Contributing
Contributions are welcome! Please read our Contributing Guide for details on our development process, coding standards, and how to submit pull requests.
Security
Security is our top priority. Please review our Security Policy for:
- Reporting security vulnerabilities
- Security best practices
- Supported versions
- Compliance information
For security issues, please email security@example.com instead of using the issue tracker.
License
This project is licensed under the MIT OR Apache-2.0 license.