2.08 Authentication, Authorization and Privilege Separation: Exit Evidence
Evidence · Step: 2.08 Authentication, Authorization and Privilege Separation
| Step | 2.08 Authentication, Authorization and Privilege Separation |
| Date | 2026-11-15 |
| Database | PostgreSQL 18.6 (postgres:18-alpine) in Docker Desktop on Windows 11; postgres:18-alpine on GitHub’s ubuntu-latest in CI |
| Decision | ADR-0017 |
| Result | Passed |
Deliverables
Section titled “Deliverables”| Deliverable | Where |
|---|---|
| Authn/authz module | crates/api/src/auth/: oauth.rs (the token endpoint), mod.rs (token lookup), scope.rs (Auth<Scope>), permissions.rs (the matrix), rate.rs (rate limits); ownership in crates/api/src/routes/common.rs |
| The permission matrix | crates/api/permissions.toml: six roles, their scopes and templates, and the roles one credential may not hold together |
| Clients and credentials | Migration 0009 (clients, client_credentials, access_tokens, MZ010); crates/engine/src/clients.rs; the mezgeb_admin role (db/init/03-admin.sql); mezgeb-cli client and mezgeb-cli credential |
| The authorization test suite | crates/api/tests/authz.rs, crates/api/tests/threats.rs, crates/api/tests/contract/oauth.rs, crates/engine/tests/clients.rs |
| Documentation | docs/api/README.md, Authentication and authorization; the clients and credentials runbook; db-schema.md, Clients and credentials; the threat model and the OWASP review |
The work arrived in fourteen pull requests: ADR-0017 (#84), the storage
(#85), the CLI (#87), the scope before the request (#88), OAuth (#89),
ownership (#90), rate limits (#91), the role × operation test (#92), the
Postman collection (#93), the documentation (#94), the threat tests (#95),
test stability (#96) and this evidence (#97). A flaky fault test that #85’s
CI showed was fixed separately (#86). Before the step began, three pure-move PRs gave crates/api a
layout for it: one file per contract tag (#81, #82, #83).
Decisions
Section titled “Decisions”Three were made with the maintainer before any code:
- OAuth 2.0 client credentials for opaque tokens, not signed JWTs or API keys. Revocation is instant and no signing key is needed before step 3.12; it costs one indexed lookup per request.
- A client touches only its own accounts, plus system accounts where a template allows them. Cross-client payments are not part of v1.
- Closed periods are never reopened, so the roadmap’s “maker-checker for manual period reopen” is met by there being no reopen at all.
Implementation tasks
Section titled “Implementation tasks”| Task | Done |
|---|---|
clients and client_credentials tables, credentials hashed (Argon2) |
Migration 0009. Secrets are Argon2id PHC strings (19 MiB, t=2, p=1), checked off the async runtime; an unknown credential is checked against a dummy hash so it is refused as slowly as a wrong secret. Tokens are 256 random bits, stored as SHA-256, 15 minutes by default. Only mezgeb_admin creates or revokes; the service cannot mint a credential |
| A permission matrix as data: role × operation × template | permissions.toml, built into the service and checked at start-up against the engine’s roles, the contract’s scopes, the released templates and the exclusive role pairs. Operations come from the contract (each needs one scope); templates are listed per role, and an unlisted template is denied |
| An authorization middleware with default deny | Auth<Scope> is every handler’s first extractor: 401 without a valid token, 403 without the scope, before the query, body or idempotency key is read. A template is checked right after. No handler lacks one: the matrix test calls every operation without a token |
| Maker-checker for adjustments and manual period reopen | Adjustments: one credential is never both operator and approver (matrix, engine, database), and the engine refuses the same person on a second credential (maker_is_checker, INV-25). Period reopen: none exists (ADR-0017) |
| Per-client rate limits | A token bucket per client (rate_per_second, burst), 429 rate_limited with Retry-After; per credential at the token endpoint, before any hashing (429 slow_down) |
And, from ADR-0017: ownership. A client reads and binds only its own accounts (and system accounts in postings), reads entries it posted or that touch its accounts, and holds on its accounts; anything else is answered as if it did not exist.
Testing requirements
Section titled “Testing requirements”| Requirement | Test | Result |
|---|---|---|
| Every endpoint × every role matches the permission matrix; anything not explicitly allowed is denied | every_role_gets_exactly_what_the_matrix_allows (tests/authz.rs): the 42 bearer operations of the contract × the 6 roles, generated from the contract and the matrix. Allowed exactly when the matrix grants one of the operation’s scopes and, for a template, lists it; 403 forbidden otherwise; 401 for all 42 without a token. Every response validated against the contract |
Passes: 252 role cases and 42 anonymous ones |
| Client A cannot read or post against client B’s accounts | a_client_cannot_touch_another_clients_accounts: B gets 404 for A’s account, balance, history, entry and hold, nothing when it finds A’s wallet by reference, 422 bound_account_not_found when it binds A’s wallet (by ID or reference, as payer or payee), and cannot capture, release or place a hold on A’s money; A’s balance is unchanged |
Passes; with the ownership code removed it fails |
| Replaying a revoked credential fails | revoked_credentials_and_disabled_clients_stop_at_once: the revoked credential’s token is refused on every replay and it gets no new one; disabling a client stops all its credentials; other clients are unaffected. In the database: credentials_log_in_and_tokens_end_with_them |
Passes |
| An approver cannot approve their own request | The contract test: the maker’s credential gets 403 forbidden (no approver role); the same person on a second, approver credential gets 403 maker_is_checker. In the engine: adjustments_need_a_second_person_and_post_once |
Passes |
The matrix test found a defect on its first run: a missing or malformed
query string (the reader on getAccountLedger without from and to) was
answered by axum in plain text, not Problem Details. A QueryParams
extractor now answers 400 invalid_request for every one (#92).
Acceptance criteria
Section titled “Acceptance criteria”The auto-generated role × endpoint test matrix passes, and no endpoint
lacks an authorization test. The matrix test reads its operations from
the contract and asserts there are 42 with a bearer token, so a new one is
tested, or the count fails, without editing the test. The token endpoint,
the 43rd, authenticates a credential instead and is tested in
contract/oauth.rs: a real exchange, a narrowed scope, a wrong secret and
an unknown credential answered alike, unsupported_grant_type,
invalid_scope, and 429 slow_down under guessing.
Exit gate
Section titled “Exit gate”The threat model’s “compromised client” and “insider” threats are each mapped to a passing test. A compromised client holds a real client credential; an insider holds an operator credential or database access.
| Threat | Tests | Notes |
|---|---|---|
| Compromised client server | ||
| TM-07 Posts entries that create money | tm07_a_client_cannot_create_money (no raw lines, fee revenue refused as a payer, no system account of its own); every_role_gets_exactly_what_the_matrix_allows; an_unlisted_template_is_denied; template role rules: every_refusal_has_its_stable_code (crates/ops) |
|
| TM-08 Connects to the database directly | the_app_role_cannot_change_account_identity, totals_only_increase, journal_history_cannot_change (crates/engine/tests/schema.rs); the service role cannot create a credential, revoke one or raise a rate limit (credentials_log_in_and_tokens_end_with_them) |
Network isolation is the operator’s (step 3.11) |
| TM-09 Reports a deposit that never arrived | each_step_refuses_what_breaks_the_policy (crates/ops, the clearing policy of step 2.02) |
Daily reconciliation against the bank statement is step 2.15 |
| TM-10 Moves money among its own users | clients_and_logins_are_rate_limited; a_client_cannot_touch_another_clients_accounts (it reaches only its own users) |
Accepted risk: detection is step 2.15 |
| TM-01, TM-05 (stolen credentials, guessed IDs) | revoked_credentials_and_disabled_clients_stop_at_once, clients_and_logins_are_rate_limited, a_client_cannot_touch_another_clients_accounts |
Listed under the external attacker; the same controls |
| Malicious insider | ||
| TM-11 Edits or deletes journal rows | journal_history_cannot_change; verification finds a changed or deleted line (a_tampered_line_amount_is_found, a_deleted_line_is_found) |
The hash chain is step 2.09 |
| TM-12 Posts an adjustment crediting their own account | The contract test (forbidden, then maker_is_checker); adjustments_need_a_second_person_and_post_once; the database refuses operator + approver (credentials_log_in_and_tokens_end_with_them); the_built_in_matrix_is_valid |
|
| TM-13 Reopens a closed period | tm13_a_closed_period_cannot_be_reopened (no such operation in the contract or the service); posting_into_a_closed_period_is_refused (the database refuses it) |
|
| TM-14 Copies a signing key | None yet: there are no signing keys until receipts (2.09) and offline vouchers (2.11); keys in a key service is step 3.12 | Not applicable in this step |
| TM-15 Disables verification | runs_are_recorded_and_the_verifier_is_read_only; verification in CI |
The missing-run alert is step 3.09 |
| TM-16 Exports data in bulk | the_built_in_matrix_is_valid (only the operator and the auditor read across clients); a_client_cannot_touch_another_clients_accounts (a client lists only its own) |
Logging reads is step 2.09 |
The whole suite. On the final branch, cargo test --workspace against
PostgreSQL ran 41 test binaries and 284 tests, with 0 failures; CI ran the same
on every PR, plus the container and the Postman smoke collection, which now
starts by exchanging the development credentials for tokens. A short
two-cycle run of the 2.07 chaos test through the API, now logging in with
OAuth, stayed clean: 482 requests, 0 violations.
The first local run of the whole suite failed in two tests, from load rather than the code under test: an unoptimised Argon2 in debug builds starved the timeout test, and the rate-limit test assumed requests faster than its refill. Both were fixed, and test databases now carry the time in their names so leftovers cannot collide (#96).
Not done in this step
Section titled “Not done in this step”- Audit events for token issuance, refusals and reads across clients: step 2.09.
- Rate limits shared between several instances: step 3.03.
- TLS in the service itself; it runs behind a TLS-terminating proxy.
- The access log records the client, not yet the credential of each request.