@fabrika/iam 0.0.0-stage → 0.9.0-dev.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (312) hide show
  1. package/fabrika.config.ts +267 -0
  2. package/migrations/0001_init.sql +316 -0
  3. package/migrations/0002_oauth.sql +38 -0
  4. package/migrations/0003_contract_apps.sql +11 -0
  5. package/migrations/0004_membership.sql +93 -0
  6. package/migrations/0005_workloads.sql +23 -0
  7. package/migrations/0006_membership_transport_keys.sql +16 -0
  8. package/migrations/0007_member_directory.sql +7 -0
  9. package/migrations/0008_durable_audit.sql +28 -0
  10. package/migrations/0009_member_lookups.sql +7 -0
  11. package/migrations/0010_workload_issues.sql +6 -0
  12. package/migrations/0011_app_presentation.sql +15 -0
  13. package/migrations/0012_external_principals.sql +10 -0
  14. package/migrations/0013_app_mail_templates.sql +14 -0
  15. package/migrations/0014_invitation_wording.sql +8 -0
  16. package/migrations/0015_app_schema_revisions.sql +15 -0
  17. package/migrations/0016_principal_grant_revision.sql +9 -0
  18. package/migrations/0017_app_sign_ins.sql +12 -0
  19. package/migrations/0018_app_members.sql +13 -0
  20. package/migrations/0019_app_mail_html.sql +7 -0
  21. package/migrations/0020_public_workloads.sql +34 -0
  22. package/migrations/0021_installation_brand.sql +19 -0
  23. package/migrations/0022_password_policy.sql +12 -0
  24. package/migrations/0023_terminals.sql +155 -0
  25. package/migrations/0024_app_terminals.sql +10 -0
  26. package/migrations/0025_terminal_completion.sql +10 -0
  27. package/migrations/0026_terminal_generation.sql +12 -0
  28. package/migrations/0027_application_exact_actions.sql +28 -0
  29. package/migrations/0028_oauth_client_assertions.sql +10 -0
  30. package/migrations/0029_role_via.sql +6 -0
  31. package/migrations/0030_membership_role_via.sql +12 -0
  32. package/migrations/0031_person_locale.sql +11 -0
  33. package/migrations-postgres/0001_init.sql +331 -0
  34. package/migrations-postgres/0002_oauth.sql +36 -0
  35. package/migrations-postgres/0003_contract_apps.sql +8 -0
  36. package/migrations-postgres/0004_membership.sql +92 -0
  37. package/migrations-postgres/0005_workloads.sql +18 -0
  38. package/migrations-postgres/0006_membership_transport_keys.sql +12 -0
  39. package/migrations-postgres/0007_member_directory.sql +7 -0
  40. package/migrations-postgres/0008_durable_audit.sql +8 -0
  41. package/migrations-postgres/0009_member_lookups.sql +7 -0
  42. package/migrations-postgres/0010_workload_issues.sql +6 -0
  43. package/migrations-postgres/0011_app_presentation.sql +15 -0
  44. package/migrations-postgres/0012_external_principals.sql +10 -0
  45. package/migrations-postgres/0013_app_mail_templates.sql +14 -0
  46. package/migrations-postgres/0014_invitation_wording.sql +8 -0
  47. package/migrations-postgres/0015_app_schema_revisions.sql +15 -0
  48. package/migrations-postgres/0016_principal_grant_revision.sql +9 -0
  49. package/migrations-postgres/0017_app_sign_ins.sql +12 -0
  50. package/migrations-postgres/0018_app_members.sql +13 -0
  51. package/migrations-postgres/0019_app_mail_html.sql +7 -0
  52. package/migrations-postgres/0020_public_workloads.sql +9 -0
  53. package/migrations-postgres/0021_installation_brand.sql +19 -0
  54. package/migrations-postgres/0022_password_policy.sql +12 -0
  55. package/migrations-postgres/0023_terminals.sql +70 -0
  56. package/migrations-postgres/0024_app_terminals.sql +10 -0
  57. package/migrations-postgres/0025_terminal_completion.sql +10 -0
  58. package/migrations-postgres/0026_terminal_generation.sql +12 -0
  59. package/migrations-postgres/0027_application_exact_actions.sql +28 -0
  60. package/migrations-postgres/0028_oauth_client_assertions.sql +9 -0
  61. package/migrations-postgres/0029_role_via.sql +4 -0
  62. package/migrations-postgres/0030_membership_role_via.sql +7 -0
  63. package/migrations-postgres/0031_person_locale.sql +11 -0
  64. package/package.json +70 -5
  65. package/src/admin/access.ts +47 -0
  66. package/src/admin/api-keys.ts +280 -0
  67. package/src/admin/app-people.ts +209 -0
  68. package/src/admin/app-schema.ts +308 -0
  69. package/src/admin/audit.ts +81 -0
  70. package/src/admin/brand.ts +54 -0
  71. package/src/admin/connections.ts +48 -0
  72. package/src/admin/context.ts +70 -0
  73. package/src/admin/grant-support.ts +192 -0
  74. package/src/admin/grants.ts +189 -0
  75. package/src/admin/handlers.ts +102 -0
  76. package/src/admin/http.ts +24 -0
  77. package/src/admin/membership-keys.ts +38 -0
  78. package/src/admin/pagination.ts +28 -0
  79. package/src/admin/password-policy.ts +48 -0
  80. package/src/admin/passwords.ts +78 -0
  81. package/src/admin/policies.ts +136 -0
  82. package/src/admin/principals.ts +226 -0
  83. package/src/admin/router.ts +291 -0
  84. package/src/admin/rpc.ts +387 -0
  85. package/src/admin/sessions.ts +115 -0
  86. package/src/admin/share-links.ts +127 -0
  87. package/src/admin/terminals.ts +115 -0
  88. package/src/admin/user-admin.ts +76 -0
  89. package/src/admin/workloads.ts +85 -0
  90. package/src/app-presentation.ts +178 -0
  91. package/src/app.ts +158 -0
  92. package/src/auth/cookies.ts +64 -0
  93. package/src/auth/failure.ts +25 -0
  94. package/src/auth/handoff.ts +136 -0
  95. package/src/auth/http.ts +88 -0
  96. package/src/auth/invitations.ts +220 -0
  97. package/src/auth/known-device.ts +44 -0
  98. package/src/auth/login.ts +162 -0
  99. package/src/auth/oidc-flight.ts +91 -0
  100. package/src/auth/oidc.ts +148 -0
  101. package/src/auth/password.ts +416 -0
  102. package/src/auth/routes.ts +108 -0
  103. package/src/auth/sessions.ts +249 -0
  104. package/src/auth/terminals.ts +284 -0
  105. package/src/auth.ts +193 -0
  106. package/src/binding-identity.ts +25 -0
  107. package/src/bounded-body.ts +35 -0
  108. package/src/catalog/cs.ts +147 -0
  109. package/src/catalog/en.ts +148 -0
  110. package/src/catalog/index.ts +122 -0
  111. package/src/catalog/uk.ts +164 -0
  112. package/src/client-address.ts +39 -0
  113. package/src/common-passwords.ts +37 -0
  114. package/src/contract-rules.ts +80 -0
  115. package/src/cron.ts +54 -0
  116. package/src/db.ts +128 -0
  117. package/src/delegation.ts +100 -0
  118. package/src/device-http.ts +39 -0
  119. package/src/device.ts +269 -0
  120. package/src/email-identity.ts +2 -0
  121. package/src/env.ts +132 -0
  122. package/src/handoff.ts +175 -0
  123. package/src/html.ts +4 -0
  124. package/src/human-admission.ts +40 -0
  125. package/src/identification.ts +185 -0
  126. package/src/index.ts +292 -0
  127. package/src/installation-brand.ts +82 -0
  128. package/src/issue.ts +265 -0
  129. package/src/json.ts +30 -0
  130. package/src/log.ts +26 -0
  131. package/src/membership-http.ts +135 -0
  132. package/src/membership.ts +1210 -0
  133. package/src/messages.ts +44 -0
  134. package/src/narrow-permissions.ts +26 -0
  135. package/src/node/browser-identity.ts +69 -0
  136. package/src/node/migrate.ts +52 -0
  137. package/src/node/prune.ts +34 -0
  138. package/src/node/runtime.ts +221 -0
  139. package/src/node/server.ts +104 -0
  140. package/src/oauth-client-auth.ts +93 -0
  141. package/src/oauth-client.ts +145 -0
  142. package/src/oauth-config.ts +72 -0
  143. package/src/oauth-http.ts +262 -0
  144. package/src/oauth.ts +272 -0
  145. package/src/oidc.ts +260 -0
  146. package/src/origin.ts +29 -0
  147. package/src/password-crypto.ts +148 -0
  148. package/src/password-db.ts +419 -0
  149. package/src/password-minimum.ts +44 -0
  150. package/src/password-pages.ts +397 -0
  151. package/src/password-policy.ts +79 -0
  152. package/src/password-service.ts +67 -0
  153. package/src/person-locale.ts +103 -0
  154. package/src/proxy-proof.ts +36 -0
  155. package/src/relay-mail.ts +64 -0
  156. package/src/repositories/app-schema.ts +550 -0
  157. package/src/repositories/audit.ts +405 -0
  158. package/src/repositories/brand.ts +66 -0
  159. package/src/repositories/credentials.ts +331 -0
  160. package/src/repositories/device.ts +137 -0
  161. package/src/repositories/grants.ts +404 -0
  162. package/src/repositories/handoff.ts +103 -0
  163. package/src/repositories/membership-transport.ts +49 -0
  164. package/src/repositories/membership.ts +875 -0
  165. package/src/repositories/oauth.ts +122 -0
  166. package/src/repositories/password-policy.ts +44 -0
  167. package/src/repositories/principals.ts +358 -0
  168. package/src/repositories/sessions.ts +339 -0
  169. package/src/repositories/sql.ts +23 -0
  170. package/src/repositories/terminals.ts +346 -0
  171. package/src/repositories/workloads.ts +134 -0
  172. package/src/request-id.ts +46 -0
  173. package/src/resolve.ts +396 -0
  174. package/src/retention.ts +34 -0
  175. package/src/roles.ts +54 -0
  176. package/src/rpc-http.ts +333 -0
  177. package/src/rpc-inputs.ts +110 -0
  178. package/src/rpc.ts +440 -0
  179. package/src/secret.ts +45 -0
  180. package/src/service.ts +59 -0
  181. package/src/services.ts +199 -0
  182. package/src/signing.ts +260 -0
  183. package/src/stored-chain.ts +23 -0
  184. package/src/stored-permissions.ts +34 -0
  185. package/src/terminals.ts +15 -0
  186. package/src/tokens.ts +506 -0
  187. package/src/workload-token.ts +130 -0
  188. package/types/fabrika.config.d.ts +11 -0
  189. package/types/src/admin/access.d.ts +19 -0
  190. package/types/src/admin/api-keys.d.ts +26 -0
  191. package/types/src/admin/app-people.d.ts +8 -0
  192. package/types/src/admin/app-schema.d.ts +32 -0
  193. package/types/src/admin/audit.d.ts +10 -0
  194. package/types/src/admin/brand.d.ts +10 -0
  195. package/types/src/admin/connections.d.ts +5 -0
  196. package/types/src/admin/context.d.ts +46 -0
  197. package/types/src/admin/grant-support.d.ts +59 -0
  198. package/types/src/admin/grants.d.ts +8 -0
  199. package/types/src/admin/handlers.d.ts +82 -0
  200. package/types/src/admin/http.d.ts +9 -0
  201. package/types/src/admin/membership-keys.d.ts +8 -0
  202. package/types/src/admin/pagination.d.ts +5 -0
  203. package/types/src/admin/password-policy.d.ts +9 -0
  204. package/types/src/admin/passwords.d.ts +5 -0
  205. package/types/src/admin/policies.d.ts +15 -0
  206. package/types/src/admin/principals.d.ts +15 -0
  207. package/types/src/admin/router.d.ts +75 -0
  208. package/types/src/admin/rpc.d.ts +6 -0
  209. package/types/src/admin/sessions.d.ts +14 -0
  210. package/types/src/admin/share-links.d.ts +21 -0
  211. package/types/src/admin/terminals.d.ts +13 -0
  212. package/types/src/admin/user-admin.d.ts +30 -0
  213. package/types/src/admin/workloads.d.ts +5 -0
  214. package/types/src/app-presentation.d.ts +86 -0
  215. package/types/src/app.d.ts +62 -0
  216. package/types/src/auth/cookies.d.ts +29 -0
  217. package/types/src/auth/failure.d.ts +47 -0
  218. package/types/src/auth/handoff.d.ts +66 -0
  219. package/types/src/auth/http.d.ts +29 -0
  220. package/types/src/auth/invitations.d.ts +18 -0
  221. package/types/src/auth/known-device.d.ts +12 -0
  222. package/types/src/auth/login.d.ts +10 -0
  223. package/types/src/auth/oidc-flight.d.ts +39 -0
  224. package/types/src/auth/oidc.d.ts +7 -0
  225. package/types/src/auth/password.d.ts +7 -0
  226. package/types/src/auth/routes.d.ts +15 -0
  227. package/types/src/auth/sessions.d.ts +60 -0
  228. package/types/src/auth/terminals.d.ts +47 -0
  229. package/types/src/auth.d.ts +70 -0
  230. package/types/src/binding-identity.d.ts +12 -0
  231. package/types/src/bounded-body.d.ts +12 -0
  232. package/types/src/catalog/cs.d.ts +2 -0
  233. package/types/src/catalog/en.d.ts +2 -0
  234. package/types/src/catalog/index.d.ts +112 -0
  235. package/types/src/catalog/uk.d.ts +2 -0
  236. package/types/src/client-address.d.ts +31 -0
  237. package/types/src/common-passwords.d.ts +2 -0
  238. package/types/src/contract-rules.d.ts +25 -0
  239. package/types/src/cron.d.ts +15 -0
  240. package/types/src/db.d.ts +58 -0
  241. package/types/src/delegation.d.ts +38 -0
  242. package/types/src/device-http.d.ts +2 -0
  243. package/types/src/device.d.ts +11 -0
  244. package/types/src/email-identity.d.ts +1 -0
  245. package/types/src/env.d.ts +127 -0
  246. package/types/src/handoff.d.ts +57 -0
  247. package/types/src/html.d.ts +2 -0
  248. package/types/src/human-admission.d.ts +22 -0
  249. package/types/src/identification.d.ts +30 -0
  250. package/types/src/index.d.ts +62 -0
  251. package/types/src/installation-brand.d.ts +22 -0
  252. package/types/src/issue.d.ts +62 -0
  253. package/types/src/json.d.ts +15 -0
  254. package/types/src/log.d.ts +15 -0
  255. package/types/src/membership-http.d.ts +8 -0
  256. package/types/src/membership.d.ts +156 -0
  257. package/types/src/messages.d.ts +20 -0
  258. package/types/src/narrow-permissions.d.ts +7 -0
  259. package/types/src/node/browser-identity.d.ts +1 -0
  260. package/types/src/node/migrate.d.ts +4 -0
  261. package/types/src/node/prune.d.ts +1 -0
  262. package/types/src/node/runtime.d.ts +51 -0
  263. package/types/src/node/server.d.ts +5 -0
  264. package/types/src/oauth-client-auth.d.ts +26 -0
  265. package/types/src/oauth-client.d.ts +17 -0
  266. package/types/src/oauth-config.d.ts +16 -0
  267. package/types/src/oauth-http.d.ts +7 -0
  268. package/types/src/oauth.d.ts +51 -0
  269. package/types/src/oidc.d.ts +81 -0
  270. package/types/src/origin.d.ts +9 -0
  271. package/types/src/password-crypto.d.ts +37 -0
  272. package/types/src/password-db.d.ts +134 -0
  273. package/types/src/password-minimum.d.ts +23 -0
  274. package/types/src/password-pages.d.ts +143 -0
  275. package/types/src/password-policy.d.ts +19 -0
  276. package/types/src/password-service.d.ts +24 -0
  277. package/types/src/person-locale.d.ts +50 -0
  278. package/types/src/proxy-proof.d.ts +21 -0
  279. package/types/src/relay-mail.d.ts +25 -0
  280. package/types/src/repositories/app-schema.d.ts +244 -0
  281. package/types/src/repositories/audit.d.ts +187 -0
  282. package/types/src/repositories/brand.d.ts +41 -0
  283. package/types/src/repositories/credentials.d.ts +148 -0
  284. package/types/src/repositories/device.d.ts +50 -0
  285. package/types/src/repositories/grants.d.ts +177 -0
  286. package/types/src/repositories/handoff.d.ts +52 -0
  287. package/types/src/repositories/membership-transport.d.ts +31 -0
  288. package/types/src/repositories/membership.d.ts +273 -0
  289. package/types/src/repositories/oauth.d.ts +55 -0
  290. package/types/src/repositories/password-policy.d.ts +25 -0
  291. package/types/src/repositories/principals.d.ts +159 -0
  292. package/types/src/repositories/sessions.d.ts +150 -0
  293. package/types/src/repositories/sql.d.ts +13 -0
  294. package/types/src/repositories/terminals.d.ts +144 -0
  295. package/types/src/repositories/workloads.d.ts +61 -0
  296. package/types/src/request-id.d.ts +25 -0
  297. package/types/src/resolve.d.ts +118 -0
  298. package/types/src/retention.d.ts +18 -0
  299. package/types/src/roles.d.ts +34 -0
  300. package/types/src/rpc-http.d.ts +53 -0
  301. package/types/src/rpc-inputs.d.ts +707 -0
  302. package/types/src/rpc.d.ts +4 -0
  303. package/types/src/secret.d.ts +21 -0
  304. package/types/src/service.d.ts +19 -0
  305. package/types/src/services.d.ts +72 -0
  306. package/types/src/signing.d.ts +72 -0
  307. package/types/src/stored-chain.d.ts +6 -0
  308. package/types/src/stored-permissions.d.ts +12 -0
  309. package/types/src/terminals.d.ts +7 -0
  310. package/types/src/tokens.d.ts +113 -0
  311. package/types/src/workload-token.d.ts +7 -0
  312. package/README.md +0 -3
