@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,159 @@
1
+ import type { PrincipalAffiliation } from '@fabrika/auth-core';
2
+ import { type SqlDatabase, type SqlStatement } from '@fabrika/platform';
3
+ import { type AuditEventInput, AuditWriter, type SqlChange, type SqlDialect } from './audit';
4
+ export interface PrincipalRow {
5
+ id: string;
6
+ type: 'user' | 'service';
7
+ external_id: string | null;
8
+ /** THE NORMALIZED MAILBOX (`normalizeEmailIdentity`), never the display spelling — see `PrincipalRepository`. */
9
+ email: string | null;
10
+ /** Display text: the mailbox as the IdP or the inviting admin spelled it. */
11
+ label: string;
12
+ disabled_at: number | null;
13
+ activated_at: number | null;
14
+ created_at: number;
15
+ /** `external`: a person from outside the organization, who holds only roles declared external (ADR-0051). */
16
+ affiliation: PrincipalAffiliation;
17
+ /** Moved on by every removal of one of its grants (`grantRemovalStatement`); an acceptance writes only under the one it checked. */
18
+ grant_revision: number;
19
+ /**
20
+ * The language the person reads IAM in, as an accepted invitation named it (`InvitationOptions.locale`), or NULL for
21
+ * none: their application's then. Parsed at read; a value IAM no longer speaks counts as none.
22
+ */
23
+ locale: string | null;
24
+ }
25
+ /** Derived principal status — invited (not activated) → active → disabled. */
26
+ export type PrincipalStatus = 'invited' | 'active' | 'disabled';
27
+ export declare function principalStatus(row: PrincipalRow): PrincipalStatus;
28
+ /**
29
+ * IAM persistence capabilities over the `SqlDatabase` port — never a cloud-specific handle.
30
+ * Portable operations use prepared statements via `db.prepare(...).bind(...)`; a runtime composition
31
+ * root may replace one complete capability when its operation shape must differ by dialect.
32
+ *
33
+ * **ONE MAILBOX RULE.** `principals.email` holds the NORMALIZED mailbox and nothing else, so every
34
+ * comparison is plain equality and no query asks the engine to case-fold (SQLite's `LOWER()` is
35
+ * ASCII-only where Postgres's is full Unicode — the same input used to resolve differently per
36
+ * backend). This class is the single place that applies `normalizeEmailIdentity`: callers hand over
37
+ * whatever spelling they hold, the mailbox lands in `email` and the spelling in `label`.
38
+ */
39
+ /**
40
+ * On Postgres, the first statement of every transaction that writes more than one of a principal's rows among
41
+ * `principals`, `password_accounts`, `password_credentials`, `password_action_tokens` and `sessions`: it takes the
42
+ * principal row `FOR NO KEY UPDATE`, so every such writer waits at the same row before it locks anything else, and two
43
+ * of them never hold each other's rows. The writers: `promoteToInternal`, and `PasswordRepository`'s `upsertCredential`,
44
+ * `disableCredential`, `completeAction` and action-token issuance. A new one must start here too. `FOR NO KEY UPDATE`
45
+ * leaves a foreign-key check (an inserted session, token or grant) unblocked. `principalId` is SQL that yields the id,
46
+ * with its binds. SQLite and D1 run one writer at a time and need none.
47
+ */
48
+ export declare function principalLockStatements(db: SqlDatabase, dialect: SqlDialect, principalId: SqlChange): SqlStatement[];
49
+ export declare class PrincipalRepository {
50
+ protected readonly db: SqlDatabase;
51
+ protected readonly writer: AuditWriter;
52
+ constructor(db: SqlDatabase, writer?: AuditWriter);
53
+ getUserByExternalId(sub: string): Promise<PrincipalRow | null>;
54
+ getServiceByExternalId(commonName: string): Promise<PrincipalRow | null>;
55
+ getPrincipalById(id: string): Promise<PrincipalRow | null>;
56
+ /**
57
+ * Keep `locale` as the language the person reads IAM in. It picks words and nothing else, so it is written alone,
58
+ * outside any principal's lock order, and leaves no audit record: no grant, credential or session depends on it.
59
+ */
60
+ setLocale(id: string, locale: string): Promise<void>;
61
+ /** Resolve a user by mailbox. `email` is normalized here, so any spelling of it finds the same row. */
62
+ getUserByEmail(email: string): Promise<PrincipalRow | null>;
63
+ /**
64
+ * Refresh a returning user's mailbox/label if the token's email changed. No-op when unchanged.
65
+ *
66
+ * `IS DISTINCT FROM` is the null-safe `<>` — it must treat a NULL email as "different" so the
67
+ * first login of an invited row still fills it in. SQLite's `IS NOT <expr>` means the same
68
+ * thing but does not exist in Postgres (there `IS NOT` only takes NULL/TRUE/FALSE/UNKNOWN/
69
+ * DISTINCT FROM/DOCUMENT/JSON); `IS DISTINCT FROM` is standard and valid in both (SQLite ≥ 3.39).
70
+ *
71
+ * The reservation moves WITH the mailbox: leaving a stale claim behind would let the next invite
72
+ * of the new address create a second principal for the same human.
73
+ */
74
+ refreshUserLabel(id: string, email: string, audit?: AuditEventInput): Promise<void>;
75
+ /**
76
+ * Claim an invited row in one statement: bind `external_id = sub`, activate it, and set the
77
+ * display label, but only while still unclaimed (`external_id IS NULL`) to avoid a race claiming
78
+ * a row twice, and only an internal one: an IdP never claims an external principal (ADR-0051). The mailbox is untouched — it is what matched this row in the first place.
79
+ * Returns the claimed row, or null if the row was concurrently claimed / not found.
80
+ */
81
+ claimInvitedUser(id: string, sub: string, label: string, audit?: AuditEventInput): Promise<PrincipalRow | null>;
82
+ /** Lazy-create a user keyed by the verified `sub`, reserving its mailbox and writing `audit` in the same transaction. */
83
+ createUser(sub: string, email: string, audit?: (principalId: string) => AuditEventInput): Promise<PrincipalRow>;
84
+ /**
85
+ * Invite a user by mailbox (external_id NULL = unclaimed), reserving it and writing `audit` in the same transaction.
86
+ * An installation or a user administrator (ADR-0058) creates an `external` one; OIDC never claims it (ADR-0051).
87
+ */
88
+ inviteUser(email: string, label?: string, audit?: (principalId: string) => AuditEventInput, affiliation?: PrincipalAffiliation): Promise<PrincipalRow>;
89
+ /**
90
+ * Reserve one mailbox for one principal. `principal_email_claims` is a second lock over the same
91
+ * rule `idx_principals_uq_email` enforces, and it moves with a principal whose address changes —
92
+ * every user row that carries a mailbox carries a claim for it.
93
+ */
94
+ private claimMailboxStatement;
95
+ /** Run a principal INSERT + its mailbox reservation as one transaction and return the new row. */
96
+ private insertUser;
97
+ /**
98
+ * Create a native service (machine) principal. `external_id` is NULL — a native service is resolved
99
+ * through its `px_` credential (credential.principal_id → principal), never an external id. (The
100
+ * legacy CF service-token path still resolves existing services by their stored external_id.)
101
+ */
102
+ createService(label: string): Promise<PrincipalRow>;
103
+ /** The INSERT of a native service principal with a caller-generated id, for a batch that also writes its key. */
104
+ createServiceStatement(id: string, label: string): SqlStatement;
105
+ /**
106
+ * One page of principals, newest first.
107
+ *
108
+ * `status` is evaluated in SQL rather than by filtering a full table in memory. It is derived from
109
+ * exactly two columns (`disabled_at`, `activated_at` — see `principalStatus`), so expressing it
110
+ * here costs nothing and is what makes the page a real page: filtering after the read returned a
111
+ * short page from a long table and made `limit` mean nothing (SEC-22).
112
+ *
113
+ * Ordering is by `id DESC` alone. `created_at` is whole seconds, so rows written in the same second
114
+ * have no order of their own; the id is a monotonic UUIDv7, so it both orders and paginates
115
+ * (`before` is a keyset cursor that can neither skip nor repeat a row).
116
+ */
117
+ listPrincipals(filter: {
118
+ type?: 'user' | 'service';
119
+ status?: PrincipalStatus;
120
+ q?: string;
121
+ before?: string;
122
+ limit: number;
123
+ /** Leave out workload principals: a job's or a webhook's key is not an API key. */
124
+ withoutWorkloads?: boolean;
125
+ /** Leave out terminals (ADR-0067), which hold no key. */
126
+ withoutTerminals?: boolean;
127
+ }): Promise<PrincipalRow[]>;
128
+ /**
129
+ * The USER principals who can access `app` — every user holding at least one non-expired
130
+ * grant that applies to the app (the grant's `app` is NULL = cross-app, OR equals `app`).
131
+ * `DISTINCT` collapses the per-grant duplicates a principal with several grants produces.
132
+ * Services are excluded — this is the people directory (assignable users / actor labels),
133
+ * not machine principals. Disabled users ARE returned (the caller decides whether to hide
134
+ * them) so their labels still resolve. Ordered by label for a stable picker.
135
+ */
136
+ getPrincipalsForApp(app: string, options: {
137
+ includeCrossApp: boolean;
138
+ }): Promise<PrincipalRow[]>;
139
+ /** Disable a principal. Returns whether this call disabled it; `audit` is written only then. */
140
+ disablePrincipal(id: string, audit?: AuditEventInput): Promise<boolean>;
141
+ /** The disabling of a principal, as a change an audited revocation groups with others. */
142
+ disablePrincipalChange(id: string): SqlChange;
143
+ /** Enable a principal. Returns whether this call enabled it; `audit` is written only then. */
144
+ enablePrincipal(id: string, audit?: AuditEventInput): Promise<boolean>;
145
+ /**
146
+ * Make an external principal internal (ADR-0051), an installation administrator's operation, and in the same
147
+ * transaction revoke every session it holds and spend every password action still pending: they were issued to an
148
+ * external person, and promotion can widen what they reach (a bootstrap mailbox becomes an installation admin).
149
+ * Returns whether this call promoted it; `audit` is written only then. There is no demotion.
150
+ *
151
+ * On Postgres the principal row is locked first (`principalLockStatements`), as a password action's completion does,
152
+ * because the three changes are one statement whose parts lock rows in no order of their own. The revocations test
153
+ * the affiliation themselves, so on SQLite, where they run in order, they come before the promotion.
154
+ */
155
+ promoteToInternal(id: string, audit?: AuditEventInput): Promise<boolean>;
156
+ private guardedUpdate;
157
+ /** Hard-delete a principal (cancels an unclaimed invite; cascades grants). */
158
+ deletePrincipal(id: string): Promise<boolean>;
159
+ }
@@ -0,0 +1,150 @@
1
+ import { type SqlDatabase, type SqlStatement } from '@fabrika/platform';
2
+ import { type AuditEventInput, AuditWriter, type AuthLogInput, type SqlGuard } from './audit';
3
+ /** The logout row of a session: its principal comes from the session itself. */
4
+ export type LogoutLogInput = Pick<AuthLogInput, 'requestId' | 'app' | 'reason'>;
5
+ /**
6
+ * How a session's holder proved who they were. `local_dev` is the `LOCAL_DEV_LOGIN` bypass — it has a
7
+ * value of its own so `sessionUsable` can ask whether the method is still enabled with no special case.
8
+ * `terminal_assertion` is an application's word about who stands at an enrolled terminal (ADR-0067).
9
+ */
10
+ export type AuthenticationMethod = 'oidc' | 'password' | 'local_dev' | 'terminal_assertion';
11
+ /** A browser session row (the `sessions` table). Only the cookie's hash is stored, never plaintext. */
12
+ export interface SessionRow {
13
+ id: string;
14
+ token_hash: string;
15
+ principal_id: string;
16
+ idp_sub: string | null;
17
+ email: string | null;
18
+ authentication_method: AuthenticationMethod;
19
+ created_at: number;
20
+ expires_at: number;
21
+ revoked_at: number | null;
22
+ /** NULL = an IAM session. Non-NULL = an app-bound child session (ADR-0022) that mints only for it. */
23
+ app: string | null;
24
+ /** The IAM session this was derived from; revoking that one revokes this. NULL on an IAM session. */
25
+ parent_session_id: string | null;
26
+ /** The terminal this login was asserted on (ADR-0067); set exactly when the method is `terminal_assertion`. */
27
+ terminal_id: string | null;
28
+ /** The application that asserted the person on `terminal_id`; set exactly when `terminal_id` is. */
29
+ asserted_by: string | null;
30
+ /** The terminal's generation the login was asserted in; the session is valid only while it is still the terminal's. */
31
+ terminal_generation: number | null;
32
+ }
33
+ export interface ListedSessionRow extends SessionRow {
34
+ parent_expires_at: number | null;
35
+ parent_revoked_at: number | null;
36
+ }
37
+ /** The terminal a `terminal_assertion` session was asserted on, and the application that asserted it. */
38
+ export interface SessionTerminal {
39
+ terminalId: string;
40
+ assertedBy: string;
41
+ }
42
+ /** A session row's terminal, or null for a session on none. The row CHECK sets both columns or neither. */
43
+ export declare function sessionTerminal(row: Pick<SessionRow, 'terminal_id' | 'asserted_by'>): SessionTerminal | null;
44
+ /** A session to create. */
45
+ export interface NewSession {
46
+ tokenHash: string;
47
+ principalId: string;
48
+ idpSub?: string | null;
49
+ email?: string | null;
50
+ authenticationMethod?: AuthenticationMethod;
51
+ expiresAt: number;
52
+ /** Bind this session to ONE app (ADR-0022). Omitted = an IAM session, usable for any app. */
53
+ app?: string | null;
54
+ /** The IAM session this is derived from. Revoking that one revokes this. */
55
+ parentSessionId?: string | null;
56
+ /** A `terminal_assertion` session's terminal and asserting application; both or neither. */
57
+ terminal?: SessionTerminal | null;
58
+ /** The terminal's generation the login was asserted in, which a child copies; required with `terminal`. */
59
+ terminalGeneration?: number | null;
60
+ }
61
+ export declare class SessionRepository {
62
+ protected readonly db: SqlDatabase;
63
+ protected readonly writer: AuditWriter;
64
+ constructor(db: SqlDatabase, writer?: AuditWriter);
65
+ /**
66
+ * Create a session. Only the hash of the opaque cookie value is stored; returns the new id. A login passes its
67
+ * `auth_log` row, written in the same batch: a session nobody logged is never opened. A child session is a sign-in to
68
+ * its app, which the batch records as the principal's last one there (`app_sign_ins`).
69
+ */
70
+ createSession(input: NewSession, log?: AuthLogInput): Promise<string>;
71
+ /**
72
+ * The statements that create session `id` with its `auth_log` row, for a use case that composes them into its own
73
+ * batch. With `guard`, they write only when the guard finds its row: after a change earlier in that batch that must
74
+ * have happened first.
75
+ */
76
+ createSessionStatements(id: string, input: NewSession, log?: AuthLogInput, guard?: SqlGuard): SqlStatement[];
77
+ /**
78
+ * Look up a session by the cookie's hash, but ONLY while still valid (not revoked, not expired).
79
+ * An invalid/absent session returns null — the caller re-authenticates. Single-statement so the
80
+ * validity check and the read can't race a concurrent revoke.
81
+ *
82
+ * A CHILD session is valid only while its parent is: the join is what makes "log out of IAM" reach
83
+ * app sessions on hosts IAM cannot set a cookie for. `ON DELETE CASCADE` does not cover this —
84
+ * revocation writes `revoked_at`, it does not delete the row.
85
+ *
86
+ * A session on a terminal is valid only while that terminal is enrolled, and so is a child whose parent is on one: a
87
+ * revoked terminal ends them at once, with no sweep (ADR-0067).
88
+ */
89
+ getActiveSessionByHash(tokenHash: string): Promise<SessionRow | null>;
90
+ /**
91
+ * The same validity rule as `getActiveSessionByHash`, addressed by id — how a handoff code reaches
92
+ * the login it was issued against, which it holds by id rather than by a hash it must not store.
93
+ */
94
+ getActiveSessionById(id: string): Promise<SessionRow | null>;
95
+ /**
96
+ * Whether the IAM login `parentSessionId` holds a live session bound to `app` — whether this login already chose that
97
+ * application. The parent's own validity is the caller's: it reads the login it asks about first.
98
+ */
99
+ hasActiveChild(parentSessionId: string, app: string): Promise<boolean>;
100
+ /**
101
+ * Revoke a session by the cookie's hash (logout). Idempotent — already-revoked → false. A logout passes its
102
+ * `auth_log` row, written in the same transaction and only for the session this call ended, with that session's
103
+ * principal: one statement on Postgres, `changes()` right after the revocation on SQLite (see `AuditWriter`).
104
+ */
105
+ revokeSessionByHash(tokenHash: string, log?: LogoutLogInput): Promise<boolean>;
106
+ /**
107
+ * A session row by the cookie's hash, VALID OR NOT: only for the audit row of a refused use, which names the session's
108
+ * principal and terminal. Nothing may authorize from it; `getActiveSessionByHash` is the read that does.
109
+ */
110
+ getSessionByHashForAudit(tokenHash: string): Promise<SessionRow | null>;
111
+ /**
112
+ * A session row by id, VALID OR NOT — the admin read, unlike `getActiveSessionById`. An operator
113
+ * revoking a session must be able to address one this instance would already refuse, and be told
114
+ * "already revoked" rather than "no such session".
115
+ */
116
+ getSessionById(id: string): Promise<SessionRow | null>;
117
+ /**
118
+ * One page of a principal's sessions, newest first, valid or not. `before` is a keyset cursor over
119
+ * the UUIDv7 id, which is monotonic with creation — the same paging every other admin list uses.
120
+ */
121
+ listSessionsForPrincipal(principalId: string, options: {
122
+ before?: string;
123
+ limit: number;
124
+ signInsOnly?: boolean;
125
+ }): Promise<ListedSessionRow[]>;
126
+ listChildren(parentIds: string[]): Promise<ListedSessionRow[]>;
127
+ lastAppSignIns(principalId: string): Promise<{
128
+ app: string;
129
+ signed_in_at: number;
130
+ }[]>;
131
+ /**
132
+ * Revoke ONE session by id. Idempotent — already-revoked → false.
133
+ *
134
+ * **Revoking an IAM session revokes every app session derived from it**, with no sweep: a child
135
+ * carries `parent_session_id`, and `getActiveSessionByHash`/`ById` join to the parent on every
136
+ * lookup, so the child stops resolving the moment the parent's `revoked_at` is set (ADR-0022).
137
+ */
138
+ revokeSessionById(id: string, audit?: AuditEventInput): Promise<boolean>;
139
+ /** The revocation of one session by id, for a use case that composes it into its own batch with the session's log row. */
140
+ revokeStatement(id: string): SqlStatement;
141
+ /** How many unrevoked sessions a principal holds — the count an operator's "revoke all" records. */
142
+ countUnrevokedSessions(principalId: string): Promise<number>;
143
+ /**
144
+ * Revoke every live session a principal holds. Returns how many rows this call revoked; `audit` is written only when
145
+ * that is at least one.
146
+ */
147
+ revokeSessionsForPrincipal(principalId: string, audit?: AuditEventInput): Promise<number>;
148
+ /** Prune expired or revoked sessions (cron). Returns the number removed. */
149
+ pruneSessions(now: number): Promise<number>;
150
+ }
@@ -0,0 +1,13 @@
1
+ import { type SqlStatement } from '@fabrika/platform';
2
+ /**
3
+ * Run a statement that always returns exactly one row (an `INSERT/UPDATE … RETURNING`
4
+ * we know matched). `.first<T>()` is typed `T | null`; this narrows it to `T`,
5
+ * throwing if the row is unexpectedly absent (a programming/DB error, not normal flow).
6
+ */
7
+ export declare function firstRow<T>(statement: SqlStatement): Promise<T>;
8
+ /**
9
+ * The current time in unix seconds — the unit every `*_at` column stores. Timestamps are
10
+ * generated HERE, never in SQL: `unixepoch()` is SQLite-only, so a statement carrying it
11
+ * cannot run on Postgres (see @fabrika/platform's `sql.ts`).
12
+ */
13
+ export declare function unixNow(): number;
@@ -0,0 +1,144 @@
1
+ import { type SqlDatabase, type SqlStatement } from '@fabrika/platform';
2
+ import { type AuditEventInput, AuditWriter, type AuthLogInput, type SqlGuard } from './audit';
3
+ import type { LogoutLogInput } from './sessions';
4
+ /** How long a person's session on a terminal lasts unless its profile says otherwise (ADR-0067). */
5
+ export declare const DEFAULT_TERMINAL_SESSION_TTL_SECONDS = 300;
6
+ /** The longest session a terminal's profile may set; the `terminals.session_ttl` CHECK states the same bound. */
7
+ export declare const MAX_TERMINAL_SESSION_TTL_SECONDS = 3600;
8
+ /** An enrolled browser (the `terminals` table), the side table of its `service` principal. */
9
+ export interface TerminalRow {
10
+ principal_id: string;
11
+ label: string;
12
+ /** The administrator who enrolled it; null once that principal is gone. */
13
+ enrolled_by: string | null;
14
+ /** SHA-256 of the `__Host-px_terminal` cookie value. */
15
+ token_hash: string;
16
+ session_ttl: number;
17
+ created_at: number;
18
+ last_seen_at: number | null;
19
+ revoked_at: number | null;
20
+ /** Incremented by every release; the sessions and identifications of an earlier generation are over. */
21
+ generation: number;
22
+ }
23
+ /** An application that may assert a person on a terminal, with its ceiling of action patterns. */
24
+ export interface TerminalAsserter {
25
+ app: string;
26
+ ceiling: string[];
27
+ }
28
+ /** What a terminal is for: its name, who may assert on it and how long a session on it lasts. */
29
+ export interface TerminalProfile {
30
+ label: string;
31
+ asserters: TerminalAsserter[];
32
+ sessionTtl: number;
33
+ }
34
+ /** One identification IAM opened for a terminal's browser (the `terminal_identifications` table). */
35
+ export interface TerminalIdentificationRow {
36
+ id_hash: string;
37
+ terminal_id: string;
38
+ app: string;
39
+ return_url: string;
40
+ handoff_state: string;
41
+ handoff_challenge: string;
42
+ created_at: number;
43
+ expires_at: number;
44
+ consumed_at: number | null;
45
+ /** The login an assertion opened for the person; the terminal's browser completes the identification with it. */
46
+ session_id: string | null;
47
+ /** When the terminal's browser completed the identification and IAM issued the handoff code. */
48
+ completed_at: number | null;
49
+ /** The terminal's generation the identification was opened in; it is over once the terminal's moves on. */
50
+ generation: number;
51
+ }
52
+ /**
53
+ * Terminals and their identifications (ADR-0067). A terminal is a `service` principal plus its row here; enrolment
54
+ * composes `PrincipalRepository.createServiceStatement` with `enrolStatement` in one batch.
55
+ */
56
+ export declare class TerminalRepository {
57
+ protected readonly db: SqlDatabase;
58
+ protected readonly writer: AuditWriter;
59
+ constructor(db: SqlDatabase, writer?: AuditWriter);
60
+ /** The INSERT of a terminal beside its service principal `principalId`, which the same batch creates first. */
61
+ enrolStatement(input: {
62
+ principalId: string;
63
+ label: string;
64
+ enrolledBy: string;
65
+ tokenHash: string;
66
+ sessionTtl: number;
67
+ }): SqlStatement;
68
+ /** The enrolled terminal whose cookie hashes to `tokenHash`; null when there is none or it was revoked. */
69
+ getActiveByTokenHash(tokenHash: string): Promise<TerminalRow | null>;
70
+ /** A terminal by its principal id, revoked or not — the admin read. */
71
+ getById(terminalId: string): Promise<TerminalRow | null>;
72
+ /** One page of terminals, newest first, revoked or not. `before` is a keyset cursor over the UUIDv7 principal id. */
73
+ list(options: {
74
+ before?: string;
75
+ limit: number;
76
+ }): Promise<TerminalRow[]>;
77
+ /** The applications that may assert on the terminal, each with its ceiling. A malformed ceiling reads as empty. */
78
+ listAsserters(terminalId: string): Promise<TerminalAsserter[]>;
79
+ /** The asserters of every terminal in `terminalIds`, in one query — the fan-out of one admin page. */
80
+ listAssertersFor(terminalIds: readonly string[]): Promise<Map<string, TerminalAsserter[]>>;
81
+ /** `app`'s entry on the terminal, or null when `app` may not assert there. */
82
+ getAsserter(terminalId: string, app: string): Promise<TerminalAsserter | null>;
83
+ /**
84
+ * Replace an enrolled terminal's profile in one batch: its label (on its principal too, which audit rows name), its
85
+ * session TTL and its whole set of asserters. Answers false, and changes nothing, when the terminal is unknown or
86
+ * revoked; `audit` is written only when it changed.
87
+ */
88
+ setProfile(terminalId: string, profile: TerminalProfile, audit?: AuditEventInput): Promise<boolean>;
89
+ /**
90
+ * Revoke a terminal, every session issued on it and every identification still pending on it, in one transaction. Idempotent: answers false, and writes no
91
+ * `audit`, when the terminal was already revoked or is unknown. The validity join ends its sessions either way; the
92
+ * sessions are revoked too so an operator sees them ended.
93
+ */
94
+ revoke(terminalId: string, audit?: AuditEventInput): Promise<boolean>;
95
+ /**
96
+ * Release the terminal, as its sign-out does (ADR-0067 §5), in one transaction: move it to its next generation, which
97
+ * ends every session and identification of the current one at once, however an assertion in flight interleaves; then
98
+ * end its pending identifications and revoke its live sessions, so an operator sees them ended. Answers how many
99
+ * sessions. A sign-out passes `log`: each login this call ends gets its `logout` row, with the login's principal,
100
+ * terminal and asserter, like `SessionRepository.revokeSessionByHash`. Its children end with it through the join.
101
+ */
102
+ release(terminalId: string, log?: LogoutLogInput): Promise<number>;
103
+ /**
104
+ * Open an identification and record that its terminal was seen now, in one batch with `log`, the identification's
105
+ * `auth_log` row. Only the hash of its id is stored; the plaintext lives in one redirect.
106
+ */
107
+ createIdentification(input: {
108
+ idHash: string;
109
+ terminalId: string;
110
+ app: string;
111
+ returnUrl: string;
112
+ handoffState: string;
113
+ handoffChallenge: string;
114
+ expiresAt: number;
115
+ }, log?: AuthLogInput): Promise<void>;
116
+ /** An identification by its id's hash, in any state — the caller judges expiry and consumption. */
117
+ getIdentification(idHash: string): Promise<TerminalIdentificationRow | null>;
118
+ /**
119
+ * Consume an identification for the login `sessionId`, atomically and at most once, and give its completion until
120
+ * `completeBy`. `login` is the batch that opens that login, written only when this call consumed the identification
121
+ * (see `consumedFor`), so a failed write leaves the identification open and a refused consumption opens nothing.
122
+ * Answers the row this call consumed, or null for an unknown, expired, cancelled or already consumed one.
123
+ */
124
+ consumeIdentification(consumption: {
125
+ idHash: string;
126
+ sessionId: string;
127
+ now: number;
128
+ completeBy: number;
129
+ }, login?: readonly SqlStatement[]): Promise<TerminalIdentificationRow | null>;
130
+ /** The guard of the statements after `consumeIdentification` in its batch: the identification is consumed for `sessionId`. */
131
+ consumedFor(idHash: string, sessionId: string): SqlGuard;
132
+ /**
133
+ * Complete an asserted identification of `terminalId`, atomically and at most once: the terminal's own browser asks,
134
+ * before the completion window closes. Null for any other identification, a second completion included.
135
+ */
136
+ completeIdentification(idHash: string, terminalId: string, now: number): Promise<TerminalIdentificationRow | null>;
137
+ /** End an open identification of `app` that the person abandoned. Answers false when there was none to end. */
138
+ cancelIdentification(idHash: string, app: string): Promise<boolean>;
139
+ /**
140
+ * Prune expired, cancelled and completed identifications (cron). An asserted one waits for its completion until it
141
+ * expires. Returns the number removed.
142
+ */
143
+ pruneIdentifications(now: number): Promise<number>;
144
+ }
@@ -0,0 +1,61 @@
1
+ import { type WorkloadKind } from '@fabrika/auth-core';
2
+ import { type SqlDatabase } from '@fabrika/platform';
3
+ import { type AuditEventInput } from './audit';
4
+ import type { PrincipalRow } from './principals';
5
+ /** A workload's identity (the `workload_principals` table). */
6
+ export interface WorkloadPrincipalRow {
7
+ principal_id: string;
8
+ app: string;
9
+ kind: WorkloadKind;
10
+ name: string;
11
+ }
12
+ /**
13
+ * One workload a schema reconcile declared (the `app_workloads` table). `actions` is a JSON array of exact actions;
14
+ * `issues` the link mount a job may issue links of, or null.
15
+ */
16
+ export interface AppWorkloadRow {
17
+ app: string;
18
+ kind: WorkloadKind;
19
+ name: string;
20
+ actions: string;
21
+ issues: string | null;
22
+ }
23
+ /** What the app's schema declares for one workload now. */
24
+ export interface WorkloadDeclaration {
25
+ actions: string[];
26
+ issues: string | null;
27
+ }
28
+ /**
29
+ * Workload principals and their declared actions (ADR-0022 "Principal kinds"). Issuing a key creates the principal;
30
+ * the schema reconcile declares what it may do (`AppSchemaRepository.reconcileAppSchema` writes `app_workloads`).
31
+ */
32
+ export declare class WorkloadRepository {
33
+ protected readonly db: SqlDatabase;
34
+ constructor(db: SqlDatabase);
35
+ /**
36
+ * Issue one key of a workload in ONE transaction: the principal and its identity on first use, the revocation of every
37
+ * other live key except `keep`, the key, and `audit` about the principal. The principal is addressed by its external
38
+ * id throughout, and both identity inserts do nothing on a conflict, so two concurrent first issuances end with one
39
+ * principal that holds both keys' history. Answers the principal's id.
40
+ */
41
+ issueKey(input: {
42
+ app: string;
43
+ kind: WorkloadKind;
44
+ name: string;
45
+ credentialId: string;
46
+ tokenHash: string;
47
+ issuedBy: string;
48
+ expiresAt: number;
49
+ keep: string | null;
50
+ }, audit: AuditEventInput): Promise<string>;
51
+ /** The workload's principal, or null when no key was ever issued for it. */
52
+ findPrincipal(app: string, kind: WorkloadKind, name: string): Promise<PrincipalRow | null>;
53
+ private identityStatements;
54
+ /** The workload's principal id, or null when no key was ever issued for it. */
55
+ findPrincipalId(app: string, kind: WorkloadKind, name: string): Promise<string | null>;
56
+ getByPrincipal(principalId: string): Promise<WorkloadPrincipalRow | null>;
57
+ /** The workload's declared actions, or null when the app's schema declares no such workload. */
58
+ declaredActions(app: string, kind: WorkloadKind, name: string): Promise<string[] | null>;
59
+ /** The workload's declaration, or null when the app's schema declares no such workload. */
60
+ declaration(app: string, kind: WorkloadKind, name: string): Promise<WorkloadDeclaration | null>;
61
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The correlation id written to `auth_log` and `audit_events`, and the rule about where it may come from.
3
+ *
4
+ * Same shape as `src/client-address.ts`: IAM does not choose a header. The COMPOSITION ROOT names the
5
+ * one header its own ingress overwrites, and shared code reads that one and nothing else. Naming none
6
+ * is supported and means every request gets a generated id.
7
+ *
8
+ * - `src/index.ts` (Cloudflare) — `cf-ray`. The Worker holds its own Custom Domain, so no proxy hop
9
+ * strips a caller's `X-Request-Id`; only the edge-written ray id is outside the caller's reach.
10
+ * - `src/node/server.ts` (Bun/Zerops) — `X-Request-Id`, which the Caddy proxy deletes at ingress and
11
+ * re-injects. Once the installation's proxy secret is configured, a request without it gets a
12
+ * generated id (`src/proxy-proof.ts`).
13
+ *
14
+ * Even a trusted header is bounded: an audit column never takes an unbounded or free-form string. The
15
+ * `IamRpc` surface (`src/rpc.ts`) holds the `requestId` its callers pass to the same rule.
16
+ */
17
+ /** A fresh id, for a composition that names no header or a request whose value was refused. */
18
+ export declare function generateRequestId(): string;
19
+ /** Read the id from the named header, or generate one when the header is unnamed, absent, or invalid. */
20
+ export declare function readRequestId(request: Request, header: string | undefined, generate?: () => string): string;
21
+ /**
22
+ * Keep a correlation id that is within the length cap and the allowlist, or replace it with a generated
23
+ * one. Never truncated: a cut id could collide with another request's trail.
24
+ */
25
+ export declare function acceptRequestId(value: string | null, generate?: () => string): string;