@engenty/wizards-runtime 0.11.11 → 0.11.13

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 (58) hide show
  1. package/apps/runtime/dist/app.js +2 -1
  2. package/apps/runtime/dist/auth/impersonation.js +33 -0
  3. package/apps/runtime/dist/auth/index.js +44 -7
  4. package/apps/runtime/dist/db/control-schema.js +3 -0
  5. package/apps/runtime/dist/db/migrations/control/0013_session_impersonated_by.sql +2 -0
  6. package/apps/runtime/dist/db/migrations/control/meta/0013_snapshot.json +621 -0
  7. package/apps/runtime/dist/db/migrations/control/meta/_journal.json +7 -0
  8. package/apps/runtime/dist/db/migrations/tenant/0026_run_impersonated_by.sql +1 -0
  9. package/apps/runtime/dist/db/migrations/tenant/meta/0026_snapshot.json +3966 -0
  10. package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +7 -0
  11. package/apps/runtime/dist/db/schema.js +2 -0
  12. package/apps/runtime/dist/engine/runner.js +2 -0
  13. package/apps/runtime/dist/manage.js +14 -0
  14. package/apps/runtime/dist/routes/runs.js +18 -7
  15. package/apps/runtime/dist/routes/studio.js +4 -0
  16. package/apps/runtime/dist/services/runs.js +1 -0
  17. package/apps/runtime/dist/sharing/index.js +1 -0
  18. package/apps/runtime/dist/texts.js +2 -0
  19. package/apps/web/dist/assets/{AboutDialog-BxPhyBQC.js → AboutDialog-pMHleH_y.js} +1 -1
  20. package/apps/web/dist/assets/{ChangelogDialog-BLst4UYt.js → ChangelogDialog-Cxwkcjdy.js} +1 -1
  21. package/apps/web/dist/assets/{CodeEditor-ydfkafOX.js → CodeEditor-BijLsoJX.js} +1 -1
  22. package/apps/web/dist/assets/{EditorPage-tBrPWV2R.js → EditorPage-v7cpENx3.js} +3 -3
  23. package/apps/web/dist/assets/{FlowPreview-CPEK110v.js → FlowPreview-Wjt28MdG.js} +1 -1
  24. package/apps/web/dist/assets/{Harness-Cm1i8s2W.js → Harness-TUpGhGX5.js} +1 -1
  25. package/apps/web/dist/assets/{Marketplace-_IfcAgYO.js → Marketplace-CpjRqTvn.js} +2 -2
  26. package/apps/web/dist/assets/{NewWizardChannels-45GyOXYL.js → NewWizardChannels-CelW1MGq.js} +1 -1
  27. package/apps/web/dist/assets/{NewWizardPage-XFt4YtU3.js → NewWizardPage-Coswm2WX.js} +1 -1
  28. package/apps/web/dist/assets/{OnboardingPage-Mko1anYV.js → OnboardingPage-C9XM_VXs.js} +1 -1
  29. package/apps/web/dist/assets/{PreviewPane-Dw9WG3lP.js → PreviewPane-BUwlltqA.js} +5 -5
  30. package/apps/web/dist/assets/{ProjectPage-DRnVIWQs.js → ProjectPage-Cw7hdXO6.js} +1 -1
  31. package/apps/web/dist/assets/PublicRunner-B5RGNs07.js +1 -0
  32. package/apps/web/dist/assets/{ReadOnly-CBNxkYUt.js → ReadOnly-DZMZPM-W.js} +1 -1
  33. package/apps/web/dist/assets/SettingsPage-CfL1e3Vs.js +2 -0
  34. package/apps/web/dist/assets/SetupPage-BRPenYa-.js +1 -0
  35. package/apps/web/dist/assets/{SharePage-gjQQ0CtP.js → SharePage-DF8LO8-K.js} +1 -1
  36. package/apps/web/dist/assets/changelog-CO6ysfQg.js +1 -0
  37. package/apps/web/dist/assets/{i18n-BX8GnEZT.js → i18n-BYeH2Dmi.js} +1 -1
  38. package/apps/web/dist/assets/index-Bl7qOZv0.js +34 -0
  39. package/apps/web/dist/assets/{phone-Dd7IVbpJ.js → phone-JGS60FQU.js} +1 -1
  40. package/apps/web/dist/assets/{runners-XifDCQG3.js → runners-CwTP03YR.js} +1 -1
  41. package/apps/web/dist/assets/{session-BwtsznZF.js → session-CngLvd02.js} +1 -1
  42. package/apps/web/dist/assets/{settings-menu-CS-fXrFW.js → settings-menu-C0yo5q3t.js} +1 -1
  43. package/apps/web/dist/assets/{theme-DFKNqmEY.js → theme-D6QLPvJL.js} +2 -2
  44. package/apps/web/dist/assets/{ui-D719V_Yj.js → ui-Bhp_a_44.js} +1 -1
  45. package/apps/web/dist/assets/{upload-CYTDCrYd.js → upload-DPLDQsRT.js} +1 -1
  46. package/apps/web/dist/assets/{useMutation-CVVmJvyz.js → useMutation-Cmn8b7tT.js} +1 -1
  47. package/apps/web/dist/assets/{volume-2-Do0dsjv2.js → volume-2-DGqbhyPO.js} +1 -1
  48. package/apps/web/dist/assets/{wrench-C5syJD9J.js → wrench-Dr19y-10.js} +1 -1
  49. package/apps/web/dist/index.html +7 -7
  50. package/apps/web/dist/mcp/flow.html +1 -1
  51. package/package.json +1 -1
  52. package/plugin/.claude-plugin/plugin.json +1 -1
  53. package/plugin/plugin.json +1 -1
  54. package/apps/web/dist/assets/PublicRunner-C3NwpZKh.js +0 -1
  55. package/apps/web/dist/assets/SettingsPage-DBckGRCS.js +0 -2
  56. package/apps/web/dist/assets/SetupPage-BJxbmS68.js +0 -1
  57. package/apps/web/dist/assets/changelog-C67l3TZ1.js +0 -1
  58. package/apps/web/dist/assets/index-BtNnNYyZ.js +0 -34
