Skip to main content

Crate slabs_auth

Crate slabs_auth 

Source
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"), and HOST_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, and expires_at.
    • slabs_magic_links: domain-separated HMAC-SHA256 hashed 6-character verification codes bound to normalized email and purpose, plus expires_at.
    • slabs_access_tokens: SHA-256 hashed Authorization: Bearer tokens, user_id, role, permission, last_used_at, and expires_at.
  • User accounts live in your application’s own users table in your SQL migrations. This crate does not own a users table.

§Agent invariants

  • Put an explicit auth guard on line 1 of every protected slab handler. Do not rely on hidden middleware or callbacks.
  • Hash passwords asynchronously with Auth::hash_password (Argon2id offloaded to spawn_blocking with a concurrency semaphore and 1024-byte cap) and verify with Auth::verify_password or Auth::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_link requires matching email, purpose, and code, locks out after 5 failed attempts in 15 minutes, and runs DELETE ... RETURNING so every code is single-use.
  • Personal access tokens start with slb_ and are stored only as SHA-256 hex digests. Return AccessTokenIssued::token once at issuance because plaintext tokens cannot be recovered later.
  • If an Authorization: Bearer header is present, Auth::optional_auth checks only the bearer token and does not fall back to a session cookie on failure.
  • Use Auth::create_session_in_tx when creating a user and a browser session inside one SQLite transaction, and Auth::rotate_session on 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§

AccessTokenIssued
Result returned when a new personal access token is issued.
Auth
Explicit authentication and session service backed by slabs_sessions, slabs_magic_links, and slabs_access_tokens.
MagicLinkConsumed
Verified metadata returned after atomically consuming a magic link code.
MagicLinkIssued
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: Bearer token.
Session
Active browser session persisted in the slabs_sessions table.

Constants§

HOST_SESSION_COOKIE_NAME
Host-prefixed cookie name used when cookie_secure is true (__Host- prefix prevents subdomain injection).
SESSION_COOKIE_NAME
Cookie name used to store browser session tokens when cookie_secure is false.