elif-auth
Authentication and authorization system for the elif.rs LLM-friendly web framework.
Features
- 🔐 JWT Authentication - Complete JWT token management with signing, validation, and refresh
- 📝 Session-Based Auth - Cookie-based sessions with multiple storage backends
- 🔒 Password Security - Argon2 and bcrypt password hashing with strength validation
- 🛡️ CSRF Protection - Session integration with CSRF tokens for enhanced security
- ⚡ Multiple Storage - Memory, database, and Redis session storage (extensible)
- 🔑 Role-Based Access - User roles and permissions with flexible authorization
- 🚀 Production Ready - Configurable security settings for development vs production
Quick Start
Add to your Cargo.toml:
[]
= "0.1.0"
JWT Authentication
use ;
// Configure JWT provider
let config = JwtConfig ;
let jwt_provider = new?;
// Generate tokens for a user
let user = JwtUser ;
let access_token = jwt_provider.generate_token?;
let = jwt_provider.generate_token_pair?;
// Validate tokens
let claims = jwt_provider.validate_token_claims?;
println!;
Session Authentication
use ;
use Duration;
// Create session storage and provider
let storage = new;
let session_provider = with_default_config;
// Create a session for authenticated user
let session_id = session_provider.create_session.await?;
// Validate session
let session_data = session_provider.validate_session.await?;
println!;
// Clean up expired sessions
let cleaned = session_provider.cleanup_expired.await?;
println!;
Password Security
use ;
// Hash password with default settings
let password = "user_password123";
let hash = hash_password?;
// Verify password
let is_valid = verify_password?;
// Use specific hasher
let hasher = production; // High security settings
let hash = hasher.hash_password?;
let is_valid = hasher.verify_password?;
// Validate password strength
validate_password_strength?;
Middleware Integration
use ;
// JWT Middleware
let jwt_middleware = new
.skip_path
.optional; // Don't fail on missing tokens
// Session Middleware
let session_config = production
.cookie_name
.cookie_secure;
let session_middleware = new;
// Extract and validate session from cookie
if let Some = session_middleware.extract_session_id_from_cookie
Configuration
JWT Configuration
use JwtConfig;
let config = JwtConfig ;
Session Configuration
use ;
// Production configuration
let config = production
.cookie_name
.cookie_domain
.cookie_secure
.cookie_same_site
.require_csrf;
// Development configuration
let dev_config = development
.cookie_secure
.require_csrf;
Storage Backends
Memory Storage (Development)
use MemorySessionStorage;
let storage = new;
// ⚠️ Sessions lost on restart - development only
Custom Storage Implementation
Implement the SessionStorage trait for custom backends:
use ;
use async_trait;
Security Features
Password Hashing
- Argon2id - Recommended for new applications (memory-hard)
- bcrypt - Compatible with existing systems
- Configurable costs - Development vs production settings
- Password strength validation - Customizable requirements
Session Security
- Secure session IDs - Cryptographically secure random generation
- Cookie security - HttpOnly, Secure, SameSite attributes
- CSRF protection - Integrated CSRF token management
- Automatic cleanup - Expired session removal
- IP and User-Agent binding - Session hijacking protection
JWT Security
- HMAC signing - HS256, HS384, HS512 algorithms
- Token validation - Expiration, issuer, audience checks
- Refresh tokens - Secure token renewal
- Claims validation - Custom claim verification
Error Handling
use ;
match jwt_provider.generate_token
Feature Flags
[]
= { = "0.1.0", = ["argon2", "bcrypt", "jwt"] }
# Or selectively:
= { = "0.1.0", = ["jwt"], = false }
Available features:
argon2- Argon2 password hashing (enabled by default)bcrypt- bcrypt password hashing (enabled by default)jwt- JWT token support (enabled by default)session- Session-based authentication (always available)
Testing
The crate includes comprehensive tests covering:
- JWT token generation and validation
- Session lifecycle management
- Password hashing and verification
- Middleware functionality
- Security configurations
Run tests with:
Examples
See the examples directory for complete working examples:
- JWT authentication flow
- Session-based login/logout
- Password management
- Middleware integration
- Custom storage backends
Framework Integration
This crate is part of the elif.rs framework ecosystem:
- elif-core - Dependency injection and configuration
- elif-http - HTTP server and routing
- elif-security - Security middleware (CORS, CSRF, rate limiting)
- elif-orm - Database ORM and query builder
- elif-validation - Input validation
Contributing
Contributions are welcome! Please see the contributing guidelines for details.
License
This project is licensed under the MIT OR Apache-2.0 license. See LICENSE files for details.
Changelog
0.1.0 - Initial Release
- JWT authentication provider with token management
- Session-based authentication with multiple storage backends
- Password hashing with Argon2 and bcrypt support
- Authentication middleware for HTTP requests
- Comprehensive security configurations
- Role-based access control foundations
- 51 passing tests with full coverage