tetthys-auth
Framework-agnostic authentication/authorization core for Rust applications.
Core Concepts
tetthys-auth separates authentication into clear responsibilities:
| Responsibility | Trait |
|---|---|
| User identity | Authenticatable |
| Roles / permissions | Authorizable |
| Load current user | AuthProvider |
| Mutate login state | AuthSession |
| Request cache & pipeline | AuthContext |
Defining a User
Your user type must implement Authenticatable.
To use authorization helpers, also implement Authorizable.
Authenticatable
use Authenticatable;
Authorizable
use Authorizable;
AuthProvider
An AuthProvider is responsible for resolving the current user.
use ;
Providers may:
- Return
Ok(None)for unauthenticated requests - Return
Err(AuthError::ProviderFailed(_))on failure
AuthContext (Request Scope)
AuthContext represents one request.
It:
- Owns the provider
- Caches the resolved user
- Is invalidated on sign-in / sign-out
You must expose it via AuthContextAccessor.
Minimal test / thread-local example
use Cell;
use ;
thread_local!
Authentication Helpers
All helpers return Result<_, AuthError>.
Auth state
? // bool
? // Option<User>
? // User or Unauthenticated
User ID
? // Option<User::Id>
? // User::Id or Unauthenticated
Authorization Helpers
Permissions
?
?
If the user has the admin role, all permissions are allowed by default.
Roles
?
?
Provider Chaining
Multiple providers can be evaluated in order.
use ChainProvider;
let chain = new;
set_ctx;
Behavior:
- Providers are queried in order
- The first
Some(user)wins - Results are cached per request
Request-Level Caching
Within a single request:
?;
?;
?;
The provider is called only once.
AuthSession (Sign-in / Sign-out)
AuthSession mutates the authentication state.
use ;
;
You must expose it via AuthSessionAccessor.
use RefCell;
use AuthSessionAccessor;
thread_local!
Sign In / Sign Out
?;
?;
Notes:
- Missing session →
AuthError::MissingSession - Automatically invalidates
AuthContextcache
Errors
MissingContext
MissingSession
Unauthenticated
ProviderFailed
Quick Start Example
let user = User ;
set_ctx;
assert!;
assert!;
Design Summary
- No global singletons
- Explicit request boundaries
- Provider-based authentication
- Clear separation of concerns
- Highly testable