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

10 Storage

Schema

schema/ has the reference schema for SQLite and PostgreSQL: the core (sqlite.sql, postgres.sql) and each feature (sqlite.tenants.sql, …). An implementation in any language that uses these tables can run on the same data. Column meanings are part of this specification. Indexes are not.

Secrets in rows are hashes, public keys, verifiers or ciphertexts. No row alone lets its reader act as a user, except an unsent mail in the outbox.

Table prefix

Auth tables live in the application’s database. Their prefix is configurable.

  • Default auth_. Pattern ^([a-z][a-z0-9_]{0,14}_)?$: empty, or at most 16 characters that end with _. A start with sqlite_ or pg_ is refused. A prefix goes into SQL text, so an implementation MUST check the pattern before it builds any SQL.
  • The prefix applies to tables, indexes, constraints, sequences and triggers. Base names have at most 40 characters, so that a full name fits the 63 bytes of PostgreSQL.
  • On PostgreSQL the schema (namespace) is configurable too, with the same pattern without the last _. SQL names it explicitly and does not use search_path.
  • Two installations with different prefixes can share a database. They share nothing else: the prefix is not part of any key derivation, token or trace.
  • Migrations run as an explicit step. Opening a store MUST NOT create tables. With a wrong prefix, opening fails with schema_missing. Otherwise a typing error would start an empty user database, with open bootstrap.
  • rename changes the prefix of an installation in one transaction, while no server runs.

Application tables can refer to <prefix>users.

Transactions

commit(writes, app_writes) (02) runs both in one transaction, at an isolation level where the preconditions hold until commit: SQLite with an immediate transaction; PostgreSQL with row locks for require, replace, delete and consume, and unique keys for insert. Row locks are taken in key order.

app_writes can read and write application tables. If it fails, nothing is stored.

Migrations

The version number of the core is in <prefix>meta. Migrations are append-only and apply in one transaction where the database allows it. A server refuses a version that it does not know. Opening never migrates: the migration step does, from version 4 on.

VersionAdds
6limits, leases, features

Version 5 existed only before a release, and has no migration.

Features

Some tables exist only when the application uses them. The migration step adds the tables of the features that it is given, and records each in <prefix>features with its level. It never removes a feature.

FeatureTablesNeeded for
tenantstenants, membershipsTenants (14)
placesplacesA separate directory (15)

A server that is configured for a feature MUST refuse to start when the database does not have its tables. Columns named tenant in core tables exist in every database: without tenants they hold the root tenant.

Shared state

Any number of server processes can use one store. State that a check needs is in the store, never in the memory of one process:

  • Single-use marks (used): the hash of each DPoP proof’s key and jti, and of each client or device assertion’s subject and jti, until the proof or assertion expires. A second use is refused. A ceremony’s challenge is a mark too.
  • Call limits (limits): one row for each counted call, under the hash of its key (a client address, or a user) and the minute. A refused call writes nothing, so a key has at most its limit of rows. Calls at the same moment can pass the limit by their number. The key is hashed, so the table holds no address.
  • Leases (leases): a piece of background work runs in one process at a time. A lease has an end, so a process that stops does not hold it for ever. Work that takes longer than its lease can run twice.

The client address that limits count is the address of the connection. Behind n proxies it is the n-th entry from the end of X-Forwarded-For, without a port: each proxy adds one, so a client cannot choose it. An entry that is not an address gives no address. The server must be reachable only through these proxies.

Retention

Expired rows (tokens, challenges, invites, single-use marks, limits and leases) are removed in small batches, by background work that runs every few seconds. Audit events and client addresses have separate retention times that the application sets.