modo-auth-0.1.0 has been yanked.
modo-auth
Session-based authentication and Argon2id password hashing for modo applications.
Features
| Feature | What it enables |
|---|---|
templates |
UserContextLayer — injects the authenticated user into the minijinja template context |
Key Types
| Type | Purpose |
|---|---|
UserProvider |
Trait — implement on your user repository to load users by session ID |
UserProviderService<U> |
Type-erased wrapper around a UserProvider; register with app.service() |
Auth<U> |
Extractor — requires an authenticated user; returns 401 if absent |
OptionalAuth<U> |
Extractor — resolves user if present, yields None if not authenticated |
PasswordHasher |
Argon2id hashing service with hash_password / verify_password |
PasswordConfig |
Argon2id tuning knobs (memory, iterations, parallelism) |
UserContextLayer |
Tower layer (feature templates) — injects user into template context |
Usage
1. Implement UserProvider
use ;
2. Register services in main
use ;
async
3. Use extractors in handlers
use ;
// Requires authentication — returns 401 if no session / user not found
async
// Optional — never rejects, yields None when not authenticated
async
4. Hash and verify passwords
use ;
use Service;
// Use default OWASP-recommended settings
let hasher = default;
let hash = hasher.hash_password.await?;
let valid = hasher.verify_password.await?;
// Extract in a handler
async
5. Custom Argon2id parameters
use ;
let config = PasswordConfig ;
let hasher = new?;
PasswordConfig implements serde::Deserialize with #[serde(default)], so you can load it from YAML with partial overrides:
password:
memory_cost_kib: 32768
# time_cost and parallelism fall back to defaults (2 and 1)
6. Inject user into template context (feature templates)
use ;
// In main — add after the session layer
app.service
.layer
.layer
.run
.await
The layer inserts the authenticated user as "user" into the minijinja TemplateContext, available in every template without explicit handler code. If no session exists or the user is not found, nothing is injected.
Error Behaviour
| Condition | Auth<U> |
OptionalAuth<U> |
|---|---|---|
| No session | 401 | None |
| Session present, user not found | 401 | None |
Provider returns Err |
500 | 500 |
| Session middleware not registered | 500 | 500 |
UserProviderService<U> not registered |
500 | 500 |