im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching·0 forks·Rust·AGPL-3.0·542.8 KiB·b9f0726·Tahinli·27 days ago·34 commits
| 1 | # Family contracts, v1 |
| 2 | |
| 3 | Status: 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 |
| 5 | bumped. The apps share no code. They share only the HTTP contracts written here. |
| 6 | |
| 7 | Contract 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 | |
| 19 | Production 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 | |
| 24 | 1. **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. |
| 26 | 2. **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. |
| 29 | 3. **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. |
| 31 | 4. **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. |
| 33 | 5. **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 | |
| 55 | Clients 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. A new key is published 24 hours before it signs anything. |
| 62 | - On admin demand (`im keys rotate`, or the admin Keys page) the rotation is immediate: the new key signs from |
| 63 | that moment, and every other key (pending, current or retired) is withdrawn at once. A withdrawn key is no |
| 64 | longer in `jwks.json`, its row is deleted, and `im` never signs or verifies with it again. This is the |
| 65 | emergency action after a suspected leak. |
| 66 | - Clients cache the JWKS for 10 minutes, and refetch at most once a minute when a token names an unknown `kid`. |
| 67 | |
| 68 | ### 2.3 Sign-in (authorization code + PKCE) |
| 69 | |
| 70 | 1. App redirects the browser to `/authorize` with `response_type=code`, `client_id`, `redirect_uri` (exact match |
| 71 | with a registered URI), `scope` containing `openid` (and `profile email`), `state` (≥128 bits), |
| 72 | `nonce` (≥128 bits), `code_challenge`, `code_challenge_method=S256`. PKCE is mandatory. |
| 73 | 2. `im` signs the user in (password, then TOTP or a recovery code if enrolled) and redirects back with |
| 74 | `code` and `state`. Codes live 60 seconds and are single use. A reused code revokes every token issued from it, |
| 75 | the access token included. |
| 76 | 3. App posts to `/token` (`grant_type=authorization_code`, `code`, `redirect_uri`, `code_verifier`), client auth |
| 77 | by `client_secret_basic` or `client_secret_post`. Response: `access_token` (JWT, 10 min), `id_token`, |
| 78 | `refresh_token`, `token_type: "Bearer"`, `expires_in`. |
| 79 | 4. App validates the `id_token`: signature against JWKS, `iss` exact, `aud` equals its `client_id`, `exp` in the |
| 80 | future, `iat` not more than 5 minutes in the future, `nonce` equal to the one it sent. |
| 81 | |
| 82 | **ID token claims:** `iss`, `sub` (the user's ULID, stable forever), `aud`, `exp`, `iat`, `auth_time`, `nonce`, |
| 83 | `sid` (the `im` browser session id), `email`, `email_verified` (always true: `im` only stores confirmed |
| 84 | addresses), `name`, and the `im` extensions `admin` (bool), `locale` (`en`|`tr`), `zoneinfo` (IANA name, for |
| 85 | example `Europe/Istanbul`), `picture_version` (integer, bumps when the photo changes). |
| 86 | |
| 87 | ### 2.4 App sessions |
| 88 | |
| 89 | After step 4 the app creates **its own local session** (its own table, its own `__Host-` cookie, its own CSRF |
| 90 | token), keyed by `sub`, and stores `sid` beside it. The app does not call `im` again for this session. |
| 91 | |
| 92 | - Local session lifetime: at most 12 hours of absolute age. After that the app sends the browser through |
| 93 | `/authorize` again. While the `im` session is alive this is an invisible redirect. |
| 94 | - The app may keep the `refresh_token` only if it needs to call `/userinfo` later. Apps in this family do not. |
| 95 | They discard it. |
| 96 | |
| 97 | ### 2.5 Sign-out and revocation |
| 98 | |
| 99 | - **App-initiated sign-out:** the app deletes its local session, then redirects to |
| 100 | `/logout?id_token_hint=...&post_logout_redirect_uri=<registered>&state=...` (OIDC RP-Initiated Logout). `im` |
| 101 | ends the `im` session and every app session tied to it (back-channel, below). |
| 102 | - **Back-Channel Logout** (OIDC Back-Channel Logout 1.0): each client registers a `backchannel_logout_uri` |
| 103 | (`<origin>/auth/backchannel-logout`). When an `im` session ends (sign-out, admin revoke, password change or |
| 104 | reset, user disabled or deleted, session expired by policy), `im` POSTs `logout_token=<JWT>` (form-encoded) |
| 105 | to every client that was used in that session. The token carries `iss`, `aud`, `iat`, `exp` (2 min), `jti`, |
| 106 | `events: {"http://schemas.openid.net/event/backchannel-logout": {}}`, and `sid` (and `sub`). For "disable" and |
| 107 | "delete" it carries only `sub`, which means "every session of this user". |
| 108 | - The app validates it like an ID token (no `nonce`; it must carry `events` and must not carry `nonce`), |
| 109 | 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 |
| 110 | body. Any other answer is a failure. |
| 111 | - `im` delivers through a durable outbox: retries with backoff (10 s, 30 s, 1 min, 5 min, then every 15 min) |
| 112 | for 24 hours. Combined with the 12-hour local session cap (2.4), a missed delivery cannot keep a revoked |
| 113 | user signed in for longer than 12 hours, even if the app was down. |
| 114 | - The directory stream (section 3) also carries `disabled: true` and `deleted` events. An app that sees one |
| 115 | deletes the user's local sessions as well. Two independent paths; either one is enough. |
| 116 | |
| 117 | ### 2.6 Machine-to-machine tokens |
| 118 | |
| 119 | - A client that calls another app's API asks `im` for a token: `POST /token` |
| 120 | `grant_type=client_credentials&scope=<scopes>&audience=<target key>` with its client auth. (`audience` is the |
| 121 | RFC 8707-style extension parameter; its value is a family key such as `in`.) |
| 122 | - `im` checks the scopes against the client's allowed scopes (set by the admin) and returns an |
| 123 | `access_token` JWT: `iss`, `sub` = `client:<client_id>`, `azp` = `client_id`, `aud` = target key, `scope` |
| 124 | (space separated), `iat`, `exp` (10 min), `jti`, plus `svc` = the caller's family key. |
| 125 | - The receiving app verifies signature, `iss`, `aud` equals its own key, `exp`, and the scope it needs. It |
| 126 | treats `svc` as the caller's identity. Callers cache the token until 60 s before `exp`. |
| 127 | |
| 128 | | Scope | Granted by default to | Lets the caller | |
| 129 | |---|---|---| |
| 130 | | `directory.read` | every family client | read the people directory and photos at `im` (section 3) | |
| 131 | | `files.service` | `iz` | store and read its own objects in `in` (section 5) | |
| 132 | |
| 133 | --- |
| 134 | |
| 135 | ## 3. People directory (im) |
| 136 | |
| 137 | All routes need a client-credentials token with `aud = "im"` and scope `directory.read`. |
| 138 | |
| 139 | ### 3.1 Member record |
| 140 | |
| 141 | ```json |
| 142 | { |
| 143 | "sub": "01J...", |
| 144 | "email": "a@example.com", |
| 145 | "name": "Ayşe Yılmaz", |
| 146 | "admin": false, |
| 147 | "disabled": false, |
| 148 | "locale": "tr", |
| 149 | "zoneinfo": "Europe/Istanbul", |
| 150 | "picture_version": 3, |
| 151 | "updated_at": "2026-10-28T09:00:00.000000000Z" |
| 152 | } |
| 153 | ``` |
| 154 | |
| 155 | ### 3.2 Routes |
| 156 | |
| 157 | - `GET /api/v1/directory` → `{"members":[<member>...], "cursor":"<opaque>"}`. Every account, disabled ones |
| 158 | included (`disabled: true`). Deleted accounts are absent. |
| 159 | - `GET /api/v1/directory/stream` → `text/event-stream`. Optional `Last-Event-ID` (or `?cursor=`) resumes from a |
| 160 | cursor. Events: |
| 161 | - `event: member`, `data: <member>`: a member was created or changed (including disabled/enabled). |
| 162 | - `event: deleted`, `data: {"sub":"..."}`: the account is gone. |
| 163 | - `event: resync`, `data: {}`: the cursor is too old; fetch `GET /api/v1/directory` again. |
| 164 | - Each event has an `id:` that is a cursor. Keep-alive comments every 30 s. `im` closes the stream after |
| 165 | 50 minutes; the client reconnects with its last cursor. |
| 166 | - `GET /api/v1/people/{sub}/photo` → image bytes with the stored `Content-Type`, or `404` when there is no photo. |
| 167 | `ETag: "p<picture_version>"`. With `?v=<picture_version>` matching, `Cache-Control: private, max-age=31536000, |
| 168 | immutable`; otherwise `private, no-cache`. Honors `If-None-Match` with `304`. |
| 169 | |
| 170 | Apps mirror the directory into a local `members` table (full fetch, then the stream), so pages never call `im` |
| 171 | to show a name or an avatar. Apps proxy photos through their own origin and cache them on disk keyed by |
| 172 | `(sub, picture_version)`; a browser never talks to `im` for a photo. |
| 173 | |
| 174 | --- |
| 175 | |
| 176 | ## 4. Service registry and switcher (im) |
| 177 | |
| 178 | - `GET /api/v1/family` (token with `aud = "im"`, scope `directory.read`) → |
| 179 | `{"services":[{"key":"in","name":"Files","url":"https://in.example.com","healthy":true,"checked_at":"..."}]}` |
| 180 | in the admin's order. `im` is included as `{"key":"im","name":"Account",...}`. |
| 181 | - The list is owned by `im`'s admin. A service row is created with its OIDC client (the admin gives a key, |
| 182 | a name and the origin; the redirect and back-channel URIs are derived from the origin). Apps do not register |
| 183 | themselves. |
| 184 | - `im` probes each row's `GET /healthz` every 60 s (2 s timeout) and reports `healthy`. Apps fetch the list |
| 185 | every 5 minutes and render the switcher from their copy; a stale copy is fine. |
| 186 | |
| 187 | ### 4.1 Health |
| 188 | |
| 189 | Every app serves `GET /healthz` without auth: `200`, body `ok <build sha>`, `Content-Type: text/plain`. |
| 190 | It checks that the process answers and that a `SELECT 1` on its database succeeds within 1 s; otherwise |
| 191 | `503` with `fail <reason>`. |
| 192 | |
| 193 | --- |
| 194 | |
| 195 | ## 5. Object store (in) |
| 196 | |
| 197 | `in` stores bytes for other apps in a per-service namespace with its own quota. All routes need a |
| 198 | client-credentials token with `aud = "in"` and scope `files.service`. The namespace is the token's `svc`. |
| 199 | An object is named by the caller's `external_id` (1–128 chars of `[A-Za-z0-9._-]`). |
| 200 | |
| 201 | | Method and path | Request | Success | |
| 202 | |---|---|---| |
| 203 | | `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"}` | |
| 204 | | `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` | |
| 205 | | `HEAD /api/v1/objects/{external_id}` | — | Headers as GET | |
| 206 | | `DELETE /api/v1/objects/{external_id}` | — | `204`; also `204` when already absent (idempotent) | |
| 207 | | `GET /api/v1/usage` | — | `{"quota_bytes": n, "used_bytes": n, "objects": n}` | |
| 208 | |
| 209 | - Uploads stream to disk; the body is never held in memory. A body larger than the remaining quota is refused |
| 210 | with `413` and error code `quota_exceeded` before or while it is read. |
| 211 | - `PUT` is atomic: the new bytes are visible only after they are fully written and fsynced. |
| 212 | - Service objects never appear in a person's drive and do not count against any person's quota. |
| 213 | - The quota per service is set in `in`'s admin page (default 10 GiB). |
| 214 | |
| 215 | --- |
| 216 | |
| 217 | ## 6. Users across apps |
| 218 | |
| 219 | - The user id everywhere is `im`'s `sub` (a ULID). Apps never invent their own user ids for people. |
| 220 | - An app learns a user when they first sign in or when the directory names them. A member that the directory |
| 221 | lists but who never signed in still exists in the app (so they can be assigned, mentioned, mailed). |
| 222 | - App-level roles (for example `iz` Admin, Member, Viewer) belong to the app. `im`'s `admin` flag means |
| 223 | "instance administrator" and every app treats it as its own top role. |
| 224 | |
| 225 | --- |
| 226 | |
| 227 | ## 7. Errors |
| 228 | |
| 229 | Machine routes answer errors as: |
| 230 | |
| 231 | ```json |
| 232 | {"error": "quota_exceeded", "message": "The service quota of 10 GiB is used up."} |
| 233 | ``` |
| 234 | |
| 235 | with the matching status: `400 invalid_request`, `401 invalid_token` (with `WWW-Authenticate: Bearer |
| 236 | error="invalid_token"`), `403 insufficient_scope`, `404 not_found`, `409 conflict`, `413 quota_exceeded` or |
| 237 | `too_large`, `429 rate_limited` (with `Retry-After`), `500 internal`, `503 unavailable`. `error` is a stable |
| 238 | machine word; `message` is English for logs and may change. OAuth endpoints (`/token`, `/revoke`, |
| 239 | `/introspect`) use the OAuth error format from their RFCs instead. |
| 240 | |
| 241 | --- |
| 242 | |
| 243 | ## 8. Change log |
| 244 | |
| 245 | - **v1** (2026-10-28): first version. |