@rebasepro/server 0.25.1 → 0.27.0

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 (179) hide show
  1. package/dist/{GCSStorageController-CRd9bFrc.js → GCSStorageController-BUClIfp0.js} +40 -3
  2. package/dist/GCSStorageController-BUClIfp0.js.map +1 -0
  3. package/dist/{S3StorageController-D00EFa5P.js → S3StorageController-CixCQEky.js} +58 -10
  4. package/dist/S3StorageController-CixCQEky.js.map +1 -0
  5. package/dist/access-BFAUvsSs.js +1897 -0
  6. package/dist/access-BFAUvsSs.js.map +1 -0
  7. package/dist/api/branch-routes.d.ts +25 -0
  8. package/dist/api/live-schema-routes.d.ts +10 -0
  9. package/dist/api/rest/api-generator.d.ts +9 -1
  10. package/dist/api/rest/idempotency.d.ts +15 -1
  11. package/dist/api/root-error-handler.d.ts +6 -5
  12. package/dist/api/types.d.ts +12 -0
  13. package/dist/{ast-schema-editor-mCeSC03x.js → ast-schema-editor-madTa9Hh.js} +2 -2
  14. package/dist/{ast-schema-editor-mCeSC03x.js.map → ast-schema-editor-madTa9Hh.js.map} +1 -1
  15. package/dist/auth/access.d.ts +10 -0
  16. package/dist/auth/adapter-middleware.d.ts +2 -1
  17. package/dist/auth/adapter-service-key.d.ts +24 -0
  18. package/dist/auth/admin-users-route.d.ts +3 -0
  19. package/dist/auth/api-keys/api-key-middleware.d.ts +21 -1
  20. package/dist/auth/api-keys/api-key-store.d.ts +8 -1
  21. package/dist/auth/api-keys/key-grant.d.ts +18 -1
  22. package/dist/auth/apple-oauth.d.ts +39 -10
  23. package/dist/auth/capabilities.d.ts +2 -0
  24. package/dist/auth/expired-token-sweep.d.ts +5 -0
  25. package/dist/auth/impersonation.d.ts +3 -1
  26. package/dist/auth/interfaces.d.ts +55 -0
  27. package/dist/auth/jwt.d.ts +8 -2
  28. package/dist/auth/oauth-signin-policy.d.ts +21 -35
  29. package/dist/auth/rls-scope.d.ts +17 -7
  30. package/dist/auth/rls-simulator-routes.d.ts +51 -0
  31. package/dist/auth/simulation-read-only.d.ts +28 -0
  32. package/dist/auth/simulation.d.ts +141 -0
  33. package/dist/auth/sql-rate-limit-store.d.ts +15 -1
  34. package/dist/{auth-Dkvo2tTJ.js → auth-DDXPw0mn.js} +527 -1883
  35. package/dist/auth-DDXPw0mn.js.map +1 -0
  36. package/dist/auth_adapter-B7RUMk6e.js +89 -0
  37. package/dist/auth_adapter-B7RUMk6e.js.map +1 -0
  38. package/dist/backend/create-backend.d.ts +6 -0
  39. package/dist/backend/options.d.ts +38 -5
  40. package/dist/{backend-DTAOsLQc.js → backend-CaG_Jyso.js} +9 -2
  41. package/dist/backend-CaG_Jyso.js.map +1 -0
  42. package/dist/{backup-B9m3cb3X.js → backup-B6brPSeB.js} +6 -6
  43. package/dist/{backup-B9m3cb3X.js.map → backup-B6brPSeB.js.map} +1 -1
  44. package/dist/boot/boot.d.ts +2 -15
  45. package/dist/boot/ddl-bootstrap.d.ts +34 -0
  46. package/dist/boot/env.d.ts +12 -0
  47. package/dist/boot/fetch-bundle.d.ts +5 -1
  48. package/dist/boot/provision-only.d.ts +23 -0
  49. package/dist/boot/provision.d.ts +53 -2
  50. package/dist/boot/role.d.ts +2 -0
  51. package/dist/boot/schema-change-recorder.d.ts +45 -0
  52. package/dist/boot/security-headers.d.ts +22 -0
  53. package/dist/branch-routes-B2vvEx53.js +84 -0
  54. package/dist/branch-routes-B2vvEx53.js.map +1 -0
  55. package/dist/{contract-routes-CsFXW7j4.js → contract-routes-CU2A3JnF.js} +2 -2
  56. package/dist/{contract-routes-CsFXW7j4.js.map → contract-routes-CU2A3JnF.js.map} +1 -1
  57. package/dist/cron/cron-loader.d.ts +4 -0
  58. package/dist/cron/cron-store.d.ts +8 -1
  59. package/dist/{cron-loader-Dj6lA4HQ.js → cron-loader-CC1Jy_KN.js} +14 -6
  60. package/dist/cron-loader-CC1Jy_KN.js.map +1 -0
  61. package/dist/{cron-routes-CwbLZguO.js → cron-routes-BK60Rn1r.js} +3 -3
  62. package/dist/{cron-routes-CwbLZguO.js.map → cron-routes-BK60Rn1r.js.map} +1 -1
  63. package/dist/{cron-scheduler-B04ACoUE.js → cron-scheduler-DyCjPlQR.js} +40 -27
  64. package/dist/cron-scheduler-DyCjPlQR.js.map +1 -0
  65. package/dist/{cron-store-DkyfuLOH.js → cron-store-DLn11M8H.js} +10 -9
  66. package/dist/cron-store-DLn11M8H.js.map +1 -0
  67. package/dist/{strings.es-BeZWAodn.js → data_driver-D0mg1Y_x.js} +2 -90
  68. package/dist/data_driver-D0mg1Y_x.js.map +1 -0
  69. package/dist/{ddl-bootstrap-Ck20GSIX.js → ddl-bootstrap-CjRrn12l.js} +91 -2
  70. package/dist/ddl-bootstrap-CjRrn12l.js.map +1 -0
  71. package/dist/deploy/pod-contract.d.ts +1 -1
  72. package/dist/{load-failure-L5hvfDoG.js → dynamic-import-Bi3XK5y-.js} +30 -18
  73. package/dist/dynamic-import-Bi3XK5y-.js.map +1 -0
  74. package/dist/email/dev-sink.d.ts +6 -0
  75. package/dist/email/index.d.ts +1 -1
  76. package/dist/{soft-delete-params-BmlVwecC.js → field-access-DYsvjI_P.js} +27 -57
  77. package/dist/field-access-DYsvjI_P.js.map +1 -0
  78. package/dist/{filter-dialect-O1HK6Nvr.js → filter-dialect-TrKFWLqT.js} +3 -3
  79. package/dist/{filter-dialect-O1HK6Nvr.js.map → filter-dialect-TrKFWLqT.js.map} +1 -1
  80. package/dist/{function-loader-KKDqTcIr.js → function-loader-eFigStYX.js} +34 -18
  81. package/dist/function-loader-eFigStYX.js.map +1 -0
  82. package/dist/functions/function-loader.d.ts +5 -0
  83. package/dist/functions/hono-like.d.ts +12 -0
  84. package/dist/functions/index.js +19 -1
  85. package/dist/functions/index.js.map +1 -1
  86. package/dist/functions/request-timeout.d.ts +3 -3
  87. package/dist/history/history-routes.d.ts +6 -0
  88. package/dist/history/retention-sweep.d.ts +5 -0
  89. package/dist/{history-recorder-B2rkuhuy.js → history-recorder-Bsau7h4X.js} +6 -6
  90. package/dist/history-recorder-Bsau7h4X.js.map +1 -0
  91. package/dist/{history-store-DPofGVDp.js → history-store-BQz5YCkW.js} +26 -2
  92. package/dist/history-store-BQz5YCkW.js.map +1 -0
  93. package/dist/index.d.ts +4 -2
  94. package/dist/index.es.js +3026 -611
  95. package/dist/index.es.js.map +1 -1
  96. package/dist/init/docs.d.ts +1 -1
  97. package/dist/init/health.d.ts +13 -0
  98. package/dist/init/process-safety.d.ts +4 -3
  99. package/dist/init/shutdown.d.ts +8 -0
  100. package/dist/init.d.ts +6 -1
  101. package/dist/jobs/job-store.d.ts +8 -1
  102. package/dist/{jobs-h1WRq3cp.js → jobs-Co6Ni5HP.js} +6 -6
  103. package/dist/jobs-Co6Ni5HP.js.map +1 -0
  104. package/dist/{jwt-D3oFauZI.js → jwt-rDS0884_.js} +4 -3
  105. package/dist/{jwt-D3oFauZI.js.map → jwt-rDS0884_.js.map} +1 -1
  106. package/dist/{keys-COzQ2aE8.js → keys-BXcdRNhJ.js} +2 -2
  107. package/dist/{keys-COzQ2aE8.js.map → keys-BXcdRNhJ.js.map} +1 -1
  108. package/dist/mcp/consent-page.d.ts +38 -1
  109. package/dist/mcp/mcp-routes.d.ts +9 -0
  110. package/dist/mcp/mcp-tools.d.ts +7 -1
  111. package/dist/mcp/oauth-store.d.ts +12 -5
  112. package/dist/metrics/history-recorder.d.ts +4 -1
  113. package/dist/metrics/history-store.d.ts +8 -0
  114. package/dist/metrics/index.d.ts +8 -0
  115. package/dist/metrics/realtime.d.ts +25 -0
  116. package/dist/{offline-BrqCRgih-BtDB1ecy.js → offline-AvuEcg7u-DFHL98OT.js} +127 -40
  117. package/dist/offline-AvuEcg7u-DFHL98OT.js.map +1 -0
  118. package/dist/{offline-store-BhYXViha-BW5j1We_.js → offline-store-BhYXViha-YTmjBpo2.js} +5 -5
  119. package/dist/{offline-store-BhYXViha-BW5j1We_.js.map → offline-store-BhYXViha-YTmjBpo2.js.map} +1 -1
  120. package/dist/{openapi-generator-BjHr6upl.js → openapi-generator-CyxEtYEX.js} +61 -22
  121. package/dist/openapi-generator-CyxEtYEX.js.map +1 -0
  122. package/dist/{proxy-CH8Jsovg.js → proxy-BYir8tWK.js} +4 -6
  123. package/dist/proxy-BYir8tWK.js.map +1 -0
  124. package/dist/{query-parser-otFZqJsC.js → query-parser-CDXWf2hH.js} +6 -5
  125. package/dist/{query-parser-otFZqJsC.js.map → query-parser-CDXWf2hH.js.map} +1 -1
  126. package/dist/{request-timeout-59crS-9u.js → request-timeout-DVjo-c_8.js} +4 -4
  127. package/dist/request-timeout-DVjo-c_8.js.map +1 -0
  128. package/dist/rls-simulator-routes-bEY7-5-u.js +190 -0
  129. package/dist/rls-simulator-routes-bEY7-5-u.js.map +1 -0
  130. package/dist/schema-edit/deployed-schema-guard.d.ts +47 -0
  131. package/dist/{schema-editor-routes-Bsut0bCC.js → schema-editor-routes-Dc3DU_jm.js} +3 -3
  132. package/dist/{schema-editor-routes-Bsut0bCC.js.map → schema-editor-routes-Dc3DU_jm.js.map} +1 -1
  133. package/dist/services/routed-realtime-service.d.ts +1 -1
  134. package/dist/soft-delete-params-BRONCOA2.js +59 -0
  135. package/dist/soft-delete-params-BRONCOA2.js.map +1 -0
  136. package/dist/{sql-export-routes-DmB_uZ4l.js → sql-export-routes-QycX8W8S.js} +38 -4
  137. package/dist/sql-export-routes-QycX8W8S.js.map +1 -0
  138. package/dist/{src-D5Y3N-S4.js → src-Bh9LRPb9.js} +21 -2
  139. package/dist/{src-D5Y3N-S4.js.map → src-Bh9LRPb9.js.map} +1 -1
  140. package/dist/storage/GCSStorageController.d.ts +11 -1
  141. package/dist/storage/LocalStorageController.d.ts +16 -4
  142. package/dist/storage/S3StorageController.d.ts +6 -2
  143. package/dist/storage/index.d.ts +1 -0
  144. package/dist/storage/maintenance-keys.d.ts +17 -0
  145. package/dist/storage/maintenance-routes.d.ts +28 -5
  146. package/dist/storage/maintenance.d.ts +136 -4
  147. package/dist/storage/orphan-scans.d.ts +120 -0
  148. package/dist/storage/policies.d.ts +1 -1
  149. package/dist/storage/streams.d.ts +17 -0
  150. package/dist/storage/types.d.ts +26 -33
  151. package/dist/strings.es-B7QLnHh2.js +96 -0
  152. package/dist/strings.es-B7QLnHh2.js.map +1 -0
  153. package/dist/{upload-validation-Bgo626uY.js → upload-validation-CkkQGs-j.js} +40 -2
  154. package/dist/upload-validation-CkkQGs-j.js.map +1 -0
  155. package/dist/utils/load-failure.d.ts +9 -0
  156. package/package.json +5 -5
  157. package/dist/GCSStorageController-CRd9bFrc.js.map +0 -1
  158. package/dist/S3StorageController-D00EFa5P.js.map +0 -1
  159. package/dist/auth-Dkvo2tTJ.js.map +0 -1
  160. package/dist/auth_adapter-CIItCTc8.js +0 -21
  161. package/dist/auth_adapter-CIItCTc8.js.map +0 -1
  162. package/dist/backend-DTAOsLQc.js.map +0 -1
  163. package/dist/cron-loader-Dj6lA4HQ.js.map +0 -1
  164. package/dist/cron-scheduler-B04ACoUE.js.map +0 -1
  165. package/dist/cron-store-DkyfuLOH.js.map +0 -1
  166. package/dist/ddl-bootstrap-Ck20GSIX.js.map +0 -1
  167. package/dist/function-loader-KKDqTcIr.js.map +0 -1
  168. package/dist/history-recorder-B2rkuhuy.js.map +0 -1
  169. package/dist/history-store-DPofGVDp.js.map +0 -1
  170. package/dist/jobs-h1WRq3cp.js.map +0 -1
  171. package/dist/load-failure-L5hvfDoG.js.map +0 -1
  172. package/dist/offline-BrqCRgih-BtDB1ecy.js.map +0 -1
  173. package/dist/openapi-generator-BjHr6upl.js.map +0 -1
  174. package/dist/proxy-CH8Jsovg.js.map +0 -1
  175. package/dist/request-timeout-59crS-9u.js.map +0 -1
  176. package/dist/soft-delete-params-BmlVwecC.js.map +0 -1
  177. package/dist/sql-export-routes-DmB_uZ4l.js.map +0 -1
  178. package/dist/strings.es-BeZWAodn.js.map +0 -1
  179. package/dist/upload-validation-Bgo626uY.js.map +0 -1