@@ -9,6 +9,7 @@ import { Hono } from "hono";
9
9
  import { bodyLimit } from "hono/body-limit";
10
10
  import { HTTPException } from "hono/http-exception";
11
11
  import { ZodError } from "zod";
12
+ import { asImpersonator } from "./auth/impersonation.js";
12
13
  import { authRoutes, principalOf } from "./auth/index.js";
13
14
  import { db, schema, withTenant } from "./db/client.js";
14
15
  import { basePath, env } from "./env.js";
@@ -251,7 +252,7 @@ if (serves.studio) {
251
252
  // A save: open studios hear of it now, not at the next tick (live/hub.ts).
252
253
  const writes = c.req.method !== "GET";
253
254
  if (managed) {
254
- await withTenant(user.tenantId, () => asRole(user.role, next));
255
+ await withTenant(user.tenantId, () => asImpersonator(user.impersonatedBy?.id, requestLang(c), () => asRole(user.role, next)));
255
256
  if (writes) {
256
257
  nudge(user.tenantId);
257
258
  }
@@ -0,0 +1,33 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ const store = new AsyncLocalStorage();
3
+ /** Runs `fn` as the admin signed in as someone; without an admin, as it is. */
4
+ export function asImpersonator(adminId, lang, fn) {
5
+ return adminId ? store.run({ adminId, lang }, fn) : fn();
6
+ }
7
+ /** The admin signed in as the person this request is for; null for the person themselves. */
8
+ export function currentImpersonator() {
9
+ return store.getStore() ?? null;
10
+ }
11
+ /**
12
+ * The Manage-App's calls that change the person's account or their organization: people,
13
+ * address, domains, API keys, name, connected apps and computers, the organization's name and
14
+ * profile, the install mail, phone numbers — and the invoices, which carry payment details.
15
+ */
16
+ const OFF = [
17
+ [/^(POST|DELETE)$/, /^\/v1\/tenants\/[^/]+\/invitations(\/|$)/],
18
+ [/^(PATCH|DELETE)$/, /^\/v1\/tenants\/[^/]+\/members\//],
19
+ [/^PUT$/, /^\/v1\/tenants\/[^/]+\/address$/],
20
+ [/^(POST|DELETE)$/, /^\/v1\/tenants\/[^/]+\/domains(\/|$)/],
21
+ [/^(POST|DELETE)$/, /^\/v1\/tenants\/[^/]+\/keys(\/|$)/],
22
+ [/^GET$/, /^\/v1\/tenants\/[^/]+\/invoices\//],
23
+ [/^(PATCH|PUT)$/, /^\/v1\/tenants\/[^/]+(\/profile)?$/],
24
+ [/^(POST|DELETE)$/, /^\/v1\/tenants\/[^/]+\/channels\/requests$/],
25
+ [/^PATCH$/, /^\/v1\/users\/[^/]+$/],
26
+ [/^DELETE$/, /^\/v1\/users\/[^/]+\/(connections|computers)\//],
27
+ [/^POST$/, /^\/v1\/install-mail$/],
28
+ ];
29
+ /** Whether a call to the Manage-App stays off while an admin is signed in as the person. */
30
+ export function refusedWhileImpersonating(method, path) {
31
+ const bare = path.split("?")[0] ?? path;
32
+ return OFF.some(([m, p]) => m.test(method) && p.test(bare));
33
+ }
@@ -102,13 +102,14 @@ function safeReturn(path) {
102
102
  : path === start || path.startsWith(`${start}/`) || path.startsWith(`${start}?`));
103
103
  return into ? path : home;
104
104
  }
105
- function setSessionCookie(c, id) {
105
+ /** The session's cookie; one that ends at `until` (an admin's sign-in as someone) ends then. */
106
+ function setSessionCookie(c, id, until) {
106
107
  setCookie(c, SESSION_COOKIE, id, {
107
108
  httpOnly: true,
108
109
  sameSite: "Lax",
109
110
  secure,
110
111
  path: cookiePath,
111
- maxAge: SESSION_DAYS * 24 * 3600,
112
+ maxAge: until ? Math.max(0, Math.floor((until - Date.now()) / 1000)) : SESSION_DAYS * 24 * 3600,
112
113
  });
113
114
  }
114
115
  async function claimsOf(tokens) {
@@ -118,6 +119,18 @@ async function claimsOf(tokens) {
118
119
  }
119
120
  return claims;
120
121
  }
122
+ /**
123
+ * An admin's sign-in as someone asks the Manage-App again every minute: ended there (on the
124
+ * account pages, in the admin app, at the hour), it ends here within a minute, not when the
125
+ * access token runs out.
126
+ */
127
+ const IMPERSONATION_RECHECK_MS = 60_000;
128
+ /** When the session's claims must be renewed: when the token runs out, sooner for an admin's sign-in as someone. */
129
+ function renewBy(claims) {
130
+ return claims.impersonatedBy
131
+ ? Math.min(claims.expiresAt, Date.now() + IMPERSONATION_RECHECK_MS)
132
+ : claims.expiresAt;
133
+ }
121
134
  /** Renews tenant and role from the Manage-App once the access token they came from has run out. */
122
135
  async function renew(row) {
123
136
  const refresh = row.refreshToken ? unseal(row.refreshToken) : null;
@@ -127,6 +140,10 @@ async function renew(row) {
127
140
  try {
128
141
  const tokens = await tokenRequest({ grant_type: "refresh_token", refresh_token: refresh, resource: env.appUrl }, client());
129
142
  const claims = await claimsOf(tokens);
143
+ // An admin's sign-in as this person renews as that or not at all.
144
+ if ((claims.impersonatedBy?.id ?? null) !== row.impersonatedBy) {
145
+ return null;
146
+ }
130
147
  const next = {
131
148
  ...row,
132
149
  tenantId: claims.tenantId,
@@ -134,7 +151,7 @@ async function renew(row) {
134
151
  name: claims.name || row.name,
135
152
  email: claims.email || row.email,
136
153
  refreshToken: tokens.refresh_token ? seal(tokens.refresh_token) : row.refreshToken,
137
- claimsExpireAt: new Date(claims.expiresAt),
154
+ claimsExpireAt: new Date(renewBy(claims)),
138
155
  };
139
156
  await controlDb.update(control.session).set(next).where(eq(control.session.id, row.id));
140
157
  return next;
@@ -169,6 +186,13 @@ async function sessionPrincipal(id) {
169
186
  name: row.name,
170
187
  email: row.email,
171
188
  image: row.image,
189
+ impersonatedBy: row.impersonatedBy
190
+ ? {
191
+ id: row.impersonatedBy,
192
+ email: row.impersonatorEmail ?? "",
193
+ until: row.expiresAt.getTime(),
194
+ }
195
+ : null,
172
196
  };
173
197
  }
174
198
  /** A new name for the person in every session of theirs here: the menu shows it at the next read. */
@@ -356,7 +380,13 @@ export const authRoutes = new Hono()
356
380
  resource: env.appUrl,
357
381
  }, client());
358
382
  const claims = await claimsOf(tokens);
383
+ const admin = claims.impersonatedBy;
384
+ // An admin's sign-in as someone stays in this browser: no desktop window takes it.
385
+ if (admin && state.desktop) {
386
+ return c.redirect(`${home}?signin=failed`);
387
+ }
359
388
  const id = randomBytes(32).toString("base64url");
389
+ const expiresAt = admin ? admin.until : Date.now() + SESSION_DAYS * 24 * 3600_000;
360
390
  await controlDb.insert(control.session).values({
361
391
  id,
362
392
  userId: claims.userId,
@@ -365,8 +395,10 @@ export const authRoutes = new Hono()
365
395
  name: claims.name || claims.email,
366
396
  email: claims.email,
367
397
  refreshToken: tokens.refresh_token ? seal(tokens.refresh_token) : null,
368
- claimsExpireAt: new Date(claims.expiresAt),
369
- expiresAt: new Date(Date.now() + SESSION_DAYS * 24 * 3600_000),
398
+ claimsExpireAt: new Date(renewBy(claims)),
399
+ expiresAt: new Date(expiresAt),
400
+ impersonatedBy: admin?.id ?? null,
401
+ impersonatorEmail: admin?.email ?? null,
370
402
  });
371
403
  if (state.desktop) {
372
404
  // The session belongs to the app's window, not to this browser.
@@ -379,7 +411,7 @@ export const authRoutes = new Hono()
379
411
  return c.html(`${closePage(c, say(c, "desktopSignedIn"))}
380
412
  <script>location.href = ${JSON.stringify(link)};</script>`);
381
413
  }
382
- setSessionCookie(c, id);
414
+ setSessionCookie(c, id, admin ? admin.until : undefined);
383
415
  return c.redirect(state.returnTo);
384
416
  }
385
417
  catch (err) {
@@ -389,6 +421,9 @@ export const authRoutes = new Hono()
389
421
  })
390
422
  .post("/auth/sign-out", async (c) => {
391
423
  const id = getCookie(c, SESSION_COOKIE);
424
+ const row = id
425
+ ? await controlDb.query.session.findFirst({ where: eq(control.session.id, id) })
426
+ : undefined;
392
427
  if (id) {
393
428
  await controlDb.delete(control.session).where(eq(control.session.id, id));
394
429
  sessions.delete(id);
@@ -400,6 +435,8 @@ export const authRoutes = new Hono()
400
435
  }
401
436
  // The person signs out of the account too (the Manage-App's `/api/sign-out`, a form the page
402
437
  // sends there): still signed in there, the sign-in page would sign them in again at once.
438
+ // An admin signed in as someone goes back to their own account there instead.
403
439
  const d = await discovery().catch(() => null);
404
- return c.json({ ok: true, account: d ? new URL("/api/sign-out", d.issuer).toString() : null });
440
+ const there = row?.impersonatedBy ? "/api/impersonate/stop" : "/api/sign-out";
441
+ return c.json({ ok: true, account: d ? new URL(there, d.issuer).toString() : null });
405
442
  });
@@ -76,6 +76,9 @@ export const session = sqliteTable("session", {
76
76
  /** When the access token's claims (tenant, role) must be renewed. */
77
77
  claimsExpireAt: integer("claims_expire_at", { mode: "timestamp_ms" }).notNull(),
78
78
  expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(),
79
+ /** The platform admin signed in as this person (the token's `act`); null for their own sign-in. */
80
+ impersonatedBy: text("impersonated_by"),
81
+ impersonatorEmail: text("impersonator_email"),
79
82
  createdAt: createdAt(),
80
83
  }, (t) => [index("session_expires").on(t.expiresAt)]);
81
84
  /** Settings of a runtime that runs alone: model bindings, the linked account. Never secrets. */
@@ -0,0 +1,2 @@
1
+ ALTER TABLE `session` ADD `impersonated_by` text;--> statement-breakpoint
2
+ ALTER TABLE `session` ADD `impersonator_email` text;