@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,21 @@
1
+ /**
2
+ * Opaque-secret helpers shared by every stored credential (API keys, share links, SSO sessions):
3
+ * generate a high-entropy token and hash it for storage. Only the SHA-256 hash is ever persisted,
4
+ * so a DB leak yields no usable secret.
5
+ */
6
+ /** SHA-256 hex of a token. Only the hash is stored — a DB leak yields no usable token. */
7
+ export declare function hashToken(token: string): Promise<string>;
8
+ /**
9
+ * Constant-time equality for two SHA-256 hex digests (`hashToken` output). Compares hashes, not raw
10
+ * secrets, and in constant time over equal-length inputs — so neither a length nor a per-character
11
+ * timing signal leaks how much of a presented token matched an expected one. Differing lengths are
12
+ * unequal (both are always 64-char hex here).
13
+ */
14
+ export declare function timingSafeEqualHex(a: string, b: string): boolean;
15
+ /** A key IAM issued: `px_` and the 27 base64url characters `generateToken` writes. A public route refuses anything else before any lookup. */
16
+ export declare const ISSUED_KEY_FORMAT: RegExp;
17
+ /**
18
+ * Generate a high-entropy random token (160 bits, > the 128-bit minimum) as
19
+ * URL-safe base64url. The plaintext is shown once at issue and never stored.
20
+ */
21
+ export declare function generateToken(): string;
@@ -0,0 +1,19 @@
1
+ /** The composition switches of `IamAppOptions`; a switched-off surface has no row, so neither side serves it. */
2
+ export interface IamServiceOptions {
3
+ /** Process liveness (`health`). */
4
+ readonly health: boolean;
5
+ /** The management transport `/rpc/*` (a non-empty `rpcKey`). */
6
+ readonly managementRpc: boolean;
7
+ /** The proxy's mint transport `/auth/mint/*` (a non-empty `proxyKey`). */
8
+ readonly mintRpc: boolean;
9
+ /** The applications' membership transport `/membership/<method>` (`membershipTransport`). */
10
+ readonly membershipTransport: boolean;
11
+ }
12
+ export declare function iamServiceContract(options: IamServiceOptions): import("@fabrika/app/service").ServiceArtifact<"admin" | "health" | "rpc" | "oauth" | "auth" | "jwks" | "membership" | "oauth-metadata" | "auth-root" | "mint-session" | "mint-key" | "mint-exchange" | "mint-delegation" | "admin-rpc" | "admin-root" | "rpc-root">;
13
+ /**
14
+ * What an installation's IAM process mounts, and so the table the platform proxy publishes: the Zerops topology always
15
+ * writes both transport keys. Zerops applications reach `/membership/*` and an operator's `fabrika control key issue`
16
+ * reaches `/rpc/*` at the public issuer; namespace proxies mint over `/auth/mint/*` there too.
17
+ */
18
+ export declare const IAM_INSTALLATION_SERVICE_OPTIONS: IamServiceOptions;
19
+ export type IamEndpointId = ReturnType<typeof iamServiceContract>['endpoints'][number]['id'];
@@ -0,0 +1,72 @@
1
+ import { type EmailSender } from '@fabrika/email';
2
+ import type { IamRepositories } from './db';
3
+ import type { Env } from './env';
4
+ import { type OAuthConfig } from './oauth-config';
5
+ import { OidcClient } from './oidc';
6
+ import { type PasswordHasher } from './password-crypto';
7
+ import { type Retention } from './retention';
8
+ /**
9
+ * Pre-wired services + parsed config for every request. Handlers take `Services`
10
+ * (or pick what they need off it) instead of reaching into `env` directly —
11
+ * keeps them decoupled from the CF binding shape and easier to test.
12
+ */
13
+ export interface Services {
14
+ readonly repositories: IamRepositories;
15
+ /** OIDC relying-party client. Null when the method is disabled. */
16
+ readonly oidc: OidcClient | null;
17
+ readonly passwordHasher: PasswordHasher;
18
+ readonly email: EmailSender | null;
19
+ readonly config: Config;
20
+ }
21
+ export interface Config {
22
+ /**
23
+ * The central human-admission allowlist — who may self-provision as a HUMAN at login. A `*` entry
24
+ * in either list means admit-all; otherwise an exact email or matching domain. Owned by IAM
25
+ * (deploy vars `FABRIKA_IAM_HUMAN_EMAIL_DOMAINS` / `FABRIKA_IAM_HUMAN_EMAILS`).
26
+ */
27
+ readonly human: {
28
+ readonly emailDomains: readonly string[];
29
+ readonly emails: readonly string[];
30
+ };
31
+ /** Bootstrap-admin emails (normally empty). Always admitted; resolve to the global `admin` role. */
32
+ readonly bootstrapAdmins: ReadonlySet<string>;
33
+ /**
34
+ * App ids whose proxy-injected access token may administer `/admin/*` (`ADMIN_AUDIENCES`).
35
+ * EMPTY means no JWT may administer at all, which is the fail-closed end of the same registry.
36
+ */
37
+ readonly adminAudiences: ReadonlySet<string>;
38
+ /**
39
+ * App ids of the installation's own services. Only a token for one of them carries cross-app grants
40
+ * and the bootstrap `admin`; an application token carries the grants of its own app id and nothing else.
41
+ */
42
+ readonly platformAudiences: ReadonlySet<string>;
43
+ readonly environment: string;
44
+ /** Create a real local admin session without contacting an external OIDC provider. */
45
+ readonly localDevLogin: boolean;
46
+ readonly authentication: {
47
+ readonly oidc: {
48
+ readonly enabled: boolean;
49
+ };
50
+ readonly password: {
51
+ readonly enabled: boolean;
52
+ /** The lowest minimum length an administrator may set here (`passwordMinLengthFloor` of the hasher's cost). */
53
+ readonly minLengthFloor: number;
54
+ };
55
+ };
56
+ /** The OAuth authorization server for MCP clients; null = off. */
57
+ readonly oauth: OAuthConfig | null;
58
+ /** How long the maintenance cron keeps `auth_log` and `audit_events`. */
59
+ readonly retention: Retention;
60
+ /** IAM's own origin — the `iss` of minted tokens and the OIDC redirect base. */
61
+ readonly issuer: string;
62
+ }
63
+ /**
64
+ * The console gateway's app id — the only first-party holder of a JWT that administers IAM, and so
65
+ * the audience an installation gets when it names none.
66
+ */
67
+ export declare const DEFAULT_ADMIN_AUDIENCES: ReadonlySet<string>;
68
+ /** IAM itself, the console (Control's app id) and Operations — the services every installation runs. */
69
+ export declare const DEFAULT_PLATFORM_AUDIENCES: ReadonlySet<string>;
70
+ /** An admin audience is a console gateway, so it is a platform service too, whatever it is named. */
71
+ export declare function platformAudiencesFor(adminAudiences: ReadonlySet<string>): ReadonlySet<string>;
72
+ export declare function buildServices(env: Env): Services;
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Token signing key custody for IAM-issued tokens. The Worker is now an ISSUER: it signs
3
+ * per-app permission tokens (and capability tokens) with its own ES256 (EC P-256) key, and
4
+ * publishes the public half so the SDK verifies locally — no per-request round-trip.
5
+ *
6
+ * Keys come from the `FABRIKA_IAM_SIGNING_KEYS` secret (a JSON array of private JWKs). Index 0 is the
7
+ * ACTIVE signer; every key is published in the JWKS, so a freshly-added rotation key can be verified
8
+ * (it's in the public set) before it's promoted to index 0 to sign. Locally, with no keys
9
+ * configured, an EPHEMERAL key is generated per isolate — fine for dev (sessions reset on restart),
10
+ * refused on stage/prod (a real deploy must provision durable keys).
11
+ *
12
+ * One Signer per isolate, memoised like the `JwtValidator` (jose imports the key once).
13
+ */
14
+ import { type AccessTokenClaims, type Jwks } from '@fabrika/auth-core';
15
+ import { type JWK, type JWTPayload } from 'jose';
16
+ import type { Env } from './env';
17
+ export declare class Signer {
18
+ private readonly keys;
19
+ private constructor();
20
+ /**
21
+ * Sign a claims object with the ACTIVE key. The `kid` goes in the protected header so the SDK
22
+ * picks the matching public JWK. The claims already carry `iss`/`aud`/`exp` (built in core).
23
+ */
24
+ sign(claims: AccessTokenClaims): Promise<string>;
25
+ /**
26
+ * Sign an OAuth resource access token (`buildResourceAccessClaims`). `typ: at+jwt` (RFC 9068) is what
27
+ * keeps it from passing as a native app token, whose header carries no `typ`.
28
+ */
29
+ signResourceToken(claims: Record<string, unknown>): Promise<string>;
30
+ /**
31
+ * Sign a short-lived INTERNAL payload — state IAM hands to a browser and expects back unaltered,
32
+ * such as the OIDC flight cookie. Not an access token: no `iss`, no `perms`, and an `aud` that is
33
+ * never an app id, so the two can never be mistaken for each other in either direction.
34
+ */
35
+ signInternal(payload: JWTPayload, audience: string, ttlSeconds: number): Promise<string>;
36
+ /** The public key set — active key first, then any rotation keys. */
37
+ jwks(): Jwks;
38
+ /** Import the configured private JWKs (index 0 = active signer). */
39
+ static fromPrivateJwks(jwks: JWK[]): Promise<Signer>;
40
+ /** Generate an ephemeral signer — LOCAL DEV ONLY (rotates on isolate restart; sessions reset). */
41
+ static ephemeral(): Promise<Signer>;
42
+ }
43
+ /**
44
+ * Verify an IAM-issued NATIVE access token (`px_token`) against THIS signer's own public keys and
45
+ * narrow it to access claims; null on any failure. The server-side mirror of the SDK's local verify
46
+ * (`session.ts`) — IAM issued the token, so it trusts the `perms`/`ptype`/`aud` it signed. The
47
+ * signer already holds the current + rotation public keys, so there is no JWKS fetch or kid-miss retry.
48
+ *
49
+ * An OAuth resource token is REFUSED here: it was minted for an MCP client and one resource, and must
50
+ * never act as a native credential for IAM itself (`verifyResourceAccessToken` is its only reader).
51
+ */
52
+ export declare function verifyAccessToken(signer: Signer, token: string, opts: VerifyOptions & {
53
+ audience?: string;
54
+ }): Promise<AccessTokenClaims | null>;
55
+ /** Verify an OAuth resource token (`typ: at+jwt`, OAuth claims present); null for anything else. */
56
+ export declare function verifyResourceAccessToken(signer: Signer, token: string, opts: VerifyOptions): Promise<AccessTokenClaims | null>;
57
+ interface VerifyOptions {
58
+ issuer: string;
59
+ /** Accept a token this many seconds past its `exp`. Only a caller that records, never authorizes, may pass it. */
60
+ expiryGraceSeconds?: number;
61
+ }
62
+ /**
63
+ * Verify a payload `signInternal` produced, against this signer's own keys; null on any failure
64
+ * (bad signature, wrong audience, expired). The counterpart of `verifyAccessToken`, and deliberately
65
+ * separate: it demands the internal audience, so an access token never verifies here.
66
+ */
67
+ export declare function verifyInternal(signer: Signer, token: string, audience: string): Promise<JWTPayload | null>;
68
+ /** Parse `FABRIKA_IAM_SIGNING_KEYS` into private JWKs. Throws loudly on malformed config (fail the deploy). */
69
+ export declare function parseSigningKeys(raw: string): JWK[];
70
+ /** The isolate-cached Signer for this env. Returns a promise (key import is async). */
71
+ export declare function getSigner(env: Pick<Env, 'FABRIKA_IAM_SIGNING_KEYS' | 'ENVIRONMENT'>): Promise<Signer>;
72
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { Scope } from '@fabrika/auth-core';
2
+ /**
3
+ * A share link's stored containment chain (`credentials.link_chain`), or null when the column does not
4
+ * hold a non-empty array of `{type, value}`. A key whose chain does not parse resolves to nothing.
5
+ */
6
+ export declare function parseStoredChain(json: string): Scope[] | null;
@@ -0,0 +1,12 @@
1
+ /** A role's stored `via`: relation → actions. A malformed row contributes nothing, as an unparseable grant does. */
2
+ export declare function parseStoredVia(json: string | null): Record<string, string[]>;
3
+ /** Legacy rows retain valid patterns even when other entries are malformed. */
4
+ export declare function parseStoredPermissions(json: string): string[];
5
+ /**
6
+ * Every action a grant of a role carries: its own, and each one it holds through a relation (`via`). Whoever gives the
7
+ * role must hold each of them, by a grant of the action itself: a relation it holds them through does not count.
8
+ */
9
+ export declare function delegatedActions(role: {
10
+ readonly permissions: string;
11
+ readonly via: string | null;
12
+ }): string[];
@@ -0,0 +1,7 @@
1
+ /** The longest label an administrator may give a terminal, in code points. */
2
+ export declare const MAX_TERMINAL_LABEL_LENGTH = 100;
3
+ /**
4
+ * A terminal's label as stored, or null when it may not be one: trimmed, 1 to `MAX_TERMINAL_LABEL_LENGTH` code points,
5
+ * no control character. It reaches a person's token as `terminal.label` and every page that names the terminal.
6
+ */
7
+ export declare function terminalLabel(raw: string): string | null;
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Minting per-app access tokens — the heart of IAM-native auth.
3
+ *
4
+ * Two fronts over one resolve→sign core:
5
+ * - `mintToken` — from the browser's opaque SSO session cookie (a principal-bound credential in a
6
+ * cookie). Validates the session, resolves the principal's permissions for the calling app.
7
+ * - `mintFromKey` — from an opaque `px_` credential (API key / share link) presented as a bearer or
8
+ * URL-path token. Resolves the credential's EFFECTIVE permissions (the 2×2: principal? ∩ inline).
9
+ *
10
+ * Both end in `signAccessToken`: sign a short-lived token the SDK then verifies locally, so this runs
11
+ * ≈ once per TTL per app, not per request. `mintToken` always yields a principal token; `mintFromKey`
12
+ * yields a principal token (bound credential) or an anonymous one (frozen inline grants).
13
+ */
14
+ import { buildAccessClaims, type LinkTokenClaims, type MintFromKeyResult, type MintTokenInput, type MintTokenResult, type PermissionEntry, type TokenPrincipalType } from '@fabrika/auth-core';
15
+ import type { CredentialRow, PrincipalRow, SessionTerminal } from './db';
16
+ import type { Env } from './env';
17
+ import { type ResolvedAuthority } from './resolve';
18
+ import type { Services } from './services';
19
+ /** The env slice minting needs (the signing keys + environment for `getSigner`). */
20
+ type MintEnv = Pick<Env, 'FABRIKA_IAM_SIGNING_KEYS' | 'ENVIRONMENT'>;
21
+ /**
22
+ * `principalId` (resolved session subject, or null on failure) and the session's terminal are surfaced for the
23
+ * auth_log row.
24
+ */
25
+ export interface MintOutcome {
26
+ result: MintTokenResult;
27
+ principalId: string | null;
28
+ terminal: SessionTerminal | null;
29
+ }
30
+ export declare function mintToken(services: Services, env: MintEnv, input: MintTokenInput): Promise<MintOutcome>;
31
+ /**
32
+ * A person's live authority at `app` on the terminal `terminalId` that asserted them there (ADR-0067 §4): the set a
33
+ * mint would sign now, and the grants behind it, the terminal principal's included; null when the terminal could no
34
+ * longer mint for `app`. What a terminal session's token is re-resolved against.
35
+ */
36
+ export declare function authorityOnTerminal(services: Services, principal: PrincipalRow, terminalId: string, app: string): Promise<ResolvedAuthority | null>;
37
+ /** `principalId`/`credentialId` are surfaced for the auth_log row. */
38
+ export interface MintFromKeyOutcome {
39
+ result: MintFromKeyResult;
40
+ principalId: string | null;
41
+ credentialId: string | null;
42
+ }
43
+ /**
44
+ * Resolve an opaque `px_` credential into an access token. The credential's EFFECTIVE permissions
45
+ * follow the 2×2: principal-bound → that principal's live resolved perms (∩ inline restriction when
46
+ * present); anonymous → its frozen inline grants. The signed token is principal-bound (carries
47
+ * `ptype`) iff the credential is.
48
+ */
49
+ export declare function mintFromKey(services: Services, env: MintEnv, input: {
50
+ app: string;
51
+ key: string;
52
+ requestId: string;
53
+ }): Promise<MintFromKeyOutcome>;
54
+ /**
55
+ * The token of a job's or a webhook's own key, for `POST /auth/workload/token`. The kind and the application are
56
+ * checked before anything about the principal is read, so every key that is not a live job or webhook key of `app` —
57
+ * absent, revoked, expired, of another kind or app, or of a disabled principal — answers the same `invalid_key`.
58
+ * A storage failure throws: the route answers it as "IAM could not be asked", never as a refusal.
59
+ */
60
+ export declare function mintFromWorkloadKey(services: Services, env: MintEnv, input: {
61
+ app: string;
62
+ key: string;
63
+ }): Promise<MintFromKeyOutcome>;
64
+ /**
65
+ * The credential's effective permissions + the token subject/type/label, or a typed failure.
66
+ * `wrong_app` is IAM-internal: every mint front reports it as `invalid_key`, because "this key is
67
+ * not for you" and "there is no such key" must look the same to a caller who holds neither.
68
+ */
69
+ export type ResolvedCredential = {
70
+ ok: true;
71
+ subject: string;
72
+ type?: TokenPrincipalType;
73
+ /** A workload principal's name (`job`, `webhook`), or a connector's consuming application (`app`). */
74
+ workload?: string;
75
+ label: string | null;
76
+ /** A user principal's mailbox. */
77
+ email?: string;
78
+ permissions: PermissionEntry[];
79
+ /** When the earliest carried permission loses its last supporting grant; `null` when none expires. */
80
+ expiresAt: number | null;
81
+ link?: LinkTokenClaims;
82
+ } | {
83
+ ok: false;
84
+ reason: 'unknown_principal' | 'disabled' | 'wrong_app' | 'issuer_unauthorized';
85
+ };
86
+ /**
87
+ * Resolve a credential's effective permissions FOR ONE APP.
88
+ *
89
+ * **The app binding is checked first, and it is not optional for an anonymous credential.** Inline
90
+ * grants are frozen at issue and were delegation-checked against the issuer's permissions *for the
91
+ * issuing app*; without a binding that authority was granted per app and spent installation-wide, so
92
+ * an app-scoped admin of the least important app could issue `{ action: '*' }` and present the key to
93
+ * every other app behind the proxy (SEC-2). A `NULL` app on an anonymous credential is therefore
94
+ * refused rather than treated as cross-app — a credential issued before the binding existed must be
95
+ * reissued, a hard cutover.
96
+ *
97
+ * A principal-bound credential may be cross-app (`NULL`) because it carries no frozen authority: its
98
+ * permissions are resolved per app from `grants`, which are themselves app-filtered. When it names an
99
+ * app, that is a downscope and is enforced the same way.
100
+ */
101
+ export declare function resolveCredential(services: Services, cred: CredentialRow, app: string): Promise<ResolvedCredential>;
102
+ /**
103
+ * The signed token, or null — with a warning naming the subject and the size — when it is above `who.bytes`
104
+ * (`MAX_ACCESS_TOKEN_BYTES` unless given). Every bearer IAM issues passes here: native tokens and OAuth resource tokens.
105
+ */
106
+ export declare function boundedToken(signed: Promise<string>, who: {
107
+ app: string;
108
+ subject: string;
109
+ bytes?: number;
110
+ }): Promise<string | null>;
111
+ /** Sign an access token with the isolate's active signing key. Shared by every mint front. */
112
+ export declare function signAccessToken(services: Services, env: MintEnv, params: Omit<Parameters<typeof buildAccessClaims>[0], 'iss'>): Promise<string>;
113
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { Env, RequestContext } from './env';
2
+ import type { Services } from './services';
3
+ /**
4
+ * A decided outcome is a 200 like the proxy's mint surface: `{ ok: false }` is "IAM said no". A storage failure is a
5
+ * 503, so the application reports "IAM could not be asked", never a refused key.
6
+ */
7
+ export declare function handleWorkloadToken(request: Request, services: Services, env: Env, ctx: RequestContext, requestId: string, clientAddress: string | null): Promise<Response>;
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # @fabrika/iam
2
-
3
- Placeholder. Releases are published from CI by contember/fabrika-platform.