@@ -1,7 +1,23 @@
1
1
  import type { OAuthProvider } from "./interfaces.js";
2
2
  import { type OAuthCodeFlowPayload } from "./oauth-code-flow.js";
3
- /** Extra field Apple's client sends that no other provider has. */
4
- export interface AppleCodeFlowPayload extends OAuthCodeFlowPayload {
3
+ /**
4
+ * The request body `POST /auth/apple` accepts.
5
+ *
6
+ * Apple mints a code for one of two kinds of client, and the token exchange
7
+ * has to name the same one:
8
+ * - **Web** — the code was issued to the Services ID through a browser
9
+ * authorization, so it carries the `redirectUri` that authorization used.
10
+ * - **Native** — the code came from `ASAuthorizationController` in an iOS,
11
+ * iPadOS or macOS app. It was issued to the app's bundle id and there was no
12
+ * redirect, so the client names the `bundleId` instead.
13
+ *
14
+ * Exactly one of the two is sent.
15
+ */
16
+ export interface AppleCodeFlowPayload extends Omit<OAuthCodeFlowPayload, "redirectUri"> {
17
+ /** Web sign-in: the redirect URI the browser authorization used. */
18
+ redirectUri?: string;
19
+ /** Native sign-in: the bundle id of the app that obtained the code. */
20
+ bundleId?: string;
5
21
  /**
6
22
  * Apple sends the user's *name* only on the first authorization, so the
7
23
  * frontend has to forward it. It is display data and nothing else — the
@@ -14,6 +30,24 @@ export interface AppleCodeFlowPayload extends OAuthCodeFlowPayload {
14
30
  };
15
31
  };
16
32
  }
33
+ export interface AppleProviderConfig {
34
+ /**
35
+ * The Services ID that web sign-in authorizes against. Optional when
36
+ * `bundleIds` is set: an app that only signs in from iOS has no Services ID.
37
+ */
38
+ clientId?: string;
39
+ /**
40
+ * Bundle ids of the native apps (iOS, iPadOS, macOS) whose codes this
41
+ * backend accepts. A native code is issued to the app, not to the Services
42
+ * ID, so each app is a client of its own. Apple's `sub` is per team, so the
43
+ * same person signing in on the web and in the app is one identity.
44
+ */
45
+ bundleIds?: string[];
46
+ teamId: string;
47
+ keyId: string;
48
+ /** The raw PEM contents of the .p8 private key file */
49
+ privateKey: string;
50
+ }
17
51
  /**
18
52
  * Creates an Apple Sign In OAuth Provider integration.
19
53
  *
@@ -21,15 +55,10 @@ export interface AppleCodeFlowPayload extends OAuthCodeFlowPayload {
21
55
  * token exchange (valid up to 6 months). This provider handles that automatically.
22
56
  *
23
57
  * Required Apple Developer configuration:
24
- * - Services ID (clientId)
58
+ * - Services ID (clientId) for web sign-in, and/or the bundle ids of the
59
+ * native apps (bundleIds)
25
60
  * - Key ID from the private key registered with Apple
26
61
  * - Team ID from Apple Developer account
27
62
  * - Private key (.p8 file contents) downloaded from Apple Developer portal
28
63
  */
29
- export declare function createAppleProvider(config: {
30
- clientId: string;
31
- teamId: string;
32
- keyId: string;
33
- /** The raw PEM contents of the .p8 private key file */
34
- privateKey: string;
35
- }): OAuthProvider<AppleCodeFlowPayload>;
64
+ export declare function createAppleProvider(config: AppleProviderConfig): OAuthProvider<AppleCodeFlowPayload>;
@@ -40,6 +40,8 @@ export interface BuiltinAuthCapabilityInputs {
40
40
  emailConfigured: boolean;
41
41
  /** OAuth provider ids the backend has wired, e.g. `["google", "github"]`. */
42
42
  enabledProviders: string[];
43
+ /** Public client ids by provider id, for the providers that publish one. */
44
+ providerClientIds?: Record<string, string>;
43
45
  }
44
46
  /**
45
47
  * Build the capability document for the built-in auth adapter.
@@ -59,6 +59,11 @@ export interface ExpiredTokenSweepWiring {
59
59
  ownsTimers: boolean;
60
60
  /** `cronPersistence: false` keeps the claims table out of the database, here too. */
61
61
  persistClaims?: boolean;
62
+ /**
63
+ * `false`: the backend changes nothing in its database, so the claims
64
+ * table is used if it is there and never created by the first tick.
65
+ */
66
+ provision?: boolean;
62
67
  }
