@fabrika/iam 0.0.1 → 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 +69 -8
  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,177 @@
1
+ import { type SqlDatabase, type SqlStatement } from '@fabrika/platform';
2
+ import { type AuditEventInput, AuditWriter, type SqlChange, type SqlDialect } from './audit';
3
+ /** A grant written for a workload principal: its authority is its declared actions, never a grant. */
4
+ export declare class WorkloadGrantError extends Error {
5
+ readonly name = "WorkloadGrantError";
6
+ constructor();
7
+ }
8
+ /** A grant an external principal may not hold: its authority is only roles its application declares external. */
9
+ export declare class ExternalGrantError extends Error {
10
+ readonly name = "ExternalGrantError";
11
+ constructor();
12
+ }
13
+ /**
14
+ * The condition every grant write carries (ADR-0051): the principal is internal, or the grant names a role of its app
15
+ * declared external. Binds the principal id, the app and the role key; a null app or role key never matches a role, so
16
+ * an inline, cross-app or built-in grant is refused for an external principal.
17
+ *
18
+ * On Postgres the role row is read `FOR SHARE`, and held to the end of the write's transaction. A reconcile deletes and
19
+ * rewrites every role row of the app, so it waits for a grant write already holding the row, and its batch guard then
20
+ * sees the committed grant; a grant write that comes second waits for the reconcile, and the lock's re-check of the
21
+ * latest row version (the old row is gone) makes it write nothing. SQLite and D1 run one writer at a time.
22
+ */
23
+ export declare function eligibleGrantSql(dialect: SqlDialect): string;
24
+ /**
25
+ * The first statement of every transaction that removes a grant of a principal: it moves the principal's grant revision
26
+ * on and, on Postgres, takes the principal row first, as every writer of a principal's rows starts. An acceptance
27
+ * writes only while its inviter's revision is the one read before its check of the inviter's live permissions
28
+ * (`InviterCheck`), so a removal of any of its grants between the check and the write is seen.
29
+ */
30
+ export declare function grantRemovalStatement(db: SqlDatabase, target: {
31
+ principalId: string;
32
+ } | {
33
+ grantId: string;
34
+ }): SqlStatement;
35
+ export declare function eligibleGrantBinds(input: {
36
+ principalId: string;
37
+ app?: string | null;
38
+ roleKey?: string | null;
39
+ }): (string | null)[];
40
+ /**
41
+ * What authorized a delegated grant change (ADR-0022 rule 6), read BEFORE its check: the administrator's grant revision
42
+ * (`principals.grant_revision`) and the app's schema revision (`AppSchemaRepository.getSchemaRevision`). The change
43
+ * writes only while the administrator is enabled and both are unchanged, so the grants and declarations the check read
44
+ * are the ones in force at the write.
45
+ */
46
+ export interface DelegatedAuthority {
47
+ readonly administratorId: string;
48
+ readonly app: string;
49
+ readonly grantRevision: number;
50
+ readonly schemaRevision: number;
51
+ }
52
+ export interface GrantRow {
53
+ id: string;
54
+ principal_id: string;
55
+ /** Registered app id this grant applies to; NULL = all apps (cross-app). */
56
+ app: string | null;
57
+ /** Named role/policy key; XOR `permissions`. Exactly one is non-null. */
58
+ role_key: string | null;
59
+ /** Inline action-pattern set (JSON array); XOR `role_key`. Exactly one is non-null. */
60
+ permissions: string | null;
61
+ /** Scope dimension; NULL = global (both scope columns rise/fall together). */
62
+ scope_type: string | null;
63
+ /** Opaque, app-owned scope value; NULL = global. */
64
+ scope_value: string | null;
65
+ granted_by: string | null;
66
+ expires_at: number | null;
67
+ created_at: number;
68
+ }
69
+ export interface CreateGrantInput {
70
+ principalId: string;
71
+ app?: string | null;
72
+ roleKey?: string | null;
73
+ permissions?: string[] | null;
74
+ scopeType?: string | null;
75
+ scopeValue?: string | null;
76
+ grantedBy?: string | null;
77
+ expiresAt?: number | null;
78
+ }
79
+ export declare class GrantRepository {
80
+ protected readonly db: SqlDatabase;
81
+ protected readonly writer: AuditWriter;
82
+ constructor(db: SqlDatabase, writer?: AuditWriter);
83
+ /** Active (non-expired) grants for a principal, ALL apps (admin/effective view). */
84
+ getActiveGrants(principalId: string): Promise<GrantRow[]>;
85
+ /**
86
+ * Active grants that apply to the calling `app` — the authz path. A grant counts when its `app`
87
+ * equals the verified calling app, and a cross-app (NULL) grant counts only when `includeCrossApp`
88
+ * says so: `resolvePermissionsAt` decides that, and it is true for platform audiences only.
89
+ */
90
+ getActiveGrantsForApp(principalId: string, app: string, options: {
91
+ includeCrossApp: boolean;
92
+ }): Promise<GrantRow[]>;
93
+ /** Grants of `roleKey` at `app` held by an external principal. */
94
+ listExternalHoldersOfRole(app: string, roleKey: string): Promise<GrantRow[]>;
95
+ /** Grants of `roleKey` at `app` that are not in a value of `scopeType` — global ones included. */
96
+ listRoleGrantsOutsideScope(app: string, roleKey: string, scopeType: string): Promise<GrantRow[]>;
97
+ /** All grants for a principal (admin view, incl. expired). */
98
+ listGrants(principalId: string): Promise<GrantRow[]>;
99
+ /**
100
+ * All grants for a PAGE of principals, in one statement — the api-keys list used to run one query
101
+ * per row over an unbounded roster (SEC-22). The page is bounded, so the `IN` list is too.
102
+ */
103
+ listGrantsFor(principalIds: readonly string[]): Promise<GrantRow[]>;
104
+ getGrantById(id: string): Promise<GrantRow | null>;
105
+ /**
106
+ * Create a grant. EITHER `roleKey` (named role/policy) OR `permissions` (an inline
107
+ * action-pattern set, JSON-encoded here) — the caller MUST supply exactly one; the
108
+ * DB CHECK rejects both/neither. `scopeType`/`scopeValue` are both-or-neither (both
109
+ * null = global). The handler validates which is set; this layer just persists it.
110
+ * `audit` is the change's record, written in the same batch. Throws `ExternalGrantError`, writing
111
+ * nothing, for a grant the principal may not hold (ADR-0051).
112
+ */
113
+ createGrant(input: CreateGrantInput, audit?: (grantId: string) => AuditEventInput): Promise<GrantRow>;
114
+ /** Whether `principalId` may hold this grant: it is internal, or the role is one its app declares external. */
115
+ isEligibleGrant(input: {
116
+ principalId: string;
117
+ app?: string | null;
118
+ roleKey?: string | null;
119
+ }): Promise<boolean>;
120
+ /** Whether `principalId` is a job's, a webhook's or a connector's principal, which holds no grant. */
121
+ isWorkloadPrincipal(principalId: string): Promise<boolean>;
122
+ /** A principal is a workload from the batch that creates it, so this check cannot race a later change. */
123
+ private refuseWorkload;
124
+ /**
125
+ * Create a ROLE grant, or answer null when the principal already holds it. The conflict target is
126
+ * the one unique index this grant falls under (`idx_grants_uq_scoped` / `idx_grants_uq_global`), so
127
+ * the statement itself decides on both engines and any other failure still throws. A grant the
128
+ * principal may not hold writes nothing and throws `ExternalGrantError` (ADR-0051).
129
+ */
130
+ createRoleGrantUnlessHeld(input: CreateGrantInput & {
131
+ roleKey: string;
132
+ permissions?: null;
133
+ }, audit?: (grantId: string) => AuditEventInput): Promise<GrantRow | null>;
134
+ /**
135
+ * `createRoleGrantUnlessHeld` of a global role under a delegated `authority`, renewing an expired one in place with the
136
+ * new expiry: null when the principal holds it unexpired or the authority changed since its check
137
+ * (`delegatedAuthorityHolds` tells them apart). Throws `ExternalGrantError` for a grant the principal may not hold.
138
+ */
139
+ createDelegatedRoleGrant(input: {
140
+ principalId: string;
141
+ roleKey: string;
142
+ grantedBy: string;
143
+ expiresAt?: number | null;
144
+ }, authority: DelegatedAuthority, audit: (grantId: string) => AuditEventInput): Promise<GrantRow | null>;
145
+ /**
146
+ * Delete a grant under a delegated `authority`, after its principal's revision (`grantRemovalStatement`); false when
147
+ * it is gone or the authority changed since its check. A removal of the administrator's own grant moves the revision
148
+ * the guard reads, so the guard expects it one higher.
149
+ */
150
+ deleteDelegatedGrant(grant: Pick<GrantRow, 'id' | 'principal_id'>, authority: DelegatedAuthority, audit: AuditEventInput): Promise<boolean>;
151
+ /** Whether `authority` still holds: the administrator enabled and both revisions the ones its check read. */
152
+ delegatedAuthorityHolds(authority: DelegatedAuthority): Promise<boolean>;
153
+ /**
154
+ * On Postgres a delegated change takes the principal rows first, as every writer of a principal's rows starts: the
155
+ * administrator's `FOR SHARE` (a disable or a grant removal waits, or the change reads the committed one), and a
156
+ * removed grant's principal by its revision's increment, which takes the administrator's own row when the grant is
157
+ * theirs. Then the app's revision row `FOR SHARE`: a reconcile's increment, its first statement, waits, or the change
158
+ * reads the new revision. SQLite and D1 run one writer at a time and need no lock.
159
+ */
160
+ private administratorLock;
161
+ private revisionLock;
162
+ private delegatedGuard;
163
+ /** The INSERT of one grant with a caller-generated id, for a batch that also writes its principal and record. */
164
+ grantStatement(id: string, input: CreateGrantInput, onConflict?: string): SqlStatement;
165
+ private grantChange;
166
+ /** Keep the audit's before-value true if another administrator edited this grant after it was read. */
167
+ updateGrant(grant: GrantRow, input: CreateGrantInput, audit: AuditEventInput): Promise<GrantRow | null>;
168
+ /** Delete a grant, after its principal's revision. */
169
+ deleteGrant(id: string, audit?: AuditEventInput): Promise<boolean>;
170
+ /** Delete all grants for a principal (revocation), after its revision (`grantRemovalStatement`). */
171
+ deleteGrantsForPrincipal(principalId: string): Promise<void>;
172
+ /**
173
+ * Every grant of a principal, as a change an audited revocation groups with others. It moves no grant revision, so
174
+ * the group must disable the principal in the same transaction, which an acceptance sees as surely (`InviterCheck`).
175
+ */
176
+ deleteGrantsForPrincipalChange(principalId: string): SqlChange;
177
+ }
@@ -0,0 +1,52 @@
1
+ import { type SqlDatabase } from '@fabrika/platform';
2
+ import { type AuditEventInput } from './audit';
3
+ /** A single-use cross-host handoff code (`auth_codes`). Only its hash is stored. */
4
+ export interface AuthCodeRow {
5
+ id: string;
6
+ code_hash: string;
7
+ app: string;
8
+ parent_session_id: string;
9
+ return_url: string;
10
+ expires_at: number;
11
+ consumed_at: number | null;
12
+ created_at: number;
13
+ }
14
+ export declare class HandoffRepository {
15
+ protected readonly db: SqlDatabase;
16
+ constructor(db: SqlDatabase);
17
+ /** Origins IAM will send a browser back to for this app. Empty = the app can receive no handoff. */
18
+ listReturnOrigins(app: string): Promise<string[]>;
19
+ /**
20
+ * Replace an app's registered return origins. Declarative on purpose: the caller states the whole
21
+ * set, so removing an origin is the same operation as adding one and a stale entry cannot survive
22
+ * a reconcile. Values are stored exactly as given — `normalizeOrigin` is the one place that decides
23
+ * what canonical means, and it runs before this.
24
+ *
25
+ * ONE batch, like `reconcileAppSchema`: a failure between the DELETE and the INSERTs would leave
26
+ * the app with no registered origin, which is a hard refusal on every cross-host sign-in.
27
+ */
28
+ setReturnOrigins(app: string, origins: readonly string[], audit?: AuditEventInput): Promise<void>;
29
+ /** Issue a single-use code. Only the hash is stored; the plaintext lives in one redirect. */
30
+ issueCode(input: {
31
+ codeHash: string;
32
+ app: string;
33
+ parentSessionId: string;
34
+ returnUrl: string;
35
+ expiresAt: number;
36
+ }): Promise<string>;
37
+ /**
38
+ * Consume a code, atomically and at most once.
39
+ *
40
+ * The UPDATE is the guard, not the SELECT: a second redemption of the same code changes no rows
41
+ * and gets null, with no window in which two callers both believe they won. The row is read only
42
+ * after it has been marked, so what comes back is what this caller consumed.
43
+ *
44
+ * Returns null for unknown, expired and already-consumed alike — the caller maps those to distinct
45
+ * reasons by looking the code up separately, because none of them is worth a second write here.
46
+ */
47
+ consumeCode(codeHash: string, now: number): Promise<AuthCodeRow | null>;
48
+ /** Whether a code exists at all — only to tell `expired_code` from `invalid_code` after a miss. */
49
+ findCode(codeHash: string): Promise<AuthCodeRow | null>;
50
+ /** Prune spent and expired codes (cron). Returns the number removed. */
51
+ pruneCodes(olderThanSeconds: number): Promise<number>;
52
+ }
@@ -0,0 +1,31 @@
1
+ import { type SqlDatabase } from '@fabrika/platform';
2
+ import { type AuditEventInput } from './audit';
3
+ /** One live membership transport key (the `membership_transport_keys` table); only its hash is stored. */
4
+ export interface MembershipTransportKeyRow {
5
+ id: string;
6
+ app: string;
7
+ key_hash: string;
8
+ expires_at: number;
9
+ created_at: number;
10
+ }
11
+ /**
12
+ * The keys an application environment authenticates IAM's membership transport with (`src/membership-http.ts`). A key
13
+ * names the one IAM application id it acts for and nothing else: it is no principal and holds no grant.
14
+ */
15
+ export declare class MembershipTransportKeyRepository {
16
+ protected readonly db: SqlDatabase;
17
+ constructor(db: SqlDatabase);
18
+ /**
19
+ * Record a new key of `app` and delete every other key of it except `keep`, in one batch: the key the caller recorded
20
+ * as active stays valid until the caller replaces it, and a key issued earlier and never recorded is gone.
21
+ */
22
+ issue(input: {
23
+ app: string;
24
+ keyHash: string;
25
+ expiresAt: number;
26
+ keep: string | null;
27
+ now: number;
28
+ }, audit?: (keyId: string) => AuditEventInput): Promise<string>;
29
+ /** The application id a live key acts for, or null for an unknown or expired key. */
30
+ appOfKey(keyHash: string, now: number): Promise<string | null>;
31
+ }
@@ -0,0 +1,273 @@
1
+ import type { PrincipalAffiliation, Scope } from '@fabrika/auth-core';
2
+ import { type SqlDatabase } from '@fabrika/platform';
3
+ import type { StoredPasswordHash } from '../password-crypto';
4
+ import { type AuditEventInput, AuditWriter, type SqlDialect } from './audit';
5
+ import { type GrantRow } from './grants';
6
+ import type { PrincipalRow } from './principals';
7
+ /** A single-use delegation grant (`delegation_grants`). Only its hash is stored. */
8
+ export interface DelegationGrantRow {
9
+ id: string;
10
+ grant_hash: string;
11
+ app: string;
12
+ principal_id: string;
13
+ endpoint: string;
14
+ request_id: string;
15
+ expires_at: number;
16
+ consumed_at: number | null;
17
+ created_at: number;
18
+ }
19
+ /** An invitation to a membership role (`membership_invitations`). `email` is the normalized mailbox. */
20
+ export interface InvitationRow {
21
+ id: string;
22
+ token_hash: string;
23
+ app: string;
24
+ scope_type: string;
25
+ scope_value: string;
26
+ role_key: string;
27
+ email: string;
28
+ invited_by: string;
29
+ expires_at: number;
30
+ accepted_at: number | null;
31
+ accepted_by: string | null;
32
+ acceptance_id: string | null;
33
+ revoked_at: number | null;
34
+ created_at: number;
35
+ /** The invited person's company as the inviting application names it (plain text), or NULL. */
36
+ company_label: string | null;
37
+ /** The inviter's personal message (plain text), or NULL. */
38
+ message: string | null;
39
+ /** The invited person's language as the inviting application named it, or NULL: the application's then. */
40
+ locale: string | null;
41
+ }
42
+ /** One membership-role grant in a value, with the principal it belongs to and its last sign-in to the app. */
43
+ export interface MemberGrantRow {
44
+ principal_id: string;
45
+ role_key: string;
46
+ label: string;
47
+ email: string | null;
48
+ disabled_at: number | null;
49
+ affiliation: PrincipalAffiliation;
50
+ signed_in_at: number | null;
51
+ }
52
+ /** One role grant at an application, in a value or global, with its principal and its last sign-in there. */
53
+ export interface AppMemberGrantRow extends MemberGrantRow {
54
+ scope_type: string | null;
55
+ scope_value: string | null;
56
+ }
57
+ export type InvitationAcceptance = 'accepted' | 'not_allowed' | 'not_eligible' | 'not_open';
58
+ /**
59
+ * How an acceptance that creates its principal ended: accepted with the new principal; refused as `acceptInvitation`
60
+ * refuses; or `account_exists`, because another writer gave the mailbox a principal first, which then signs in and
61
+ * accepts as any existing principal does.
62
+ */
63
+ export type NewPrincipalAcceptance = {
64
+ readonly outcome: 'accepted';
65
+ readonly principal: PrincipalRow;
66
+ } | {
67
+ readonly outcome: Exclude<InvitationAcceptance, 'accepted'> | 'account_exists';
68
+ };
69
+ /**
70
+ * What authorized an acceptance, as the use case checked it: the roles that make the inviter an assigner of the
71
+ * invitation's role, the revision of the app's schema and policies (`AppSchemaRepository.getSchemaRevision`) and the
72
+ * inviter's grant revision (`principals.grant_revision`), both read BEFORE that check. The acceptance writes only while
73
+ * both are unchanged, so the declarations and the grants the check read are the ones in force at the write.
74
+ */
75
+ export interface InviterCheck {
76
+ readonly assigners: readonly string[];
77
+ readonly revision: number;
78
+ readonly grantRevision: number;
79
+ /** The expiry of the grant the acceptance writes: the earliest expiry among the inviter's grants behind it (S8). */
80
+ readonly expiresAt: number | null;
81
+ }
82
+ /** The records of an acceptance that creates its principal, each written exactly when its change was. */
83
+ export interface NewPrincipalEvents {
84
+ readonly principal: AuditEventInput;
85
+ readonly acceptance: AuditEventInput;
86
+ readonly password: AuditEventInput;
87
+ }
88
+ export interface MembershipRepositoryOptions {
89
+ /**
90
+ * The dialect of `db`. On Postgres an acceptance locks the inviter's assigning grant while it commits (`FOR SHARE`),
91
+ * so a removal of that grant waits for it; SQLite has no row locks, and D1 runs a batch alone, which is atomic
92
+ * already. It also picks how a change writes its record (`AuditWriter`).
93
+ */
94
+ readonly dialect?: SqlDialect;
95
+ }
96
+ export declare class MembershipRepository {
97
+ #private;
98
+ protected readonly db: SqlDatabase;
99
+ protected readonly writer: AuditWriter;
100
+ constructor(db: SqlDatabase, options?: MembershipRepositoryOptions);
101
+ issueDelegationGrant(input: {
102
+ grantHash: string;
103
+ app: string;
104
+ principalId: string;
105
+ endpoint: string;
106
+ requestId: string;
107
+ expiresAt: number;
108
+ }): Promise<void>;
109
+ /**
110
+ * Spend a delegation grant, at most once. The UPDATE is the guard, as for `auth_codes`: a second
111
+ * attempt changes no rows. The caller compares the binding AFTER this, so a mismatched attempt spends it too.
112
+ */
113
+ consumeDelegationGrant(grantHash: string, now: number): Promise<DelegationGrantRow | null>;
114
+ /**
115
+ * Record a value IAM generated, grant its creator the creator role, and write `audit`, in one transaction. Both writes
116
+ * carry the eligibility condition (ADR-0051), so a creator who may not hold the role writes nothing: answers false.
117
+ */
118
+ createScope(input: {
119
+ app: string;
120
+ scope: Scope;
121
+ createdBy: string;
122
+ roleKey: string;
123
+ expiresAt?: number | null;
124
+ }, audit?: AuditEventInput): Promise<boolean>;
125
+ /** The active role grants a principal holds in exactly `scope` — the roles it holds "there". */
126
+ listRoleGrantsAt(app: string, principalId: string, scope: Scope): Promise<GrantRow[]>;
127
+ /** The active role grants that make a principal an assigner in `scope` (`ASSIGNING_GRANT`). */
128
+ listAssigningGrants(app: string, principalId: string, scope: Scope): Promise<GrantRow[]>;
129
+ /** Every active grant of a membership role of `scope.type` in `scope`, with its principal. */
130
+ listMemberGrants(app: string, scope: Scope): Promise<MemberGrantRow[]>;
131
+ /** A page of the principals holding a role of `app` (`APP_MEMBER_GRANT`), by id after `after`, at most `limit`. */
132
+ listAppMemberIds(app: string, after: string | null, limit: number): Promise<string[]>;
133
+ /** The grants of `principalIds` that make them people of `app` (`APP_MEMBER_GRANT`), with each principal. */
134
+ listAppMemberGrants(app: string, principalIds: readonly string[]): Promise<AppMemberGrantRow[]>;
135
+ /** Every active grant of a membership role of `scope.type` in `scope` held by `principalId`, with the principal. */
136
+ listMemberGrantsOf(app: string, scope: Scope, principalId: string): Promise<MemberGrantRow[]>;
137
+ /**
138
+ * Grant a membership role in `scope`. Returns the row when this call granted or renewed it, null when it was held or
139
+ * the principal may not hold it (ADR-0051: the insert carries the condition); `audit` is written exactly then.
140
+ */
141
+ grantMembershipRole(input: {
142
+ app: string;
143
+ principalId: string;
144
+ roleKey: string;
145
+ scope: Scope;
146
+ grantedBy: string;
147
+ expiresAt?: number | null;
148
+ }, audit?: AuditEventInput): Promise<GrantRow | null>;
149
+ /** Remove one role grant of a principal in `scope`, after its grant revision (`grantRemovalStatement`). Returns whether a row went. */
150
+ deleteRoleGrant(app: string, principalId: string, roleKey: string, scope: Scope): Promise<boolean>;
151
+ /**
152
+ * Remove a principal's creator-role grant in `scope` only while another principal still holds the role
153
+ * there. The value's `membership_scopes` row is the lock: created on demand, because an administrator
154
+ * may have granted the role without `createScope`, then taken with a row-locking UPDATE — the one lock
155
+ * statement SQLite and Postgres share; D1 runs a batch serially anyway. Two revokes of the last two
156
+ * holders therefore run one after the other, and the second sees the first's delete. The principal's
157
+ * grant revision moves first (`grantRemovalStatement`), so its row is locked before the value's.
158
+ */
159
+ deleteCreatorGrantUnlessLast(app: string, principalId: string, roleKey: string, scope: Scope): Promise<boolean>;
160
+ /**
161
+ * Remove a member's roles in `scope`, in one transaction under the value's lock (`deleteCreatorGrantUnlessLast`),
162
+ * writing one `audit` row per role this call removed, decided by each DELETE itself. The creator role goes only
163
+ * while another principal holds it, and while the member still holds it no other role goes either, so a refused
164
+ * creator leaves every role in place. Answers the roles removed.
165
+ */
166
+ revokeMemberRoles(input: {
167
+ app: string;
168
+ principalId: string;
169
+ scope: Scope;
170
+ roles: readonly string[];
171
+ creatorRole: string | null;
172
+ }, audit: (roleKey: string) => AuditEventInput): Promise<string[]>;
173
+ /** Create the value's `membership_scopes` row on demand and lock it; see `deleteCreatorGrantUnlessLast`. */
174
+ private scopeLockStatements;
175
+ /** The DELETE of a principal's creator grant in `scope`, only while another principal holds the role there. */
176
+ private deleteCreatorChange;
177
+ /**
178
+ * Open an invitation, or answer the open one for the same value, role and mailbox. An expired open
179
+ * invitation is closed first, so it never blocks a new one. `audit` is written exactly when this call opened one.
180
+ */
181
+ openInvitation(input: {
182
+ tokenHash: string;
183
+ app: string;
184
+ scope: Scope;
185
+ roleKey: string;
186
+ email: string;
187
+ invitedBy: string;
188
+ expiresAt: number;
189
+ companyLabel?: string | null;
190
+ message?: string | null;
191
+ locale?: string | null;
192
+ }, audit?: (invitationId: string) => AuditEventInput): Promise<{
193
+ invitation: InvitationRow;
194
+ created: boolean;
195
+ }>;
196
+ /**
197
+ * Give an open invitation a new link and a new lifetime (a resend): the old token's hash is replaced, so its link
198
+ * stops working. A label, message or locale the call names replaces the stored one; NULL keeps it. Answers the
199
+ * invitation when it was still open, and `audit` is written exactly then.
200
+ */
201
+ rotateInvitation(input: {
202
+ id: string;
203
+ tokenHash: string;
204
+ expiresAt: number;
205
+ companyLabel: string | null;
206
+ message: string | null;
207
+ locale: string | null;
208
+ }, audit?: AuditEventInput): Promise<InvitationRow | null>;
209
+ getInvitationByHash(tokenHash: string): Promise<InvitationRow | null>;
210
+ getInvitationById(id: string): Promise<InvitationRow | null>;
211
+ /** Remove an invitation whose email could not be delivered, so a retry opens a fresh one; `audit` records the removal. */
212
+ deleteInvitation(id: string, audit?: AuditEventInput): Promise<void>;
213
+ /** Close an open invitation. Answers whether it was still open; `audit` is written exactly then. */
214
+ revokeInvitation(id: string, audit?: AuditEventInput): Promise<boolean>;
215
+ /**
216
+ * Accept an invitation for `principalId` and write its grant, in one transaction. The guarded UPDATE
217
+ * stamps a fresh acceptance id on the invitation only when it is still open under this token AND the authority the
218
+ * use case checked still holds (`checkedAuthority`): the inviter enabled, the app's schema revision unchanged, and
219
+ * one of the assigners held in the value or, an application-wide role, globally (`ASSIGNING_GRANT`). The re-check
220
+ * is part of the statement that consumes the invitation, so a removal or disable of the inviter, or a reconcile,
221
+ * cannot land between a check and the write. The grant is written
222
+ * only for the attempt holding that id — never on the strength of a timestamp another attempt can share.
223
+ * An expired grant of the role is renewed; an active one is left as it is. A refused acceptance leaves the
224
+ * invitation open. `audit` is written only for the attempt holding the acceptance id, in the same batch.
225
+ */
226
+ acceptInvitation(invitation: InvitationRow, principalId: string, inviter: InviterCheck, audit?: AuditEventInput): Promise<InvitationAcceptance>;
227
+ /**
228
+ * Accept an invitation to a role declared external for a mailbox no principal holds, creating its principal, in ONE
229
+ * transaction: an `external` user labelled with the invited address, active, with its mailbox claimed; the
230
+ * acceptance and its grant as `acceptInvitation` writes them; an enabled password account holding `password`; and the
231
+ * three records. After the inviter's locks (`checkedAuthority`), the new principal's row is the first of its rows
232
+ * written, as every writer of a principal's rows starts at it (`principalLockStatements`); nobody else sees it before
233
+ * the commit.
234
+ *
235
+ * The principal is inserted only while the invitation is open under this token, the checked authority holds
236
+ * (`checkedAuthority`: the inviter enabled, the schema revision unchanged, its assigning grant `FOR SHARE`) and the
237
+ * role is still external (`FOR SHARE`), so a refusal writes nothing. The acceptance repeats the authority and
238
+ * eligibility conditions, and a guard rolls the whole batch back if a change in between (the invitation cancelled or
239
+ * rotated) left the principal without its grant. A mailbox another writer claimed first fails the principal's unique
240
+ * mailbox, which rolls the batch back: `account_exists`.
241
+ */
242
+ acceptAsNewPrincipal(input: {
243
+ readonly invitation: InvitationRow;
244
+ readonly inviter: InviterCheck;
245
+ readonly password: StoredPasswordHash;
246
+ }, events: (principalId: string) => NewPrincipalEvents): Promise<NewPrincipalAcceptance>;
247
+ /** Whether a user principal holds `mailbox`, a normalized address. */
248
+ private mailboxHeld;
249
+ /** Why an acceptance that would create its principal wrote nothing: the invitation closed, or its role is no longer external. */
250
+ private newPrincipalRefusal;
251
+ /**
252
+ * What an acceptance's writes require of the authority `inviter` checked: the inviter is still enabled and its grant
253
+ * revision unchanged (no grant of it was removed, the one that supplied an action of the role included), the app's
254
+ * schema revision is still the one read before the check, and the inviter still holds one of the assigners in the
255
+ * value, or an application-wide one globally (`ASSIGNING_GRANT`).
256
+ *
257
+ * On Postgres `locks` come first in the batch, in the principal-first order: the inviter's principal row `FOR SHARE`
258
+ * (a disable or a grant removal waits for the acceptance, or the acceptance reads the committed change), then the revision row
259
+ * `FOR SHARE` (a reconcile's increment, its first statement, waits, or the acceptance reads the new revision), and
260
+ * only then do the writes' conditions lock the assigning grant and the role row. SQLite and D1 run one writer at a
261
+ * time and need none.
262
+ */
263
+ private checkedAuthority;
264
+ /** Whether `principalId` may not hold the invitation's role now (ADR-0051). */
265
+ private ineligible;
266
+ /** Open, unexpired invitations to `scope`. */
267
+ listOpenInvitations(app: string, scope: Scope): Promise<InvitationRow[]>;
268
+ /**
269
+ * Prune spent and expired delegation grants, and invitations whose expiry is older than
270
+ * `invitationCutoff` whatever their state (the audit log keeps the history). Returns rows removed.
271
+ */
272
+ prune(now: number, invitationCutoff: number): Promise<number>;
273
+ }
@@ -0,0 +1,55 @@
1
+ import type { SqlDatabase } from '@fabrika/platform';
2
+ import { type AuditEventInput, AuditWriter } from './audit';
3
+ export interface OAuthGrantRow {
4
+ id: string;
5
+ principal_id: string;
6
+ session_id: string;
7
+ client_id: string;
8
+ app: string;
9
+ resource: string;
10
+ /** JSON array of the consented scope names. */
11
+ scopes: string;
12
+ created_at: number;
13
+ expires_at: number;
14
+ revoked_at: number | null;
15
+ }
16
+ export interface OAuthCodeRow {
17
+ token_hash: string;
18
+ grant_id: string;
19
+ redirect_uri: string;
20
+ challenge: string;
21
+ expires_at: number;
22
+ used_at: number | null;
23
+ }
24
+ export interface OAuthRefreshTokenRow {
25
+ token_hash: string;
26
+ grant_id: string;
27
+ expires_at: number;
28
+ used_at: number | null;
29
+ }
30
+ /** Codes and refresh tokens are addressed by hash only; the plaintext never reaches this layer. */
31
+ export declare class OAuthRepository {
32
+ private readonly db;
33
+ private readonly writer;
34
+ constructor(db: SqlDatabase, writer?: AuditWriter);
35
+ /** Record a consent, its one authorization code and `audit` together. */
36
+ createGrant(grant: OAuthGrantRow, code: Omit<OAuthCodeRow, 'grant_id' | 'used_at'>, audit?: AuditEventInput): Promise<void>;
37
+ grant(id: string): Promise<OAuthGrantRow | null>;
38
+ listForPrincipal(principalId: string, before: string | undefined, limit: number): Promise<OAuthGrantRow[]>;
39
+ /** Revoke a grant; `audit` is written exactly when this call revoked it. */
40
+ revokeGrant(id: string, now: number, audit?: AuditEventInput): Promise<void>;
41
+ /** Spend a code. Exactly one caller gets the row back; a replay gets null and finds it via `code`. */
42
+ consumeCode(tokenHash: string, now: number): Promise<OAuthCodeRow | null>;
43
+ code(tokenHash: string): Promise<OAuthCodeRow | null>;
44
+ insertRefreshToken(row: Omit<OAuthRefreshTokenRow, 'used_at'>): Promise<void>;
45
+ /** Spend a refresh token for rotation. A second presentation gets null and finds the spent row via `refreshToken`. */
46
+ consumeRefreshToken(tokenHash: string, now: number): Promise<OAuthRefreshTokenRow | null>;
47
+ refreshToken(tokenHash: string): Promise<OAuthRefreshTokenRow | null>;
48
+ /** Record an accepted client assertion's `jti`. False when the client already presented it: a replay. */
49
+ recordClientAssertion(clientId: string, jti: string, expiresAt: number): Promise<boolean>;
50
+ /**
51
+ * Delete codes, refresh tokens and client assertion ids past expiry. Spent rows stay until then because they are what
52
+ * detects a replay. Grants go with their session row (`ON DELETE CASCADE`), which shares their expiry.
53
+ */
54
+ prune(now: number): Promise<void>;
55
+ }
@@ -0,0 +1,25 @@
1
+ import { type SqlDatabase } from '@fabrika/platform';
2
+ import { type AuditEventInput, AuditWriter } from './audit';
3
+ /** The one row of `password_policy` (migration 0022). No row is the default minimum. */
4
+ export interface PasswordPolicyRow {
5
+ id: number;
6
+ min_length: number;
7
+ /** When an administrator last changed the minimum, in epoch seconds. */
8
+ updated_at: number;
9
+ }
10
+ /**
11
+ * The installation's minimum password length (ADR-0020). The row is the installation's, so its key is the constant 1. The
12
+ * caller has held the value to the runtime's floor; this stores it as given, and the reader checks it again.
13
+ */
14
+ export declare class PasswordPolicyRepository {
15
+ protected readonly db: SqlDatabase;
16
+ private readonly writer;
17
+ constructor(db: SqlDatabase, writer?: AuditWriter);
18
+ /** The stored row, or null when nobody ever set a minimum. */
19
+ get(): Promise<PasswordPolicyRow | null>;
20
+ /**
21
+ * Store the minimum, with its record in the same statement group. Only a value that differs from the stored one is a
22
+ * change, so setting the minimum it already has writes neither the row nor its record; null then.
23
+ */
24
+ setMinLength(minLength: number, audit: AuditEventInput): Promise<PasswordPolicyRow | null>;
25
+ }