oxidite-auth
Authentication and authorization for Oxidite (RBAC, JWT, OAuth2, 2FA, API keys).
Overview
oxidite-auth provides a comprehensive authentication and authorization system for the Oxidite web framework. It includes JWT token management, secure password hashing, role-based access control, API key authentication, and two-factor authentication.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Features
- JWT token management - Secure JSON Web Token generation and verification
- Password hashing - Industry-standard Argon2 password hashing
- Role-Based Access Control (RBAC) - Flexible role and permission system
- API key authentication - Secure API key generation and validation
- Two-Factor Authentication (2FA) - TOTP-based second factor authentication
- OAuth2 integration - Support for popular OAuth2 providers
- Email verification - Token-based email verification system
- Password reset - Secure password reset functionality
- Rate limiting - Account-based rate limiting to prevent abuse
Usage
JWT Authentication
Secure user authentication with JSON Web Tokens:
use ;
// Initialize JWT manager
let jwt_manager = new;
// Create a token for a user
let claims = Claims ;
let token = create_token?;
// Verify and extract claims from a token
let verified_claims = verify_token?;
Password Hashing
Secure password storage using industry-standard Argon2:
use Hasher;
let hasher = new;
// Hash a password
let password_hash = hasher.hash?;
// Verify a password against its hash
let is_valid = hasher.verify?;
if is_valid else
Role-Based Access Control (RBAC)
Manage roles and permissions for fine-grained access control:
use ;
// Create roles with permissions
let admin_role = Role ;
// Check if a user has a specific permission
if admin_role.has_permission
API Key Authentication
Secure API access with API key management:
use ;
let manager = new;
// Generate a new API key for a user
let api_key = manager.generate_key?;
// Validate an API key
let is_valid = manager.validate_key.await?;
if is_valid else
Two-Factor Authentication (2FA)
Enhance security with TOTP-based two-factor authentication:
use ;
// Generate a secret for a user
let secret = generate_secret;
// The user receives this secret and sets up their authenticator app
println!;
// Later, verify a code from the user's authenticator app
let user_code = "123456"; // Code entered by user
let is_valid = verify_code?;
if is_valid else
OAuth2 Integration
Integrate with popular OAuth2 providers:
use ;
let config = OAuth2Config ;
let google_provider = new;
// Generate authorization URL
let auth_url = google_provider.authorize_url;
// After user authorization, exchange code for token
// let token = google_provider.exchange_code("authorization-code").await?;
Authorization Middleware
Protect routes with authentication and authorization checks:
use ;
use AuthMiddleware;
// Use authentication middleware to protect routes
// This would typically be integrated with Oxidite's middleware system
Security Best Practices
- Always use strong, randomly generated secrets for JWT signing
- Implement proper rate limiting to prevent brute force attacks
- Store sensitive data securely and encrypt at rest when possible
- Regularly rotate secrets and API keys
- Use HTTPS in production environments
- Validate and sanitize all user inputs
License
MIT