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

Data

Summary: the tables that auth-chad keeps, the types that carry a request through the code, and how a row becomes a write.
Key terms: row, table, version, Stored, Write, Decision, Authenticated, Ctx, hash at rest, prefix, feature.
Related: Flows · Interfaces · spec 10

auth-chad stores its data in ordinary tables in your database, prefixed with auth_. Each row has a version that increases with every change, and every write names the version it expects. Tokens, invites and codes are stored as hashes, and keys are stored as public keys. A copy of the database is therefore not enough to log in.

The picture as text
erDiagram
    users ||--o{ passkeys : has
    users ||--o{ sessions : has
    users ||--o{ devices : approved
    users ||--o| recovery_keys : has
    users ||--o{ envelopes : "e2ee only"
    sessions ||--o{ tokens : issues
    devices ||--o{ tokens : issues
    tenants ||--o{ memberships : has
    users ||--o{ memberships : has
    users ||--o{ events : "audit"

The tables

People and their credentials

TableOne row isNotes
usersA userID (16 bytes), optional handle or address, disabled, recovery hold. Names and profiles belong in your tables
passkeysA passkeyPublic key (COSE), sign counter, last use
recovery_keysA recovery keyOnly the public verifier derived from the key
identitiesAn account at another providerProof of identity; it cannot log in
envelopesAn encrypted copy of the e2ee secretOne per passkey (PRF) and one for the recovery key

Lasting logins

TableOne row isNotes
sessionsA loginTenant, kind (browser, cookie, native), key thumbprint, last passkey use, expiry
tokensAn access, cookie, refresh or connected-app tokenStored as a hash. A used refresh token stays, so reuse is detected
devicesAn approved CLI, app or machinePublic key, kind (user or machine), scope, your data
device_requestsA device waiting for approvalUser code (hash), address, state
api_keysAn API keyHash, scopes, expiry (at most 90 days)

Other companies’ apps

TableOne row isNotes
clientsA connected appRedirect URIs, keys
grantsA user’s consent to an appScope, audience, the key it is bound to
codesAn authorization code, a pushed request, or a code to enter a tenantHash, single use

Account recovery

TableOne row isNotes
recoveriesA pending recovery (email or owners)Hash, ready time, expiry
invitesAn inviteHash, optional identifier, your data

Bookkeeping

TableOne row isNotes
eventsAn audit eventTime, user, device, action, target, address. No secrets or names
outboxWork after a commit, such as a mailRetried until it succeeds, at most five times
usedA single-use markThe hash of a used challenge or proof
limitsOne counted call for a rate limitExpires after one minute
leasesThe process that currently owns a piece of workPrevents two processes from sending the same mail
featuresAn optional group of tables that this database hastenants, places

Optional features

TableFeatureOne row is
tenantstenantsAn organization or customer, with its tag for a host or path
membershipstenantsA user who may enter a tenant, plus your own columns
placesplacesThe location of a tenant’s or user’s data

Run auth-chad schema sqlite --with tenants to print the SQL.

The types that carry a request

TypeHoldsCreated by
CtxThe current time, the tenant, the config and keys, the caller’s address and deviceThe adapter, once per request
Stored<T>A row and its versionThe store, on a read
WriteA change with its conditionA flow, or your code
Decision<T>out and writesA flow
AuthenticatedWho, tenant, level, binding, session, device, last passkey use, scopesThe library only
RefusedA refusal code, sent as {"error": "<code>"}Extractors and routes
use auth_chad::core::rows::Session;
use auth_chad::core::row::Field;
use auth_chad::core::StoreError;
use auth_chad::{Reader, UserId};

async fn count_sessions<R: Reader>(db: &R, user: UserId) -> Result<usize, StoreError> {
    let rows = db.find_by::<Session>("user", user.store()).await?;
    Ok(rows.len()) // each item is a Stored<Session>: the row and its version
}

From a row to a write

A read returns Stored<T>. Each write that depends on that read names its version.

WriteConditionUse
Write::insert(row)No row with this key or unique value existsNew rows
stored.replace(row)The row is still at this versionChanges
stored.delete()The row is still at this versionRemovals
stored.require()The row is still at this version; nothing changesA decision depends on the row
stored.touch()Like replace with the same rowTwo commits that depend on the row cannot both succeed
Write::delete_where::<T>(column, value)NoneClean-up, such as ending all sessions of a user

Rules for rows

  • Each table is declared once, with the table! macro in rust/formats/core/src/rows.rs. The SQL for SQLite and PostgreSQL is generated from that declaration.
  • User, tenant, session and device IDs are 16 bytes. Your application maps its own IDs to them.
  • Every tenant-scoped read takes the tenant as an argument. There is no form without it.
  • The prefix (auth_ by default) is set when the store opens. The SQL text is built once.
  • Optional tables exist only when the database was migrated with their feature. check_tables refuses to start a server that needs a feature that the database lacks.
  • Your own data about people (name, picture, role names) lives in your tables, with the user ID as a plain value.

Where the details are