Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Start

[dependencies]
auth-chad = { git = "https://github.com/audiochad/auth-chad", rev = "<commit>", features = ["axum", "sqlite"] }
use std::{path::Path, sync::Arc};
use auth_chad::{Config, Prefix, axum::Auth, sqlite::SqliteStore};

fn auth(master: &[u8; 32]) -> Result<Arc<Auth<SqliteStore>>, Box<dyn std::error::Error>> {
    let config = Config::new(
        "myapp/v1",                    // never change it
        "https://app.example.com",     // where the API is
        "app.example.com",             // passkeys belong to this domain: never change it
        "My app",
        &["https://app.example.com"],  // pages that may call the API
    )?;
    let store = SqliteStore::open(Path::new("/var/lib/myapp/app.db"), &Prefix::default(), 4)?;
    Ok(Arc::new(Auth::new(config, master, store)?))
}
  • Master key: 32 random bytes from a secret store. Never in the repository.
  • Tables: run the migration as a deploy step, not at start. auth-chad schema sqlite prints the SQL, and SqliteStore::create(path, prefix, readers, &[]) runs it. open never creates tables. Tenants and places are optional features: add them with &[Feature::Tenants] when you use them, and call auth.check_tables() at start.
  • Routes: Router::new()….with_state(auth.clone()).merge(auth.router()). The API is below /auth.
  • Page: import { signup, login, logout, session, stepUp } from "/auth/auth-chad.js". Call them from a click.
  • Behind a proxy: with_proxies(n) (load-balancer).
  • A web app on another host (for example app.example.com calling api.example.com): list its origin in Config origins (for passkeys) and in with_web_origins (for CORS). Its calls carry no cookies, so it logs in with a key-bound token, for example with the Rust client. A key-bound policy refuses cookies everywhere.
  • No client addresses in audit events: without_addresses(). Rate limits still use them.
  • Housekeeping: call auth_chad::axum::work(&auth) every few seconds: it prunes, sends mails and runs places.

A whole application: rust/examples/minimal.