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, and SESSION_COOKIE_NAME ("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: SHA-256 hashed 6-character verification codes, normalized email, purpose, and 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 with Auth::hash_password (Argon2id with a random OS salt) and verify with Auth::verify_password.
  • Magic links generate 6-character alphanumeric codes without ambiguous characters (0, O, 1, I). Auth::consume_magic_link strips non-alphanumeric characters, uppercases the input, hashes with SHA-256, 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.

§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§

SESSION_COOKIE_NAME
Cookie name used to store browser session tokens.