63
68
  /**
64
69
  * Start the sweep where it belongs, or answer `undefined` where it does not:
@@ -94,7 +94,9 @@ export interface ImpersonationOptions {
94
94
  */
95
95
  export declare function applyImpersonation(c: Context<HonoEnv>, { credential, driver, resolveUser }: ImpersonationOptions): Promise<Response | undefined>;
96
96
  /**
97
- * Refuse {@link IMPERSONATE_HEADER} on every route that does not honour it.
97
+ * Refuse {@link IMPERSONATE_HEADER} — and the RLS simulator's
98
+ * `x-rebase-simulate-*` headers (`auth/simulation.ts`) — on every route that
99
+ * does not honour it.
98
100
  *
99
101
  * Only the data API and custom functions run a request as another user; the
100
102
  * rest — storage, auth, the admin surfaces — authenticate the caller and
@@ -95,6 +95,14 @@ export interface OAuthProvider<T = unknown> {
95
95
  id: string;
96
96
  /** Zod schema validating the expected request payload (e.g. { code: string }) */
97
97
  schema: z.ZodSchema<T>;
98
+ /**
99
+ * The client id a browser needs to *start* this provider's sign-in, when
100
+ * it is public. Published in `GET /auth/config` as
101
+ * `providerClientIds[id]`, so a login screen can offer the button from
102
+ * the backend's configuration alone rather than from a second copy of the
103
+ * id in the frontend's environment. Never set it to anything secret.
104
+ */
105
+ publicClientId?: string;
98
106
  /**
99
107
  * Verify external tokens/codes and return a standardized user profile.
100
108
  *
@@ -257,6 +265,32 @@ export interface PaginatedUsersResult {
257
265
  limit: number;
258
266
  offset: number;
259
267
  }
268
+ /** The window and filter of {@link UserRepository.listGuests}. */
269
+ export interface ListGuestsOptions {
270
+ /** Max results per page (default 25) */
271
+ limit?: number;
272
+ /** Number of results to skip (default 0) */
273
+ offset?: number;
274
+ /** Only the guests last seen before this instant. */
275
+ lastSeenBefore?: Date;
276
+ }
277
+ /** A row anonymous sign-in made, that has not become an account. */
278
+ export interface GuestData {
279
+ id: string;
280
+ createdAt: Date;
281
+ /**
282
+ * When its newest refresh token was issued: a session refreshing rotates
283
+ * its token, so this moves while a guest keeps coming back. `createdAt`
284
+ * when no token is left.
285
+ */
286
+ lastSeenAt: Date;
287
+ }
288
+ /** Result of {@link UserRepository.listGuests}. */
289
+ export interface PaginatedGuestsResult {
290
+ guests: GuestData[];
291
+ /** How many guests match, ignoring limit and offset. */
292
+ total: number;
293
+ }
260
294
  /**
261
295
  * Abstract user repository interface.
262
296
  * Handles all user-related database operations.
@@ -334,6 +368,27 @@ export interface UserRepository {
334
368
  * instead (`isUserTableEmpty` / `isOnlyUser` in `registration-policy.ts`).
335
369
  */