@@ -0,0 +1,11 @@
1
+ -- The language a person reads IAM in, beside the application's (`app_presentation.locale`). SQLite/D1 dialect;
2
+ -- `../migrations-postgres/0031_person_locale.sql` states the same outcome.
3
+ --
4
+ -- An application may name the invited person's language on an invitation (`InvitationOptions.locale`): IAM mails the
5
+ -- invitation in it, and its acceptance keeps it as the principal's. A password action keeps the language its mail was
6
+ -- written in, so the page its link opens speaks the same one. None of the three is checked here: IAM parses each at
7
+ -- read and treats a value it does not speak as not stated. NULL on every row before this file and on one that names
8
+ -- none, which keeps the application's language as before.
9
+ ALTER TABLE membership_invitations ADD COLUMN locale TEXT;
10
+ ALTER TABLE principals ADD COLUMN locale TEXT;
11
+ ALTER TABLE password_action_tokens ADD COLUMN locale TEXT;
@@ -0,0 +1,331 @@
1
+ -- IAM schema — POSTGRES dialect. `../migrations/0001_init.sql` states the same schema for SQLite/D1.
2
+ -- What must match is the OUTCOME: `src/db.ts` runs against both unchanged — same column names, same
3
+ -- constraints, same partial unique indexes. Add every later change to both sets.
4
+ --
5
+ -- ── TYPES: the rule, and the one exception ───────────────────────────────────────────────────────
6
+ --
7
+ -- Bun's Postgres client decodes by COLUMN TYPE OID, not by value: `int8`/`numeric` come back as a
8
+ -- STRING, always, even for a value of 5 (see @fabrika/platform-node's sql-postgres.ts). D1 hands back
9
+ -- a `number`. So every column a row shape in `src/db.ts` types as `number` is `INTEGER` (int4) here.
10
+ --
11
+ -- That works because EVERY timestamp column in this schema is unix SECONDS, bound by the writer.
12
+ -- int4 holds those until 2038-01-19. This schema DOES have a 2038 problem; it is noted, not solved
13
+ -- (solving it means widening the columns AND teaching the readers to accept a string). A
14
+ -- unix-MILLISECOND column could not be int4 at all; if one is ever added it must be BIGINT and its
15
+ -- reader must handle a string.
16
+ --
17
+ -- THE EXCEPTION IS `auth_log.id` — see the block on that table. It is BIGINT, it is the only BIGINT
18
+ -- in this file, and `AuthLogRow.id` is typed `number | string` because of it.
19
+
20
+ -- ── Policies ─────────────────────────────────────────────────────────────────────────────────────
21
+
22
+ CREATE TABLE principals (
23
+ id TEXT PRIMARY KEY, -- UUIDv7, ours; STABLE — grants & audit reference this, never external_id
24
+ type TEXT NOT NULL CHECK (type IN ('user','service')),
25
+ external_id TEXT, -- IdP `sub` (user) / client_id (service). NULL = user INVITED, not yet claimed
26
+ email TEXT, -- users: the NORMALIZED mailbox, invite-match key + label source; NULL for services
27
+ label TEXT NOT NULL, -- email / token name, human-readable
28
+ disabled_at INTEGER, -- unix SECONDS; soft-disable, NULL = active
29
+ created_at INTEGER NOT NULL, -- unix SECONDS, bound by the writer (no DDL default)
30
+ activated_at INTEGER -- set once a user claims (OIDC) or enrolls a password; services at creation
31
+ );
32
+
33
+ -- Status is derived: invited (external_id IS NULL) → claimed/active → disabled (disabled_at set).
34
+ -- external_id is NULL for invited-not-yet-claimed users, and BOTH engines treat NULLs as distinct in
35
+ -- a UNIQUE index, so these stay partial indexes rather than table constraints — identical to SQLite.
36
+ CREATE UNIQUE INDEX idx_principals_uq_external ON principals(type, external_id)
37
+ WHERE external_id IS NOT NULL;
38
+ CREATE UNIQUE INDEX idx_principals_uq_email ON principals(email)
39
+ WHERE type = 'user' AND email IS NOT NULL;
40
+
41
+ -- The seeded PROVISIONING identity. `resolveCaller` (src/auth.ts) resolves a bearer matching the
42
+ -- FABRIKA_IAM_PROVISIONING_KEY secret to a synthetic global admin with this id; the row exists so the
43
+ -- audit that key drives resolves its principal_id FK. A migration cannot bind a parameter, so
44
+ -- `created_at` is a fixed literal.
45
+ INSERT INTO principals (id, type, external_id, email, label, disabled_at, created_at, activated_at)
46
+ VALUES ('provisioning-admin', 'service', NULL, NULL, 'provisioning', NULL, 1782561600, 1782561600);
47
+
48
+ -- Reserves a normalized mailbox for exactly one user principal.
49
+ CREATE TABLE principal_email_claims (
50
+ normalized_email TEXT PRIMARY KEY,
51
+ principal_id TEXT NOT NULL UNIQUE REFERENCES principals(id) ON DELETE CASCADE
52
+ );
53
+
54
+ -- ── App-declared vocabulary (reconciled from each app's code) ─────────────────────────────────────
55
+
56
+ -- The scope DIMENSIONS an app understands. Opaque to authz (scope matching is string equality); this
57
+ -- exists so the admin UI offers a real dropdown per app.
58
+ CREATE TABLE app_scopes (
59
+ app TEXT NOT NULL,
60
+ scope_type TEXT NOT NULL, -- 'organization' | 'team' | 'project' | …
61
+ label TEXT,
62
+ PRIMARY KEY (app, scope_type)
63
+ );
64
+
65
+ -- The ACTION CATALOG per app — every concrete action the app authorizes against.
66
+ CREATE TABLE app_actions (
67
+ app TEXT NOT NULL,
68
+ action TEXT NOT NULL, -- concrete action, e.g. 'project.read'
69
+ description TEXT,
70
+ PRIMARY KEY (app, action)
71
+ );
72
+
73
+ -- Named permission BUNDLES (AWS "managed policies"). `permissions` is a JSON array of action
74
+ -- patterns. JSON validity is NOT a DB constraint here and must NOT become one: `json_valid()` is
75
+ -- SQLite-only and `IS JSON` is Postgres-only, so a check on either side would make the two schemas
76
+ -- disagree. The guarantee lives at the two ends instead — the only writer JSON-encodes, every reader
77
+ -- goes through `parseJsonOrNull` (src/json.ts).
78
+ CREATE TABLE roles (
79
+ app TEXT NOT NULL,
80
+ role_key TEXT NOT NULL,
81
+ name TEXT NOT NULL,
82
+ description TEXT,
83
+ permissions TEXT NOT NULL, -- JSON array of action patterns
84
+ origin TEXT NOT NULL CHECK (origin IN ('app', 'custom')), -- 'app'=reconciled from code, 'custom'=admin-made
85
+ created_at INTEGER NOT NULL, -- unix SECONDS
86
+ PRIMARY KEY (app, role_key)
87
+ );
88
+
89
+ -- Return origins an app registered for the cross-host session handoff (ADR-0021).
90
+ CREATE TABLE app_return_origins (
91
+ app TEXT NOT NULL,
92
+ origin TEXT NOT NULL, -- canonical scheme://host[:port], lower-cased, no trailing slash
93
+ created_at INTEGER NOT NULL,
94
+ PRIMARY KEY (app, origin)
95
+ );
96
+
97
+ -- ── grants ───────────────────────────────────────────────────────────────────────────────────────
98
+ --
99
+ -- Scope is the generic, flat (scope_type, scope_value) pair. A grant is EITHER role-based OR inline,
100
+ -- never both, never neither. role_key is deliberately NOT FK'd to roles(app, role_key): a grant may
101
+ -- set app=NULL (cross-app) while a role row always has a concrete app, so no FK can express it —
102
+ -- validation happens in the service.
103
+ CREATE TABLE grants (
104
+ id TEXT PRIMARY KEY, -- UUIDv7
105
+ principal_id TEXT NOT NULL REFERENCES principals(id) ON DELETE CASCADE,
106
+ app TEXT, -- NULL = all apps (cross-app, e.g. super-admin)
107
+ role_key TEXT, -- named role/policy; XOR permissions
108
+ permissions TEXT, -- inline action set (JSON array); XOR role_key
109
+ scope_type TEXT, -- NULL = global (all scopes)
110
+ scope_value TEXT, -- opaque, app-owned; NULL = global
111
+ granted_by TEXT REFERENCES principals(id),
112
+ expires_at INTEGER, -- unix SECONDS; NULL = permanent
113
+ created_at INTEGER NOT NULL, -- unix SECONDS
114
+ CHECK ((role_key IS NULL) <> (permissions IS NULL)), -- exactly one of role_key / inline permissions
115
+ CHECK ((scope_type IS NULL) = (scope_value IS NULL)) -- scope is both-or-neither
116
+ );
117
+
118
+ -- The partial unique indexes, character-for-character as in the SQLite set. They must match EXACTLY:
119
+ -- `ON CONFLICT` infers a target from the index predicate + expression list, so a schema that spells
120
+ -- `COALESCE(app, '*')` differently (or drops the `WHERE`) silently stops inferring. They also
121
+ -- constrain ONLY role-based grants — inline grants are distinct attachments and may legitimately
122
+ -- repeat. NULL app folds to '*' because raw NULLs compare as distinct in a unique index on both
123
+ -- engines, which would otherwise allow unlimited duplicate cross-app grants.
124
+ CREATE UNIQUE INDEX idx_grants_uq_scoped ON grants(principal_id, role_key, scope_type, scope_value, COALESCE(app, '*'))
125
+ WHERE role_key IS NOT NULL AND scope_value IS NOT NULL;
126
+ CREATE UNIQUE INDEX idx_grants_uq_global ON grants(principal_id, role_key, COALESCE(app, '*'))
127
+ WHERE role_key IS NOT NULL AND scope_value IS NULL;
128
+
129
+ CREATE INDEX idx_grants_principal ON grants(principal_id);
130
+
131
+ -- ── Credentials (unified opaque API keys / share links) ──────────────────────────────────────────
132
+ --
133
+ -- ONE stored thing: an opaque `px_` secret (only its SHA-256 hash kept) resolved into a short signed
134
+ -- access token. principal_id set → the credential carries that principal's LIVE permissions;
135
+ -- principal_id NULL → an anonymous credential whose FROZEN inline grants, bound to `app`, are the
136
+ -- whole permission set.
137
+ CREATE TABLE credentials (
138
+ id TEXT PRIMARY KEY, -- UUIDv7
139
+ token_hash TEXT NOT NULL UNIQUE, -- SHA-256 of the opaque `px_` secret; never plaintext
140
+ label TEXT,
141
+ principal_id TEXT REFERENCES principals(id) ON DELETE CASCADE, -- NULL = anonymous (frozen inline grants)
142
+ issued_by TEXT REFERENCES principals(id), -- resolved server-side at issue, never self-asserted
143
+ expires_at INTEGER, -- unix SECONDS; NULL = no expiry
144
+ revoked_at INTEGER, -- unix SECONDS; NULL = active
145
+ created_at INTEGER NOT NULL, -- unix SECONDS
146
+ app TEXT -- the app it was issued for; an anonymous credential without one never resolves
147
+ );
148
+
149
+ CREATE INDEX idx_credentials_principal ON credentials(principal_id);
150
+ CREATE INDEX idx_credentials_expires ON credentials(expires_at);
151
+ CREATE INDEX idx_credentials_app ON credentials(app);
152
+
153
+ -- What an inline-grant credential confers: 1..N (action, scope) entries, matched by permits().
154
+ CREATE TABLE credential_grants (
155
+ credential_id TEXT NOT NULL REFERENCES credentials(id) ON DELETE CASCADE,
156
+ action TEXT NOT NULL,
157
+ scope_type TEXT,
158
+ scope_value TEXT,
159
+ CHECK ((scope_type IS NULL) = (scope_value IS NULL))
160
+ );
161
+
162
+ CREATE INDEX idx_credential_grants_credential ON credential_grants(credential_id);
163
+
164
+ -- ── Audit ────────────────────────────────────────────────────────────────────────────────────────
165
+
166
+ -- Domain events: what actually changed (produced by apps). `credential_id` is set when the actor is
167
+ -- an anonymous credential, in which case principal_id is NULL.
168
+ CREATE TABLE audit_events (
169
+ id TEXT PRIMARY KEY, -- UUIDv7 = time-sortable; the keyset cursor
170
+ request_id TEXT NOT NULL, -- correlates with auth_log
171
+ principal_id TEXT REFERENCES principals(id) ON DELETE SET NULL,
172
+ principal_label TEXT NOT NULL, -- SNAPSHOT, survives principal deletion
173
+ credential_id TEXT, -- anonymous-actor linkage; principal_id is NULL then
174
+ app TEXT NOT NULL, -- self-asserted caller id (audit() carries no token)
175
+ action TEXT NOT NULL, -- 'project.settings.update'
176
+ resource_type TEXT NOT NULL, -- 'project'
177
+ resource_id TEXT,
178
+ diff TEXT, -- JSON text; validity enforced at the code ends, not here (see `roles`)
179
+ metadata TEXT, -- JSON text; same
180
+ created_at INTEGER NOT NULL -- unix SECONDS
181
+ );
182
+
183
+ CREATE INDEX idx_audit_resource ON audit_events(resource_type, resource_id, created_at);
184
+ CREATE INDEX idx_audit_principal ON audit_events(principal_id, created_at);
185
+ CREATE INDEX idx_audit_request ON audit_events(request_id);
186
+
187
+ -- Auth log: every mint/authenticate outcome (produced by the IAM service). Dense, high-churn,
188
+ -- pruned on a schedule; never FK'd.
189
+ --
190
+ -- `id` IS THE ONE COLUMN WITH NO SQLITE ∩ POSTGRES SPELLING. `writeAuthLog()` omits it and needs the
191
+ -- engine to assign it. SQLite gets that from `INTEGER PRIMARY KEY` (the rowid alias) — which Postgres
192
+ -- reads as a plain int4 with no default, so every insert would violate NOT NULL. Postgres gets it
193
+ -- from `GENERATED BY DEFAULT AS IDENTITY` — which SQLite cannot parse at all. The two sets diverge
194
+ -- here ON PURPOSE.
195
+ --
196
+ -- CONSEQUENCE, and it is not cosmetic: an identity column is int8, so Bun decodes `id` as a STRING —
197
+ -- `'42'`, not `42`. `AuthLogRow.id` is therefore typed `number | string` (a `number` on D1, a
198
+ -- `string` here) and `toAuthLogDto` normalises it once, at the single reader. Do NOT "fix" this by
199
+ -- making the column INTEGER: int4 identity silently stops at 2147483647 rows, and this is the
200
+ -- highest-volume table in the schema.
201
+ CREATE TABLE auth_log (
202
+ id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
203
+ request_id TEXT NOT NULL,
204
+ app TEXT NOT NULL, -- aud-derived (verified) when a valid token was presented; self-asserted otherwise
205
+ kind TEXT NOT NULL CHECK (kind IN ('authenticate')),
206
+ principal_id TEXT REFERENCES principals(id) ON DELETE SET NULL,
207
+ credential_id TEXT, -- set for an anonymous-credential authentication
208
+ decision TEXT NOT NULL CHECK (decision IN ('allow','deny')),
209
+ reason TEXT,
210
+ created_at INTEGER NOT NULL -- unix SECONDS; the prune scans this
211
+ );
212
+
213
+ CREATE INDEX idx_auth_log_principal ON auth_log(principal_id, created_at);
214
+ CREATE INDEX idx_auth_log_request ON auth_log(request_id);
215
+
216
+ -- ── Sessions ─────────────────────────────────────────────────────────────────────────────────────
217
+ --
218
+ -- The long-lived opaque credential a browser carries. Only the SHA-256 hash of the cookie value is
219
+ -- stored; the session is the source of truth for "who is logged in". `app` NULL marks an IAM session
220
+ -- (held on IAM's own host); non-NULL marks a child session the proxy holds on ONE app's host, and it
221
+ -- may mint only for that app. Revoking the parent revokes every child derived from it.
222
+ CREATE TABLE sessions (
223
+ id TEXT PRIMARY KEY, -- UUIDv7, ours
224
+ token_hash TEXT NOT NULL UNIQUE, -- SHA-256 of the opaque session cookie value
225
+ principal_id TEXT NOT NULL REFERENCES principals(id) ON DELETE CASCADE,
226
+ idp_sub TEXT, -- upstream IdP subject; NULL for a password session
227
+ email TEXT, -- snapshot, for the admin session list
228
+ created_at INTEGER NOT NULL, -- unix SECONDS
229
+ expires_at INTEGER NOT NULL, -- unix SECONDS, absolute session expiry
230
+ revoked_at INTEGER, -- unix SECONDS; explicit logout / admin kill, NULL = active
231
+ authentication_method TEXT NOT NULL CHECK (authentication_method IN ('oidc', 'password', 'local_dev')),
232
+ app TEXT,
233
+ parent_session_id TEXT REFERENCES sessions(id) ON DELETE CASCADE,
234
+ CONSTRAINT sessions_method_subject_check CHECK (
235
+ (authentication_method IN ('oidc', 'local_dev') AND idp_sub IS NOT NULL)
236
+ OR (authentication_method = 'password' AND idp_sub IS NULL)
237
+ )
238
+ );
239
+
240
+ CREATE INDEX idx_sessions_principal ON sessions(principal_id);
241
+ CREATE INDEX idx_sessions_expires ON sessions(expires_at);
242
+ CREATE INDEX idx_sessions_principal_method ON sessions(principal_id, authentication_method);
243
+ CREATE INDEX idx_sessions_parent ON sessions(parent_session_id);
244
+
245
+ -- One-time codes that hand a parent session over to an app host.
246
+ CREATE TABLE auth_codes (
247
+ id TEXT PRIMARY KEY,
248
+ code_hash TEXT NOT NULL UNIQUE,
249
+ app TEXT NOT NULL,
250
+ parent_session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
251
+ return_url TEXT NOT NULL,
252
+ expires_at INTEGER NOT NULL,
253
+ consumed_at INTEGER,
254
+ created_at INTEGER NOT NULL
255
+ );
256
+
257
+ CREATE INDEX idx_auth_codes_expires ON auth_codes(expires_at);
258
+
259
+ -- ── Password authentication ──────────────────────────────────────────────────────────────────────
260
+
261
+ CREATE TABLE password_accounts (
262
+ principal_id TEXT PRIMARY KEY REFERENCES principals(id) ON DELETE CASCADE,
263
+ state TEXT NOT NULL CHECK (state IN ('disabled', 'pending', 'enabled')),
264
+ created_at INTEGER NOT NULL,
265
+ updated_at INTEGER NOT NULL
266
+ );
267
+
268
+ CREATE TABLE password_credentials (
269
+ principal_id TEXT PRIMARY KEY REFERENCES principals(id) ON DELETE CASCADE,
270
+ algorithm TEXT NOT NULL,
271
+ parameters TEXT NOT NULL,
272
+ salt TEXT NOT NULL,
273
+ password_hash TEXT NOT NULL,
274
+ created_at INTEGER NOT NULL,
275
+ updated_at INTEGER NOT NULL
276
+ );
277
+
278
+ CREATE TABLE password_action_tokens (
279
+ id TEXT PRIMARY KEY,
280
+ principal_id TEXT NOT NULL REFERENCES principals(id) ON DELETE CASCADE,
281
+ purpose TEXT NOT NULL CHECK (purpose IN ('enrollment', 'reset')),
282
+ token_hash TEXT NOT NULL UNIQUE,
283
+ issued_by TEXT REFERENCES principals(id) ON DELETE SET NULL,
284
+ expires_at INTEGER NOT NULL,
285
+ consumed_at INTEGER,
286
+ consumption_id TEXT UNIQUE,
287
+ created_at INTEGER NOT NULL
288
+ );
289
+
290
+ CREATE INDEX idx_password_action_tokens_principal ON password_action_tokens(principal_id, purpose);
291
+ CREATE INDEX idx_password_action_tokens_expires ON password_action_tokens(expires_at);
292
+ CREATE UNIQUE INDEX uq_password_action_tokens_active
293
+ ON password_action_tokens(principal_id, purpose) WHERE consumed_at IS NULL;
294
+
295
+ CREATE TABLE password_login_throttles (
296
+ login_key_hash TEXT PRIMARY KEY,
297
+ window_started_at INTEGER NOT NULL,
298
+ attempt_count INTEGER NOT NULL CHECK (attempt_count > 0),
299
+ blocked_until INTEGER,
300
+ updated_at INTEGER NOT NULL
301
+ );
302
+
303
+ CREATE INDEX idx_password_login_throttles_updated ON password_login_throttles(updated_at);
304
+
305
+ -- ── Device authorization (builder pairing) ───────────────────────────────────────────────────────
306
+
307
+ CREATE TABLE device_authorizations (
308
+ id TEXT PRIMARY KEY,
309
+ app TEXT NOT NULL,
310
+ label TEXT NOT NULL,
311
+ permissions TEXT NOT NULL,
312
+ key_lifetime_seconds INTEGER NOT NULL CHECK (key_lifetime_seconds > 0 AND key_lifetime_seconds <= 2592000),
313
+ redemption_challenge TEXT NOT NULL,
314
+ approval_challenge TEXT NOT NULL,
315
+ expires_at INTEGER NOT NULL,
316
+ next_poll_at INTEGER NOT NULL,
317
+ status TEXT NOT NULL CHECK (status IN ('pending', 'approved', 'issued', 'denied', 'cancelled', 'authority_lost')),
318
+ approver_id TEXT,
319
+ consent_id TEXT UNIQUE,
320
+ scope_type TEXT,
321
+ scope_value TEXT,
322
+ key_expires_at INTEGER,
323
+ claim_id TEXT UNIQUE,
324
+ credential_id TEXT,
325
+ principal_id TEXT,
326
+ issued_at INTEGER,
327
+ created_at INTEGER NOT NULL
328
+ );
329
+
330
+ CREATE INDEX idx_device_authorizations_issuer ON device_authorizations (approver_id, app, id);
331
+ CREATE INDEX idx_device_authorizations_expiry ON device_authorizations (expires_at);
@@ -0,0 +1,36 @@
1
+ -- OAuth 2.1 authorization server for MCP clients. Postgres twin of ../migrations/0002_oauth.sql —
2
+ -- see that file for the model; the outcome is identical.
3
+ CREATE TABLE oauth_grants (
4
+ id TEXT PRIMARY KEY,
5
+ principal_id TEXT NOT NULL REFERENCES principals(id) ON DELETE CASCADE,
6
+ session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
7
+ client_id TEXT NOT NULL,
8
+ app TEXT NOT NULL,
9
+ resource TEXT NOT NULL,
10
+ scopes TEXT NOT NULL,
11
+ created_at INTEGER NOT NULL,
12
+ expires_at INTEGER NOT NULL,
13
+ revoked_at INTEGER
14
+ );
15
+ CREATE INDEX idx_oauth_grants_session ON oauth_grants(session_id);
16
+ CREATE INDEX idx_oauth_grants_principal ON oauth_grants(principal_id);
17
+
18
+ CREATE TABLE oauth_codes (
19
+ token_hash TEXT PRIMARY KEY,
20
+ grant_id TEXT NOT NULL REFERENCES oauth_grants(id) ON DELETE CASCADE,
21
+ redirect_uri TEXT NOT NULL,
22
+ challenge TEXT NOT NULL,
23
+ expires_at INTEGER NOT NULL,
24
+ used_at INTEGER
25
+ );
26
+ CREATE INDEX idx_oauth_codes_grant ON oauth_codes(grant_id);
27
+ CREATE INDEX idx_oauth_codes_expires ON oauth_codes(expires_at);
28
+
29
+ CREATE TABLE oauth_refresh_tokens (
30
+ token_hash TEXT PRIMARY KEY,
31
+ grant_id TEXT NOT NULL REFERENCES oauth_grants(id) ON DELETE CASCADE,
32
+ expires_at INTEGER NOT NULL,
33
+ used_at INTEGER
34
+ );
35
+ CREATE INDEX idx_oauth_refresh_tokens_grant ON oauth_refresh_tokens(grant_id);
36
+ CREATE INDEX idx_oauth_refresh_tokens_expires ON oauth_refresh_tokens(expires_at);
@@ -0,0 +1,8 @@
1
+ -- Contract applications. Postgres twin of ../migrations/0003_contract_apps.sql — see that file for
2
+ -- the model; the outcome is identical.
3
+ CREATE TABLE contract_apps (
4
+ app TEXT PRIMARY KEY,
5
+ created_at INTEGER NOT NULL
6
+ );
7
+
8
+ ALTER TABLE roles ADD COLUMN member_scope TEXT;
@@ -0,0 +1,92 @@
1
+ -- Membership, delegation grants and share links. Postgres twin of ../migrations/0004_membership.sql —
2
+ -- see that file for the model; the outcome is identical.
3
+
4
+ -- What a contract compiles into its schema: a dimension's container, its creator role and the actions
5
+ -- that manage and list its members; who may assign a membership role (JSON array of role keys).
6
+ ALTER TABLE app_scopes ADD COLUMN parent_scope TEXT;
7
+ ALTER TABLE app_scopes ADD COLUMN creator_role TEXT;
8
+ ALTER TABLE app_scopes ADD COLUMN members_manage TEXT;
9
+ ALTER TABLE app_scopes ADD COLUMN members_list TEXT;
10
+ ALTER TABLE roles ADD COLUMN assignable_by TEXT;
11
+
12
+ -- Share-link mounts. `grants` is a JSON array of exact actions, like `roles.permissions`.
13
+ CREATE TABLE app_link_mounts (
14
+ app TEXT NOT NULL,
15
+ mount TEXT NOT NULL,
16
+ dimension TEXT NOT NULL,
17
+ grants TEXT NOT NULL,
18
+ issued_by TEXT NOT NULL,
19
+ max_days INTEGER NOT NULL CHECK (max_days > 0),
20
+ PRIMARY KEY (app, mount)
21
+ );
22
+
23
+ -- The endpoints whose requests may spend the caller's membership authority through a delegation grant.
24
+ CREATE TABLE app_delegated_endpoints (
25
+ app TEXT NOT NULL,
26
+ endpoint TEXT NOT NULL,
27
+ action TEXT NOT NULL,
28
+ dimension TEXT NOT NULL,
29
+ purpose TEXT NOT NULL CHECK (purpose IN ('members', 'creates')),
30
+ PRIMARY KEY (app, endpoint)
31
+ );
32
+
33
+ -- A value IAM created for an app. Membership itself is ordinary role grants scoped to the value; this
34
+ -- row records who created it and is the row a revoke locks, so two concurrent revokes cannot each
35
+ -- remove one of the last two holders of the creator role.
36
+ CREATE TABLE membership_scopes (
37
+ app TEXT NOT NULL,
38
+ scope_type TEXT NOT NULL,
39
+ scope_value TEXT NOT NULL,
40
+ created_by TEXT REFERENCES principals(id) ON DELETE SET NULL,
41
+ created_at INTEGER NOT NULL,
42
+ PRIMARY KEY (app, scope_type, scope_value)
43
+ );
44
+
45
+ -- Single-use delegation grants (ADR-0022 rule 6): bound to one caller, one endpoint and one request,
46
+ -- stored only as a hash and spent by a conditional UPDATE, like `auth_codes`.
47
+ CREATE TABLE delegation_grants (
48
+ id TEXT PRIMARY KEY,
49
+ grant_hash TEXT NOT NULL UNIQUE,
50
+ app TEXT NOT NULL,
51
+ principal_id TEXT NOT NULL REFERENCES principals(id) ON DELETE CASCADE,
52
+ endpoint TEXT NOT NULL,
53
+ request_id TEXT NOT NULL,
54
+ expires_at INTEGER NOT NULL,
55
+ consumed_at INTEGER,
56
+ created_at INTEGER NOT NULL
57
+ );
58
+
59
+ CREATE INDEX idx_delegation_grants_expires ON delegation_grants(expires_at);
60
+
61
+ -- An invitation to a membership role in one value, accepted only by the verified owner of `email` (the
62
+ -- normalized mailbox). The one-time token travels in the invitation email and only its hash is kept.
63
+ CREATE TABLE membership_invitations (
64
+ id TEXT PRIMARY KEY,
65
+ token_hash TEXT NOT NULL UNIQUE,
66
+ app TEXT NOT NULL,
67
+ scope_type TEXT NOT NULL,
68
+ scope_value TEXT NOT NULL,
69
+ role_key TEXT NOT NULL,
70
+ email TEXT NOT NULL,
71
+ invited_by TEXT NOT NULL REFERENCES principals(id) ON DELETE CASCADE,
72
+ expires_at INTEGER NOT NULL,
73
+ accepted_at INTEGER,
74
+ accepted_by TEXT REFERENCES principals(id) ON DELETE SET NULL,
75
+ -- Set by the one acceptance that won; the grant is written only for the attempt that holds it.
76
+ acceptance_id TEXT UNIQUE,
77
+ revoked_at INTEGER,
78
+ created_at INTEGER NOT NULL
79
+ );
80
+
81
+ -- One open invitation per (value, role, mailbox), so repeating an invitation answers the same one. The
82
+ -- predicate must stay character-for-character equal to the Postgres set (`ON CONFLICT` infers from it).
83
+ CREATE UNIQUE INDEX idx_membership_invitations_open
84
+ ON membership_invitations(app, scope_type, scope_value, role_key, email)
85
+ WHERE accepted_at IS NULL AND revoked_at IS NULL;
86
+ CREATE INDEX idx_membership_invitations_expires ON membership_invitations(expires_at);
87
+
88
+ -- A share link is an anonymous credential bound to a link mount and the containment chain of its object
89
+ -- (a JSON array of `{type, value}`, the object first). NULL on every other credential.
90
+ ALTER TABLE credentials ADD COLUMN link_mount TEXT;
91
+ ALTER TABLE credentials ADD COLUMN link_dimension TEXT;
92
+ ALTER TABLE credentials ADD COLUMN link_chain TEXT;
@@ -0,0 +1,18 @@
1
+ -- Workload credentials. Postgres twin of ../migrations/0005_workloads.sql — see that file for the model; the outcome is
2
+ -- identical.
3
+
4
+ CREATE TABLE workload_principals (
5
+ principal_id TEXT PRIMARY KEY REFERENCES principals(id) ON DELETE CASCADE,
6
+ app TEXT NOT NULL,
7
+ kind TEXT NOT NULL CHECK (kind IN ('job', 'webhook', 'app')),
8
+ name TEXT NOT NULL,
9
+ UNIQUE (app, kind, name)
10
+ );
11
+
12
+ CREATE TABLE app_workloads (
13
+ app TEXT NOT NULL,
14
+ kind TEXT NOT NULL CHECK (kind IN ('job', 'webhook', 'app')),
15
+ name TEXT NOT NULL,
16
+ actions TEXT NOT NULL,
17
+ PRIMARY KEY (app, kind, name)
18
+ );
@@ -0,0 +1,12 @@
1
+ -- Membership transport keys. Postgres twin of ../migrations/0006_membership_transport_keys.sql — see that file for the
2
+ -- model; the outcome is identical.
3
+
4
+ CREATE TABLE membership_transport_keys (
5
+ id TEXT PRIMARY KEY,
6
+ app TEXT NOT NULL,
7
+ key_hash TEXT NOT NULL UNIQUE,
8
+ expires_at INTEGER NOT NULL,
9
+ created_at INTEGER NOT NULL
10
+ );
11
+
12
+ CREATE INDEX membership_transport_keys_app ON membership_transport_keys (app);
@@ -0,0 +1,7 @@
1
+ -- A dimension's member directory (`directory: 'labels'` in the contract's vocabulary). Postgres dialect;
2
+ -- `../migrations/0007_member_directory.sql` states the same outcome.
3
+ --
4
+ -- `listMembers` shows a user's label (its spelling of its address) to a holder of `members.manage` only, unless the
5
+ -- dimension declares `labels` here: then every holder of `members.list` sees it too. Emails and invitations stay with
6
+ -- `members.manage`.
7
+ ALTER TABLE app_scopes ADD COLUMN members_directory TEXT CHECK (members_directory IS NULL OR members_directory = 'labels');
@@ -0,0 +1,8 @@
1
+ -- Durable audit: a logout is an `auth_log` row of its own, and a login of an installation administrator is marked
2
+ -- privileged. Postgres dialect; `../migrations/0008_durable_audit.sql` states the same outcome. Every existing row
3
+ -- was a login attempt that nobody marked, so the column defaults to 0.
4
+ ALTER TABLE auth_log DROP CONSTRAINT auth_log_kind_check;
5
+
6
+ ALTER TABLE auth_log ADD CONSTRAINT auth_log_kind_check CHECK (kind IN ('authenticate', 'logout'));
7
+
8
+ ALTER TABLE auth_log ADD COLUMN privileged INTEGER NOT NULL DEFAULT 0 CHECK (privileged IN (0, 1));
@@ -0,0 +1,7 @@
1
+ -- The actions that look up one member of a dimension (`findMember`), from the endpoints on, in or below it that the
2
+ -- approved contract records under `directories`. Postgres dialect; `../migrations/0009_member_lookups.sql` states the
3
+ -- same outcome.
4
+ --
5
+ -- A JSON array of exact actions, or NULL for none. IAM answers a lookup only for one of them, held on the directory's
6
+ -- value itself, so the application names no containment IAM would have to trust.
7
+ ALTER TABLE app_scopes ADD COLUMN members_lookup TEXT;
@@ -0,0 +1,6 @@
1
+ -- The link mount a job may issue links of (`issues: '<mount>'` on its `c.cron`, read from the approved artifact).
2
+ -- Postgres dialect; `../migrations/0010_workload_issues.sql` states the same outcome.
3
+ --
4
+ -- `issueLink` and `revokeLink` accept a job's token only while its row here names the mount; every other membership
5
+ -- operation still refuses a workload. NULL for a job that issues nothing, and always for a webhook or a connector.
6
+ ALTER TABLE app_workloads ADD COLUMN issues TEXT CHECK (issues IS NULL OR kind = 'job');
@@ -0,0 +1,15 @@
1
+ -- How IAM names an application to a person (`locale` and `displayName` in the contract's vocabulary), and the
2
+ -- application a password action returns to. Postgres dialect; `../migrations/0011_app_presentation.sql`
3
+ -- states the same outcome.
4
+ --
5
+ -- The schema reconcile writes `app_presentation` in its own batch; an app that declares neither field has no row.
6
+ -- `locale` is not checked here: IAM parses it at read and falls back to its default for a value it does not speak.
7
+ CREATE TABLE app_presentation (
8
+ app TEXT PRIMARY KEY,
9
+ locale TEXT,
10
+ display_name TEXT
11
+ );
12
+
13
+ -- The application a forgot-password request came from: its pages and mail speak the application's language, and the
14
+ -- completed action lands in it. NULL for an action on the installation itself, which is every row before this file.
15
+ ALTER TABLE password_action_tokens ADD COLUMN app TEXT;
@@ -0,0 +1,10 @@
1
+ -- External principals and the roles they may hold (ADR-0051).
2
+ -- Postgres dialect; `../migrations/0012_external_principals.sql` states the same outcome.
3
+ --
4
+ -- A user is `internal` (every row written before this file) or `external`, a person from outside the organization;
5
+ -- a service is always `internal`. An external principal holds only grants of a role its application declares
6
+ -- `external`, which only a membership role reconciled from the application may be.
7
+ ALTER TABLE principals ADD COLUMN affiliation TEXT NOT NULL DEFAULT 'internal'
8
+ CHECK (affiliation IN ('internal', 'external') AND (affiliation = 'internal' OR type = 'user'));
9
+ ALTER TABLE roles ADD COLUMN external INTEGER NOT NULL DEFAULT 0
10
+ CHECK (external IN (0, 1) AND (external = 0 OR (member_scope IS NOT NULL AND origin = 'app')));
@@ -0,0 +1,14 @@
1
+ -- An application's wording of IAM's e-mails (`mail` in the contract's vocabulary): per message and language, the subject
2
+ -- and the plain-text body IAM fills and wraps in its own HTML. Postgres dialect; `../migrations/0013_app_mail_templates.sql`
3
+ -- states the same outcome.
4
+ --
5
+ -- The schema reconcile rewrites an app's rows in its own batch; an app that declares none has no row, and IAM sends its
6
+ -- catalog's wording. IAM checks a row again when it renders it and falls back to the catalog for one it refuses.
7
+ CREATE TABLE app_mail_templates (
8
+ app TEXT NOT NULL,
9
+ kind TEXT NOT NULL,
10
+ locale TEXT NOT NULL,
11
+ subject TEXT NOT NULL,
12
+ body TEXT NOT NULL,
13
+ PRIMARY KEY (app, kind, locale)
14
+ );
@@ -0,0 +1,8 @@
1
+ -- What an invitation by address carries besides its role: the invited person's company as the inviting application
2
+ -- names it, and the inviter's personal message. Postgres dialect; `../migrations/0014_invitation_wording.sql`
3
+ -- states the same outcome.
4
+ --
5
+ -- Both are plain text IAM puts into the invitation's e-mail (`{{companyLabel}}`, `{{message}}`), escaped in its HTML
6
+ -- part, and keeps for a resend. NULL on every invitation before this file and on one that names neither.
7
+ ALTER TABLE membership_invitations ADD COLUMN company_label TEXT;
8
+ ALTER TABLE membership_invitations ADD COLUMN message TEXT;
@@ -0,0 +1,15 @@
1
+ -- The revision of an application's reconciled schema and policies. Postgres dialect;
2
+ -- `../migrations/0015_app_schema_revisions.sql` states the same outcome.
3
+ --
4
+ -- Every schema reconcile and every custom-policy write of the app increments it in its own transaction, as its first
5
+ -- statement. An invitation's acceptance reads it before it checks the inviter's authority and writes only while it is
6
+ -- unchanged, so a reconcile between the check and the write makes the write change nothing. On Postgres the
7
+ -- acceptance holds the row `FOR SHARE` to its commit, and a reconcile's increment waits for it.
8
+ CREATE TABLE app_schema_revisions (
9
+ app TEXT PRIMARY KEY,
10
+ revision INTEGER NOT NULL
11
+ );
12
+
13
+ -- Every application IAM already holds a schema or a policy of starts at revision 1.
14
+ INSERT INTO app_schema_revisions (app, revision)
15
+ SELECT app, 1 FROM (SELECT app FROM roles UNION SELECT app FROM app_scopes UNION SELECT app FROM app_actions) known;
@@ -0,0 +1,9 @@
1
+ -- The revision of a principal's grants. Postgres dialect; `../migrations/0016_principal_grant_revision.sql`
2
+ -- states the same outcome.
3
+ --
4
+ -- Every write that removes a grant of a principal increments it, as the first statement of its transaction. An
5
+ -- invitation's acceptance reads the inviter's before it checks the inviter's live permissions and writes only while it
6
+ -- is unchanged, so a removal of ANY grant of the inviter between the check and the write makes the write change
7
+ -- nothing. On Postgres the increment takes the principal row first, so it waits for an acceptance holding the row
8
+ -- `FOR SHARE`. Every principal before this file starts at 0.
9
+ ALTER TABLE principals ADD COLUMN grant_revision INTEGER NOT NULL DEFAULT 0;
@@ -0,0 +1,12 @@
1
+ -- When each person last signed in to each application. Postgres dialect; `../migrations/0017_app_sign_ins.sql`
2
+ -- states the same outcome.
3
+ --
4
+ -- A sign-in to an application is the redemption of its handoff code: the child session it opens writes this row in
5
+ -- its own batch. IAM answers it, as `lastSignInAt`, only to a holder of `members.manage` (it is personal data, like an
6
+ -- address). It is not pruned with `auth_log`, and it goes with its principal. Empty for every sign-in before this file.
7
+ CREATE TABLE app_sign_ins (
8
+ app TEXT NOT NULL,
9
+ principal_id TEXT NOT NULL REFERENCES principals(id) ON DELETE CASCADE,
10
+ signed_in_at INTEGER NOT NULL,
11
+ PRIMARY KEY (app, principal_id)
12
+ );