02 Flows
Contract
A flow step is a function that decides:
step(context, reader, draws, input) -> decision | refusal
context = time, realm, tenant, policy, settings, caller's address
decision = output, writes
refusal = code
- A step reads through
readeronly, and gets random bytes fromdrawsonly. With the same answers from both it gives the same decision, in every language. - A step MUST NOT read a clock, a random source, the network or the environment, and MUST NOT write.
- A decision has no effect until its writes are committed. The output MUST NOT be used before that.
- A refusal changes nothing. A step that must change state and answer with an error (a poll that is too early, a reused token) returns a decision whose output is the error.
Writes and preconditions
Between reads and commit, another request can change the state. Each write therefore has its precondition:
| Write | Precondition |
|---|---|
insert(row) | No row with this key or unique value |
replace(row, version) | The stored row has this version |
delete(key, version) | The stored row has this version |
require(key, version) | The stored row has this version (no change) |
delete_where(column, value) | None |
Writes are plain data. An application can add them to its own transaction. Each row has a version that grows with every change. Writes apply in order.
commit(writes, app_writes) runs in one transaction. If a precondition fails, nothing is stored and the result is
conflict. A caller MAY run the flow again, at most three times. It MUST use new draws.
Security decisions MUST depend only on loaded state that a precondition protects, or on state that cannot become less permissive in a harmful way. Each flow lists its protected reads.
Single use
A challenge is used by inserting its hash. An invite or a device request is deleted, and a code or a refresh token is marked as used, with its version. This is in the same commit as the result. Of two requests with one token, exactly one succeeds.
Effects
An effect is work outside the database: send mail, call back a client. Effects are stored in an outbox in the same commit, and run at least once after it. An effect has an ID, so that a receiver can ignore a repeat. Each effect has a lease (10), so that two processes do not run it at the same time. The secret part of a mail is in the outbox only until it is sent.
Audit
Every decision that changes an account gives an event: time, tenant, subject, action, target, the client address and the device of the caller if known, and method. An application can keep addresses out of events. Events have no secrets, no names and no free text from a client. They are writes, so a change without its event cannot exist.
Ceremonies
A ceremony spans two requests. Its state is in a MAC-protected token that the client returns, with the challenge, kind, tenant and expiry. The challenge is stored as used at the end (single use), so a ceremony works once.