336
370
  firstUserIds?(limit: number): Promise<string[]>;
371
+ /**
372
+ * The guests, the ones last seen longest ago first — a stable order, for
373
+ * the same `lastSeenBefore` and offset.
374
+ *
375
+ * A guest is `isAnonymous` with no way to sign back in: no password and no
376
+ * linked identity. The flag alone is not enough, because a row can carry it
377
+ * and still belong to someone who signs in.
378
+ *
379
+ * Optional, with {@link deleteGuest}: the admin guest routes answer 501 on
380
+ * a repository without them.
381
+ */
382
+ listGuests?(options?: ListGuestsOptions): Promise<PaginatedGuestsResult>;
383
+ /**
384
+ * Delete this user if it is a guest, as {@link listGuests} means it, and
385
+ * say whether it did. The statement that deletes is what decides, so an
386
+ * account can never go through here, whatever the caller read a moment ago.
387
+ *
388
+ * A row of the project's own that requires the guest (`ON DELETE RESTRICT`)
389
+ * is a 409 `GUEST_REFERENCED`.
390
+ */
391
+ deleteGuest?(id: string): Promise<boolean>;
337
392
  /**
338
393
  * Update user's password hash. `null` removes the password: the account
339
394
  * then signs in only by link, code or provider.
@@ -318,7 +318,7 @@ export interface JwtService {
318
318
  * it ever needs to verify one, and an asymmetric key here would be a
319
319
  * second key surface bought for nobody.
320
320
  */
