lmrc-auth
Authentication framework for LMRC Stack applications.
Provides flexible, trait-based authentication with multiple providers, session management, and ready-to-use Axum handlers.
Features
- Flexible Authentication: Trait-based design supports database, LDAP, OAuth, and custom providers
- Session Management: Secure session creation, validation, and destruction
- Password Hashing: Built-in bcrypt support for secure password storage
- Database Integration: SeaORM-based provider for PostgreSQL authentication
- Axum Ready: Pre-built handlers and middleware for Axum web framework
- Type-Safe: Leverages Rust's type system for security
Installation
[]
= "0.3.11"
Feature Flags
bcrypt(default) - Password hashing with bcryptdatabase- Database-backed authentication provider with SeaORM
To enable all features:
[]
= { = "0.3.11", = ["database"] }
Quick Start
1. Database Setup
Create the required tables:
(
id BIGSERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
role VARCHAR(50) NOT NULL DEFAULT 'user',
is_active BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMP NOT NULL DEFAULT NOW
);
(
token VARCHAR(255) PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id),
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW
);
2. Configure Authentication
use ;
use Database;
use Arc;
async
3. Add Authentication Routes
use ;
use ;
let auth_routes = new
.route
.route
.route
.with_state;
4. Protect Routes with Middleware
use middleware;
use auth_middleware;
let protected_routes = new
.route
.route
.layer;
Core Concepts
AuthProvider Trait
The AuthProvider trait defines the authentication interface:
Custom Providers
Implement custom authentication strategies:
use ;
use async_trait;
Data Models
AuthUser
Session
Credentials
Configuration
AuthConfig
Custom Configuration
let config = AuthConfig ;
Examples
Login Handler
use ;
use ;
// POST /auth/login
// Body: {"email": "user@example.com", "password": "secret"}
// Returns: {"token": "...", "user": {...}, "expires_at": "..."}
Logout Handler
// POST /auth/logout
// Cookie: session_token=...
// Returns: 204 No Content
Get Current User
// GET /auth/me
// Cookie: session_token=...
// Returns: {"id": 1, "email": "user@example.com", "role": "admin"}
Testing
Security Considerations
- Password Hashing: Uses bcrypt with appropriate cost factor
- Session Tokens: Generated using UUID v4 (cryptographically random)
- Session Expiration: Configurable expiration with automatic cleanup
- Secure Cookies: HTTPS-only cookies by default
- SQL Injection: Uses parameterized queries via SeaORM
Integration with lmrc-http-common
Works seamlessly with lmrc-http-common for error handling, middleware, and HTTP utilities.
Contributing
This library is part of the LMRC Stack monorepo.
License
Dual licensed under MIT OR Apache-2.0 (your choice).