forge
Explore
Log in

Don't you dare go Hollow

in

Clone
★ 0

0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits

contracts.md241 lines, 13.3 KiBHistoryRawDownload
1# Family contracts, v1
2
3Status: normative. This file is copied byte for byte into `im/docs/contracts.md`, `in/docs/contracts.md` and
4`iz/docs/contracts.md`. A change is made in all three repos in the same sitting, and the version line below is
5bumped. The apps share no code. They share only the HTTP contracts written here.
6
7Contract version: **1**
8
9---
10
11## 1. The family
12
13| Key | App | Role | Default listen |
14|---|---|---|---|
15| `im` | Account | OpenID Connect provider, people directory, service registry. Depends on nothing. | `127.0.0.1:7650` |
16| `in` | Files | Personal drive. Blob store for the other apps. Depends on `im`. | `127.0.0.1:7655` |
17| `iz` | Board | Task board. Depends on `im`, optionally on `in`. | `127.0.0.1:7654` |
18
19Production origins are `https://<key>.<domain>` behind a TLS-terminating reverse proxy (Ferron). Apps bind
20`127.0.0.1` only.
21
22### 1.1 Design rules
23
241. **No app is down because another is down.** A signed-in user keeps working in `in` and `iz` while `im` is
25 unreachable. Only new sign-ins need `im`. Nothing calls `im` on the request path of an ordinary page.
262. **Standards first.** Where a standard exists (OIDC Core, OIDC Discovery, OIDC RP-Initiated Logout, OIDC
27 Back-Channel Logout, OAuth 2.0 client credentials, RFC 7009 revocation, RFC 7662 introspection), it is used
28 as written. Extensions are named and listed here.
293. **Machine calls carry a short-lived JWT** issued by `im` through the client credentials grant, with an
30 audience and scopes. Static shared bearer keys are not used.
314. **Every machine API is versioned** under `/api/v1/`. A consumer accepts unknown JSON fields. A provider keeps
32 v1 working for at least one release after v2 ships.
335. **Errors are JSON with a real HTTP status** (section 7).
34
35---
36
37## 2. Identity (OIDC)
38
39`im` is the issuer. Its issuer URL is its public origin with no trailing slash, for example
40`https://im.example.com`.
41
42### 2.1 Discovery
43
44`GET /.well-known/openid-configuration` returns standard metadata, including:
45
46- `issuer`, `authorization_endpoint` (`/authorize`), `token_endpoint` (`/token`), `userinfo_endpoint`
47 (`/userinfo`), `jwks_uri` (`/jwks.json`), `end_session_endpoint` (`/logout`), `revocation_endpoint`
48 (`/revoke`), `introspection_endpoint` (`/introspect`)
49- `response_types_supported: ["code"]`, `grant_types_supported: ["authorization_code", "refresh_token", "client_credentials"]`
50- `code_challenge_methods_supported: ["S256"]`, `id_token_signing_alg_values_supported: ["EdDSA"]`
51- `token_endpoint_auth_methods_supported: ["client_secret_basic", "client_secret_post"]`
52- `backchannel_logout_supported: true`, `backchannel_logout_session_supported: true`
53- `scopes_supported: ["openid", "profile", "email", "directory.read", "files.service"]`
54
55Clients fetch discovery once at boot and cache it. They may hard-code the paths above as a fallback.
56
57### 2.2 Keys
58
59- Signing algorithm: **EdDSA (Ed25519)**. `jwks.json` publishes every key that is active or retired within
60 the last 7 days, as `{"kty":"OKP","crv":"Ed25519","x":...,"kid":...,"use":"sig","alg":"EdDSA"}`.
61- `im` rotates the signing key every 90 days and on admin demand. A new key is published 24 hours before it
62 signs anything.
63- Clients cache the JWKS for 10 minutes, and refetch at most once a minute when a token names an unknown `kid`.
64
65### 2.3 Sign-in (authorization code + PKCE)
66
671. App redirects the browser to `/authorize` with `response_type=code`, `client_id`, `redirect_uri` (exact match
68 with a registered URI), `scope` containing `openid` (and `profile email`), `state` (≥128 bits),
69 `nonce` (≥128 bits), `code_challenge`, `code_challenge_method=S256`. PKCE is mandatory.
702. `im` signs the user in (password, then TOTP or a recovery code if enrolled) and redirects back with
71 `code` and `state`. Codes live 60 seconds and are single use. A reused code revokes every token issued from it.
723. App posts to `/token` (`grant_type=authorization_code`, `code`, `redirect_uri`, `code_verifier`), client auth
73 by `client_secret_basic` or `client_secret_post`. Response: `access_token` (JWT, 10 min), `id_token`,
74 `refresh_token`, `token_type: "Bearer"`, `expires_in`.
754. App validates the `id_token`: signature against JWKS, `iss` exact, `aud` equals its `client_id`, `exp` in the
76 future, `iat` not more than 5 minutes in the future, `nonce` equal to the one it sent.
77
78**ID token claims:** `iss`, `sub` (the user's ULID, stable forever), `aud`, `exp`, `iat`, `auth_time`, `nonce`,
79`sid` (the `im` browser session id), `email`, `email_verified` (always true: `im` only stores confirmed
80addresses), `name`, and the `im` extensions `admin` (bool), `locale` (`en`|`tr`), `zoneinfo` (IANA name, for
81example `Europe/Istanbul`), `picture_version` (integer, bumps when the photo changes).
82
83### 2.4 App sessions
84
85After step 4 the app creates **its own local session** (its own table, its own `__Host-` cookie, its own CSRF
86token), keyed by `sub`, and stores `sid` beside it. The app does not call `im` again for this session.
87
88- Local session lifetime: at most 12 hours of absolute age. After that the app sends the browser through
89 `/authorize` again. While the `im` session is alive this is an invisible redirect.
90- The app may keep the `refresh_token` only if it needs to call `/userinfo` later. Apps in this family do not.
91 They discard it.
92
93### 2.5 Sign-out and revocation
94
95- **App-initiated sign-out:** the app deletes its local session, then redirects to
96 `/logout?id_token_hint=...&post_logout_redirect_uri=<registered>&state=...` (OIDC RP-Initiated Logout). `im`
97 ends the `im` session and every app session tied to it (back-channel, below).
98- **Back-Channel Logout** (OIDC Back-Channel Logout 1.0): each client registers a `backchannel_logout_uri`
99 (`<origin>/auth/backchannel-logout`). When an `im` session ends (sign-out, admin revoke, password change or
100 reset, user disabled or deleted, session expired by policy), `im` POSTs `logout_token=<JWT>` (form-encoded)
101 to every client that was used in that session. The token carries `iss`, `aud`, `iat`, `exp` (2 min), `jti`,
102 `events: {"http://schemas.openid.net/event/backchannel-logout": {}}`, and `sid` (and `sub`). For "disable" and
103 "delete" it carries only `sub`, which means "every session of this user".
104- The app validates it like an ID token (no `nonce`; it must carry `events` and must not carry `nonce`),
105 records each `jti` for 10 minutes (a repeat is already applied: it changes nothing and answers `200`), deletes the matching local sessions, and answers `200` with an empty
106 body. Any other answer is a failure.
107- `im` delivers through a durable outbox: retries with backoff (10 s, 30 s, 1 min, 5 min, then every 15 min)
108 for 24 hours. Combined with the 12-hour local session cap (2.4), a missed delivery cannot keep a revoked
109 user signed in for longer than 12 hours, even if the app was down.
110- The directory stream (section 3) also carries `disabled: true` and `deleted` events. An app that sees one
111 deletes the user's local sessions as well. Two independent paths; either one is enough.
112
113### 2.6 Machine-to-machine tokens
114
115- A client that calls another app's API asks `im` for a token: `POST /token`
116 `grant_type=client_credentials&scope=<scopes>&audience=<target key>` with its client auth. (`audience` is the
117 RFC 8707-style extension parameter; its value is a family key such as `in`.)
118- `im` checks the scopes against the client's allowed scopes (set by the admin) and returns an
119 `access_token` JWT: `iss`, `sub` = `client:<client_id>`, `azp` = `client_id`, `aud` = target key, `scope`
120 (space separated), `iat`, `exp` (10 min), `jti`, plus `svc` = the caller's family key.
121- The receiving app verifies signature, `iss`, `aud` equals its own key, `exp`, and the scope it needs. It
122 treats `svc` as the caller's identity. Callers cache the token until 60 s before `exp`.
123
124| Scope | Granted by default to | Lets the caller |
125|---|---|---|
126| `directory.read` | every family client | read the people directory and photos at `im` (section 3) |
127| `files.service` | `iz` | store and read its own objects in `in` (section 5) |
128
129---
130
131## 3. People directory (im)
132
133All routes need a client-credentials token with `aud = "im"` and scope `directory.read`.
134
135### 3.1 Member record
136
137```json
138{
139 "sub": "01J...",
140 "email": "a@example.com",
141 "name": "Ayşe Yılmaz",
142 "admin": false,
143 "disabled": false,
144 "locale": "tr",
145 "zoneinfo": "Europe/Istanbul",
146 "picture_version": 3,
147 "updated_at": "2026-10-28T09:00:00.000000000Z"
148}
149```
150
151### 3.2 Routes
152
153- `GET /api/v1/directory` → `{"members":[<member>...], "cursor":"<opaque>"}`. Every account, disabled ones
154 included (`disabled: true`). Deleted accounts are absent.
155- `GET /api/v1/directory/stream` → `text/event-stream`. Optional `Last-Event-ID` (or `?cursor=`) resumes from a
156 cursor. Events:
157 - `event: member`, `data: <member>`: a member was created or changed (including disabled/enabled).
158 - `event: deleted`, `data: {"sub":"..."}`: the account is gone.
159 - `event: resync`, `data: {}`: the cursor is too old; fetch `GET /api/v1/directory` again.
160 - Each event has an `id:` that is a cursor. Keep-alive comments every 30 s. `im` closes the stream after
161 50 minutes; the client reconnects with its last cursor.
162- `GET /api/v1/people/{sub}/photo` → image bytes with the stored `Content-Type`, or `404` when there is no photo.
163 `ETag: "p<picture_version>"`. With `?v=<picture_version>` matching, `Cache-Control: private, max-age=31536000,
164 immutable`; otherwise `private, no-cache`. Honors `If-None-Match` with `304`.
165
166Apps mirror the directory into a local `members` table (full fetch, then the stream), so pages never call `im`
167to show a name or an avatar. Apps proxy photos through their own origin and cache them on disk keyed by
168`(sub, picture_version)`; a browser never talks to `im` for a photo.
169
170---
171
172## 4. Service registry and switcher (im)
173
174- `GET /api/v1/family` (token with `aud = "im"`, scope `directory.read`) →
175 `{"services":[{"key":"in","name":"Files","url":"https://in.example.com","healthy":true,"checked_at":"..."}]}`
176 in the admin's order. `im` is included as `{"key":"im","name":"Account",...}`.
177- The list is owned by `im`'s admin. A service row is created with its OIDC client (the admin gives a key,
178 a name and the origin; the redirect and back-channel URIs are derived from the origin). Apps do not register
179 themselves.
180- `im` probes each row's `GET /healthz` every 60 s (2 s timeout) and reports `healthy`. Apps fetch the list
181 every 5 minutes and render the switcher from their copy; a stale copy is fine.
182
183### 4.1 Health
184
185Every app serves `GET /healthz` without auth: `200`, body `ok <build sha>`, `Content-Type: text/plain`.
186It checks that the process answers and that a `SELECT 1` on its database succeeds within 1 s; otherwise
187`503` with `fail <reason>`.
188
189---
190
191## 5. Object store (in)
192
193`in` stores bytes for other apps in a per-service namespace with its own quota. All routes need a
194client-credentials token with `aud = "in"` and scope `files.service`. The namespace is the token's `svc`.
195An object is named by the caller's `external_id` (1–128 chars of `[A-Za-z0-9._-]`).
196
197| Method and path | Request | Success |
198|---|---|---|
199| `PUT /api/v1/objects/{external_id}` | Raw body; `Content-Type` (stored as given, re-sniffed for display); optional `X-Object-Name` (UTF-8, percent-encoded, ≤255 bytes); optional `If-None-Match: *` | `201` (created) or `200` (replaced), JSON `{"external_id","size","sha256","content_type","created_at"}` |
200| `GET /api/v1/objects/{external_id}` | Optional `Range: bytes=a-b` (one range) | `200` or `206`, the bytes, `ETag: "<sha256>"`, `Content-Length`, `Accept-Ranges: bytes` |
201| `HEAD /api/v1/objects/{external_id}` | — | Headers as GET |
202| `DELETE /api/v1/objects/{external_id}` | — | `204`; also `204` when already absent (idempotent) |
203| `GET /api/v1/usage` | — | `{"quota_bytes": n, "used_bytes": n, "objects": n}` |
204
205- Uploads stream to disk; the body is never held in memory. A body larger than the remaining quota is refused
206 with `413` and error code `quota_exceeded` before or while it is read.
207- `PUT` is atomic: the new bytes are visible only after they are fully written and fsynced.
208- Service objects never appear in a person's drive and do not count against any person's quota.
209- The quota per service is set in `in`'s admin page (default 10 GiB).
210
211---
212
213## 6. Users across apps
214
215- The user id everywhere is `im`'s `sub` (a ULID). Apps never invent their own user ids for people.
216- An app learns a user when they first sign in or when the directory names them. A member that the directory
217 lists but who never signed in still exists in the app (so they can be assigned, mentioned, mailed).
218- App-level roles (for example `iz` Admin, Member, Viewer) belong to the app. `im`'s `admin` flag means
219 "instance administrator" and every app treats it as its own top role.
220
221---
222
223## 7. Errors
224
225Machine routes answer errors as:
226
227```json
228{"error": "quota_exceeded", "message": "The service quota of 10 GiB is used up."}
229```
230
231with the matching status: `400 invalid_request`, `401 invalid_token` (with `WWW-Authenticate: Bearer
232error="invalid_token"`), `403 insufficient_scope`, `404 not_found`, `409 conflict`, `413 quota_exceeded` or
233`too_large`, `429 rate_limited` (with `Retry-After`), `500 internal`, `503 unavailable`. `error` is a stable
234machine word; `message` is English for logs and may change. OAuth endpoints (`/token`, `/revoke`,
235`/introspect`) use the OAuth error format from their RFCs instead.
236
237---
238
239## 8. Change log
240
241- **v1** (2026-10-28): first version.