321
- generateMcpAccessToken(payload: Omit<McpAccessTokenPayload, "purpose">, expiresInSeconds: number): Promise<string>;
321
+ generateMcpAccessToken(payload: Omit<McpAccessTokenPayload, "purpose" | "iat">, expiresInSeconds: number): Promise<string>;
322
322
  /**
323
323
  * Verify an MCP access token **for a specific resource**.
324
324
  *
@@ -484,9 +484,15 @@ export interface McpAccessTokenPayload {
484
484
  /** The resource this token is bound to (RFC 8707). */
485
485
  aud: string;
486
486
  iss: string;
487
+ /**
488
+ * When it was signed, in whole seconds. Set on a verified token only; the
489
+ * signer stamps it itself. `/mcp` places it against the account's
490
+ * revocation watermark, as every other door does a session token.
491
+ */
492
+ iat?: number;
487
493
  }
488
494
  /** See {@link JwtService.generateMcpAccessToken}. Answers for the process default. */
489
- export declare function generateMcpAccessToken(payload: Omit<McpAccessTokenPayload, "purpose">, expiresInSeconds: number): Promise<string>;
495
+ export declare function generateMcpAccessToken(payload: Omit<McpAccessTokenPayload, "purpose" | "iat">, expiresInSeconds: number): Promise<string>;
490
496
  /** See {@link JwtService.verifyMcpAccessToken}. Answers for the process default. */
491
497
  export declare function verifyMcpAccessToken(token: string, expectedAudience: string): Promise<McpAccessTokenPayload | null>;
492
498
  /**
@@ -6,25 +6,17 @@
6
6
  /** Why an incoming OAuth identity may not be auto-attached to an existing account. */
7
7
  export type AutoLinkRefusal =
8
8
  /** The provider did not report that it verified the address. */
