Expand description
Authentication, browser sessions, magic links, personal access tokens, and HMAC signing for Slabs.
§What this crate owns
- Types:
Auth,Session,Principal,MagicLinkIssued,MagicLinkConsumed,AccessTokenIssued,SESSION_COOKIE_NAME("slabs_session"), andHOST_SESSION_COOKIE_NAME("__Host-slabs_session"). - Framework SQLite tables created by
slabs_db::Db::init_system_tables:slabs_sessions: SHA-256 hashed browser session tokens,user_id,role, andexpires_at.slabs_magic_links: domain-separated HMAC-SHA256 hashed 6-character verification codes bound to normalizedemailandpurpose, plusexpires_at.slabs_access_tokens: SHA-256 hashedAuthorization: Bearertokens,user_id,role,permission,last_used_at, andexpires_at.
- User accounts live in your application’s own
userstable in your SQL migrations. This crate does not own auserstable.
§Agent invariants
- Put an explicit auth guard on line 1 of every protected slab handler. Do not rely on hidden middleware or callbacks.
Auth::require_session: requires a valid session cookie (HTTP 401 when missing or expired).Auth::require_admin: requires a valid session withrole == "admin"(HTTP 401 or 403).Auth::require_auth: accepts eitherAuthorization: Bearer <token>or a session cookie (HTTP 401).Auth::require_write_auth: requiresAuth::require_authwithpermission == "write"(HTTP 403 for"read"tokens).
- Hash passwords asynchronously with
Auth::hash_password(Argon2idoffloaded tospawn_blockingwith a concurrency semaphore and 1024-byte cap) and verify withAuth::verify_passwordorAuth::verify_password_constant_time. - Magic links generate 6-character alphanumeric codes without ambiguous characters (
0,O,1,I), hashed with a keyed domain-separated HMAC bound to(email, purpose).Auth::consume_magic_linkrequires matchingemail,purpose, and code, locks out after 5 failed attempts in 15 minutes, and runsDELETE ... RETURNINGso every code is single-use. - Personal access tokens start with
slb_and are stored only as SHA-256 hex digests. ReturnAccessTokenIssued::tokenonce at issuance because plaintext tokens cannot be recovered later. - If an
Authorization: Bearerheader is present,Auth::optional_authchecks only the bearer token and does not fall back to a session cookie on failure. - Use
Auth::create_session_in_txwhen creating a user and a browser session inside one SQLite transaction, andAuth::rotate_sessionon login or privilege escalation.
§Example
use axum_extra::extract::CookieJar;
use http::HeaderMap;
use slabs_auth::Auth;
use slabs_core::SlabResult;
async fn protected_handler(auth: &Auth, jar: &CookieJar, headers: &HeaderMap) -> SlabResult<()> {
// Line 1 guard for browser-only endpoints:
let session = auth.require_session(jar).await?;
// Or accept either a session cookie or a write-scoped Bearer token:
let principal = auth.require_write_auth(jar, headers).await?;
let _ = (session.user_id, principal.user_id);
Ok(())
}Structs§
- Access
Token Issued - Result returned when a new personal access token is issued.
- Auth
- Explicit authentication and session service backed by
slabs_sessions,slabs_magic_links, andslabs_access_tokens. - Magic
Link Consumed - Verified metadata returned after atomically consuming a magic link code.
- Magic
Link Issued - Result returned when a new one-time magic link code is issued.
- Principal
- Authenticated actor resolved from either a browser session cookie or an
Authorization: Bearertoken. - Session
- Active browser session persisted in the
slabs_sessionstable.
Constants§
- HOST_
SESSION_ COOKIE_ NAME - Host-prefixed cookie name used when
cookie_secureistrue(__Host-prefix prevents subdomain injection). - SESSION_
COOKIE_ NAME - Cookie name used to store browser session tokens when
cookie_secureisfalse.