9
- "provider-email-unverified"
10
- /**
11
- * The local account holds a password nobody ever proved they own: it was
12
- * created through `POST /auth/register`, which does not verify the
13
- * address. Attaching a provider identity to it would hand the session to
14
- * whoever registered the address first — the classic pre-hijack.
15
- */
16
- | "local-account-unverified"
17
- /**
18
- * The local account has no password, and its address was never proven
19
- * either: it was made by a sign-in through a provider that did not vouch
20
- * for the address, or by someone who never had to. Its sign-in methods are
21
- * whatever that someone attached, so this is the same pre-hijack as a
22
- * password would be. Reported apart from `local-account-unverified` only
23
- * so a login screen does not tell its owner to use a password.
24
- */
25
- | "local-account-unverified-passwordless";
9
+ "provider-email-unverified";
26
10
  export type AutoLinkDecision = {
27
11
  allowed: true;
12
+ /**
13
+ * The local account's address was never proven, and this sign-in
14
+ * proves it. The route settles the account the way an email code does
15
+ * (`confirmAddressOwnership`) before it links the identity: the
16
+ * password and every identity whose provider did not vouch for the
17
+ * address go, and every session ends.
18
+ */
19
+ provesAddress: boolean;
28
20
  } | {
29
21
  allowed: false;
30
22
  reason: AutoLinkRefusal;
@@ -37,25 +29,19 @@ export interface AutoLinkExistingUser {
37
29
  /**
38
30
  * May an OAuth identity be attached to a pre-existing account found *by email*?
39
31
  *
40
- * Both sides have to be trustworthy:
41
- *
42
- * - the **provider** must have verified the address, or the caller has not
43
- * shown they control it;
44
- * - the **local account**'s address must have been verified too. Whoever
45
- * made an unverified account never proved the address, and every way in
46
- * they left on it — a password, an identity from a provider that did not
47
- * vouch for the address — would go on opening the account the owner is
48
- * about to be signed into.
49
- *
50
- * The second rule used to exempt an account with no password, on the theory
51
- * that one made by an OAuth sign-in holds no credential an attacker could have
52
- * planted. It holds exactly one: the identity it was made with, which is an
53
- * attacker's whenever the provider did not vouch for the address.
32
+ * Only when the **provider** verified the address. Otherwise the caller has
33
+ * not shown they control it, and linking would hand them the account.
54
34
  *
55
- * A refusal is not a dead end. `POST /auth/link/<provider>` attaches the
56
- * identity once the caller proves ownership by holding a session, and proving
57
- * the address (a magic link, an email code, a password reset) verifies the
58
- * account — removing what nobody proved — after which this answers yes.
35
+ * When it did, the caller has proven the address exactly as reading a code out
36
+ * of the inbox does: only the inbox's owner can hold a provider account that
37
+ * vouches for it. If the **local account**'s address was never verified,
38
+ * whoever made it never proved the address, and every way in they left on it —
39
+ * a password, an identity from a provider that did not vouch for the address,
40
+ * a live session — would go on opening the account the owner is about to be
41
+ * signed into. So that case answers `provesAddress: true`, and the route
42
+ * removes those ways in before it links (the pre-hijack defence). It used to
43
+ * refuse instead, which left the owner of the address locked out of their own
44
+ * address until they found a magic link or a reset.
59
45
  */
60
46
  export declare function decideOAuthAutoLink(args: {
61
47
  providerEmailVerified: boolean | undefined;
@@ -11,7 +11,7 @@
11
11
  *
12
12
  * @module
13
13
  */
14
- import type { DataDriver } from "@rebasepro/types";
14
+ import type { CallerCredential, DataDriver } from "@rebasepro/types";
15
15
  /**
16
16
  * The identity the trusted server plane runs as: `rebase.dataAsAdmin`, and any
17
17
  * caller presenting the service key.
@@ -37,6 +37,21 @@ export declare const SERVICE_IDENTITY: {
37
37
  uid: string;
38
38
  roles: string[];
39
39
  };
40
+ /**
41
+ * Who a request runs as, as a driver is scoped to it.
42
+ *
43
+ * `credential` is the API key the request authenticated with, when it was one.
44
+ * It changes nothing the database sees; the driver hands it to collection
45
+ * callbacks as `context.credential`, so a personal key's write can be told
46
+ * from its owner's own.
47
+ */
48
+ export interface ScopeIdentity {
49
+ uid: string;
50
+ roles?: string[];
51
+ isAnonymous?: boolean;
52
+ claims?: Record<string, unknown>;
53
+ credential?: CallerCredential;
54
+ }
40
55
  /**
41
56
  * Can this driver run a request as a specific user?
42
57
  *
@@ -71,9 +86,4 @@ export declare function scopeDataDriver(driver: DataDriver,
71
86
  * without this the database sees the same principal either way. See
72
87
  * `rebase.is_anonymous()`.
73
88
  */
74
- user: {
75
- uid: string;
76
- roles?: string[];
77
- isAnonymous?: boolean;
78
- claims?: Record<string, unknown>;
79
- }): Promise<DataDriver>;
89
+ user: ScopeIdentity): Promise<DataDriver>;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The RLS simulator's admin surface, under `/api/admin/rls-simulator`.
3
+ *
4
+ * The data API answers one collection at a time for a simulated identity
5
+ * (`auth/simulation.ts`). This answers the questions the admin panel's
6
+ * "Simulate access" bar asks about all of them at once:
7
+ *
8
+ * - `GET /options` — what can be simulated: the roles the project names (the
9
+ * access model's, the ones its row and field rules mention, and `anon`) and
10
+ * the API keys that are live.
11
+ * - `POST /preview` — for `{ role?, userId?, apiKeyId? }`, each collection's
12
+ * rows as that identity sees them against the caller's own view, whether a
13
+ * key's scopes shut it out, and which fields its roles cannot read.
14
+ *
15
+ * The same decision as the data API: only an administrator's own session, a
16
+ * 403 for anyone else.
17
+ *
18
+ * @module
19
+ */
20
+ import { Hono } from "hono";
21
+ import { type CollectionConfig, type DataDriver } from "@rebasepro/types";
22
+ import type { HonoEnv } from "../api/types.js";
23
+ import type { ApiKeyStore } from "./api-keys/api-key-store.js";
24
+ import type { ImpersonationUserResolver } from "./impersonation.js";
25
+ export interface RlsSimulatorRouteConfig {
26
+ /** The collections this backend serves, as they are now. */
27
+ collections(): readonly CollectionConfig[];
28
+ /** The unscoped driver a collection's rows live in. */
29
+ driverFor(slug: string): DataDriver;
30
+ resolveUser?: ImpersonationUserResolver;
31
+ apiKeyStore?: ApiKeyStore;
32
+ }
33
+ /** One collection, as the simulated identity reaches it. */
34
+ export interface SimulatedCollectionView {
35
+ slug: string;
36
+ name: string;
37
+ /**
38
+ * Why the identity sees nothing here: its key's scopes, row policies
39
+ * hiding every row, or a read that fails for it (`error` says why).
40
+ */
41
+ hiddenBy?: "scope" | "rls" | "error";
42
+ /** Rows the simulated identity reads. Absent when not counted. */
43
+ visibleRows?: number;
44
+ /** Rows the administrator asking reads. */
45
+ totalRows?: number;
46
+ /** Fields the identity's roles cannot read. */
47
+ maskedColumns: string[];
48
+ /** Why the rows could not be counted. */
49
+ error?: string;
50
+ }
51
+ export declare function createRlsSimulatorRoutes(config: RlsSimulatorRouteConfig): Hono<HonoEnv>;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * "Simulate access" is read-only.
3
+ *
4
+ * The simulator answers "what does this role, user or key see". A write made
5
+ * while simulating ran as the simulated identity for real: it committed as
6
+ * `rebase_user` with `app.uid` set to the simulated uid, `entity_history`
7
+ * recorded that uid as the author, callbacks saw `context.user` holding a role
8
+ * the account may not even have, and a role-only simulation stamped owner
9
+ * columns with the nobody uid. Only the request log named the administrator.
10
+ * So every door that honours the simulation headers refuses anything but a
11
+ * read, naming the simulation, rather than performing it as someone else.
12
+ *
13
+ * Its own module so the REST generator, history routes and the functions
14
+ * guard can share it without importing the simulator itself.
15
+ *
16
+ * @module
17
+ */
18
+ import type { DataOperation } from "./api-keys/http-operation.js";
19
+ /**
20
+ * Refuse a write or delete on a request that simulates access.
21
+ *
22
+ * `c` is anything with the request context's `get` — the REST generator calls
23
+ * this with a per-operation stand-in for each operation of a `_batch`.
24
+ * `operation` is what the request does, not its verb: `POST …/query` is a read.
25
+ */
26
+ export declare function refuseSimulatedWrite(c: {
27
+ get: (key: string) => unknown;
28
+ }, operation: DataOperation): void;
@@ -0,0 +1,141 @@
1
+ /**
2
+ * The RLS simulator: an administrator's data-plane request run as a role, a
3
+ * user or a service key — `x-rebase-simulate-role`, `x-rebase-simulate-user-id`
4
+ * and `x-rebase-simulate-api-key-id`.
5
+ *
6
+ * Impersonation ({@link ./impersonation}) answers "what does this person see";
7
+ * this answers the question an author asks while writing policies, before any
8
+ * such person exists: "what does a `customer` see, what does `u_1` holding
9
+ * `customer` see, what does the support agent's key see". So a role need not be
10
+ * held by anyone and a uid sent with a role need not name an account. Nothing
11
+ * here widens what the caller could do: the caller is an administrator, whom
12
+ * the default policies already let read and write every row, and every
13
+ * simulated identity is narrower.
14
+ *
15
+ * The identity reaches the database exactly as a real caller's does —
16
+ * `scopeDataDriver` → `withAuth` → `applyAuthContext`, which sets the
17
+ * transaction-local identity (`app.uid`, `app.roles`, `app.jwt`) and switches to
18
+ * `rebase_user`, so every policy is evaluated for it. A simulated key also
19
+ * carries its scopes, so the data API refuses the collections the key cannot
20
+ * reach, as it would refuse the key. Fields the identity's roles cannot read
21
+ * are stripped by the driver as for any caller; {@link reportSimulatedContext}
22
+ * names them on the response.
23
+ *
24
+ * Who may: a signed-in administrator on their own session, judged on their
25
+ * roles as the database has them now. Never an API key, never the service
26
+ * key, never anyone else — a 403, never an answer as themselves.
27
+ *
28
+ * Where: the data API and custom functions, like impersonation. Every other
29
+ * route refuses the headers (`refuseUnhonouredImpersonation`).
30
+ *
31
+ * Read-only: a write, delete, batch, revert or non-GET function call that
32
+ * carries the headers is refused (`SIMULATION_READ_ONLY`, see
33
+ * {@link ./simulation-read-only}) rather than run as the simulated identity.
34
+ *
35
+ * Not simulated: the data API's `requireAuth` gate. A simulated anonymous
36
+ * caller is let past it, so the answer is what the rows' policies give an
37
+ * anonymous caller — a 401 there would send the admin panel to refresh a
38
+ * session that is fine. Everywhere else it is no session, as a real anonymous
39
+ * caller is: it carries no `user`, so a function's `requireAuth` refuses it.
40
+ *
41
+ * @module
42
+ */
43
+ import type { Context, MiddlewareHandler } from "hono";
44
+ import { type AccessSimulation, type ApiKeyMasked, type CollectionConfig, type DataDriver } from "@rebasepro/types";
45
+ import type { HonoEnv } from "../api/types.js";
46
+ import { ApiError } from "../api/errors.js";
47
+ import type { ImpersonationCredential, ImpersonationUserResolver } from "./impersonation.js";
48
+ import type { ApiKeyStore } from "./api-keys/api-key-store.js";
49
+ import { type AccessControl } from "./access.js";
50
+ /**
51
+ * The uid a role simulated on its own runs as: a valid UUID that no row
52
+ * names, so an ownership policy matches nothing and a policy comparing a
53
+ * `uuid` column to `auth.uid()` still casts cleanly.
54
+ */
55
+ export declare const SIMULATED_NOBODY_UID = "00000000-0000-0000-0000-000000000000";
56
+ /** Who a granted simulation runs as. */
57
+ export interface SimulatedIdentity {
58
+ uid: string;
59
+ roles: string[];
60
+ isAnonymous: boolean;
61
+ claims?: Record<string, unknown>;
62
+ /** A key's scopes. Absent for a person, whose data plane is RLS's to decide. */
63
+ scopes?: string[];
64
+ apiKey?: ApiKeyMasked;
65
+ /** What the response header names: the role (or roles) and the uid. */
66
+ role: string;
67
+ }
68
+ export interface SimulationRequest {
69
+ simulation: AccessSimulation;
70
+ /** What the caller presented. Only a `session` may simulate. */
71
+ credential: ImpersonationCredential;
72
+ caller?: {
73
+ uid: string;
74
+ roles: readonly string[];
75
+ };
76
+ /** Re-reads the caller's roles, and finds a user named without a role. */
77
+ resolveUser?: ImpersonationUserResolver;
78
+ /** Finds a key named by id. Absent: a key simulation is refused. */
79
+ apiKeyStore?: ApiKeyStore;
80
+ /** Whose access model a personal key's owner is judged against. */
81
+ access?: AccessControl;
82
+ /** Where the request came in, for the audit line; absent writes none. */
83
+ audit?: Record<string, unknown>;
84
+ }
85
+ export type SimulationDecision = {
86
+ granted: SimulatedIdentity;
87
+ simulator: string;
88
+ } | {
89
+ refused: ApiError;
90
+ };
91
+ /** Read the three simulation headers, or `undefined` when none was sent. */
92
+ export declare function simulationOf(header: (name: string) => string | undefined): AccessSimulation | undefined;
93
+ /** Whether a request carries any simulation header. */
94
+ export declare function carriesSimulation(header: (name: string) => string | undefined): boolean;
95
+ /**
96
+ * Decide whether this caller may simulate what they asked for, and who the
97
+ * request then runs as.
98
+ *
99
+ * The caller is judged before anything they named is looked up, so a refusal
100
+ * says nothing about whether a user or key exists. Throws when a lookup fails.
101
+ */
102
+ export declare function decideSimulation(request: SimulationRequest): Promise<SimulationDecision>;
103
+ export interface SimulationOptions {
104
+ credential: ImpersonationCredential;
105
+ /** The unscoped delegate the request's driver was scoped from. */
106
+ driver: DataDriver;
107
+ resolveUser?: ImpersonationUserResolver;
108
+ apiKeyStore?: ApiKeyStore;
109
+ }
110
+ /** The caller the auth middleware put on the context, narrowed. */
111
+ export declare function callerOf(c: Context<HonoEnv>): {
112
+ uid: string;
113
+ roles: string[];
114
+ } | undefined;
115
+ /**
116
+ * Honour or refuse this request's simulation headers.
117
+ *
118
+ * Called by the data-plane auth middlewares once the caller is on the context
119
+ * and before impersonation, which it excludes. On a grant the context's
120
+ * `user`, `driver`, `scopes` and `apiKey` become the simulated identity's,
121
+ * `impersonator` names the administrator for the request log, and
122
+ * `simulation` records what to report on the response.
123
+ *
124
+ * @returns The refusal to send, or `undefined` to carry on.
125
+ */
126
+ export declare function applySimulation(c: Context<HonoEnv>, { credential, driver, resolveUser, apiKeyStore }: SimulationOptions): Promise<Response | undefined>;
127
+ /**
128
+ * The fields of `collection` the roles cannot read: what the driver strips
129
+ * from every row it hands them. Fields hidden from everyone (`excludeFromApi`)
130
+ * are left out — no role sees those, so they say nothing about this one.
131
+ */
132
+ export declare function maskedFieldsFor(collection: CollectionConfig, roles: readonly string[]): string[];
133
+ /**
134
+ * Name what a simulated request ran as on its response
135
+ * ({@link SIMULATED_CONTEXT_HEADER}), refusals included.
136
+ *
137
+ * Mounted ahead of the data-plane groups; it reads, after the request is
138
+ * answered, what {@link applySimulation} and the REST generator recorded on
139
+ * the context — the identity, and which collection the request was about.
140
+ */
141
+ export declare function reportSimulatedContext(): MiddlewareHandler<HonoEnv>;
@@ -46,6 +46,12 @@ import type { RateLimitStore } from "./rate-limit-store.js";
46
46
  export interface SqlRateLimitStoreOptions {
47
47
  /** How often to delete expired buckets. Defaults to 5 minutes. */
48
48
  sweepMs?: number;
49
+ /**
50
+ * Default `true`. `false` is a backend that changes nothing in its
51
+ * database: the table is used if it is there, never created, and counts
52
+ * stay per-process without it.
53
+ */
54
+ provision?: boolean;
49
55
  /**
50
56
  * Clock, for tests. Driving the window directly beats sleeping through half
51
57
  * of one — a real sleep overruns on a loaded machine and takes the hits it
@@ -60,4 +66,12 @@ export interface SqlRateLimitStoreOptions {
60
66
  * deployment on a driver with no SQL admin should degrade to per-process counts
61
67
  * with a warning, not refuse to serve.
62
68
  */
63
- export declare function createSqlRateLimitStore(driver: DataDriver, options?: SqlRateLimitStoreOptions): RateLimitStore | undefined;
69
+ /** The shared store, plus the one step a provisioning run takes ahead of time. */
70
+ export interface SqlRateLimitStore extends RateLimitStore {
71
+ /**
72
+ * Create the table now rather than on the first counted request — what a
73
+ * provisioning run calls. Resolves to whether it is usable.
74
+ */
75
+ ensureTable(): Promise<boolean>;
76
+ }
77
+ export declare function createSqlRateLimitStore(driver: DataDriver, options?: SqlRateLimitStoreOptions): SqlRateLimitStore | undefined;