@join-pantheon/cli 0.33.11-rc.2 → 0.34.0-rc.1

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.
package/dist/accounts.js CHANGED
@@ -35,6 +35,11 @@ export const MAX_FAILS_PER_NETWORK = 50;
35
35
  export const FAIL_WINDOW_MS = 60 * 60 * 1_000;
36
36
  const MAX_PENDING_PER_EMAIL = 5;
37
37
  const now = () => Date.now();
38
+ export const VIEWER_DEFAULT_DAYS = 90;
39
+ export const VIEWER_MAX_DAYS = 365;
40
+ export const VIEWER_SESSION_IDLE_MS = 12 * 3_600_000;
41
+ export const VIEWER_SESSION_MAX_MS = 7 * 86_400_000;
42
+ export const ADMIN_SESSION_MS = 12 * 3_600_000;
38
43
  const newId = (p) => `${p}_${randomBytes(9).toString("base64url")}`;
39
44
  const sha = (s) => createHash("sha256").update(s).digest("hex");
40
45
  export function normalizeEmail(email) {
@@ -175,6 +180,58 @@ export class Accounts {
175
180
  deleted_at INTEGER
176
181
  );
177
182
  CREATE INDEX IF NOT EXISTS idx_spaces_owner ON spaces(owner_account);
183
+ -- The operator's browser sessions: a random secret in the cookie, only
184
+ -- its hash here. token_fp ties a session to the admin token it was
185
+ -- opened with, so changing PANTHEON_ADMIN_TOKEN signs every browser out.
186
+ -- Read-only team viewers (0.34): an email address the operator gave a
187
+ -- team's flow to, until a date. Never a member, never an account.
188
+ CREATE TABLE IF NOT EXISTS viewer_grants (
189
+ id TEXT PRIMARY KEY,
190
+ space TEXT NOT NULL,
191
+ email TEXT NOT NULL,
192
+ scope TEXT NOT NULL DEFAULT 'flow',
193
+ granted_by TEXT NOT NULL,
194
+ granted_at INTEGER NOT NULL,
195
+ expires_at INTEGER NOT NULL,
196
+ revoked_at INTEGER,
197
+ revoked_by TEXT,
198
+ last_view_at INTEGER,
199
+ expired_noted INTEGER
200
+ );
201
+ CREATE INDEX IF NOT EXISTS idx_viewer_grants_email ON viewer_grants(email);
202
+ CREATE INDEX IF NOT EXISTS idx_viewer_grants_space ON viewer_grants(space);
203
+ -- A viewer's browser session: only the hash of the cookie's secret.
204
+ CREATE TABLE IF NOT EXISTS viewer_sessions (
205
+ id_hash TEXT PRIMARY KEY,
206
+ email TEXT NOT NULL,
207
+ ip TEXT NOT NULL,
208
+ created_at INTEGER NOT NULL,
209
+ last_seen INTEGER NOT NULL,
210
+ expires_at INTEGER NOT NULL,
211
+ revoked_at INTEGER
212
+ );
213
+ CREATE INDEX IF NOT EXISTS idx_viewer_sessions_email ON viewer_sessions(email);
214
+ -- A viewer's sign-in codes. A table of their own, not verifications_by_device:
215
+ -- a relay rolled back to 0.33 reads every row of that table as an account's
216
+ -- code, and would take a viewer's code as a login (review, 14 Oct 2026).
217
+ CREATE TABLE IF NOT EXISTS viewer_codes (
218
+ email TEXT NOT NULL,
219
+ nonce TEXT NOT NULL,
220
+ code_hash TEXT NOT NULL,
221
+ expires_at INTEGER NOT NULL,
222
+ attempts INTEGER NOT NULL DEFAULT 0,
223
+ created_at INTEGER NOT NULL,
224
+ PRIMARY KEY (email, nonce)
225
+ );
226
+ CREATE TABLE IF NOT EXISTS admin_sessions (
227
+ id_hash TEXT PRIMARY KEY,
228
+ token_fp TEXT NOT NULL,
229
+ ip TEXT NOT NULL,
230
+ created_at INTEGER NOT NULL,
231
+ last_seen INTEGER NOT NULL,
232
+ expires_at INTEGER NOT NULL,
233
+ revoked_at INTEGER
234
+ );
178
235
  `);
179
236
  const cols = new Set(this.db.prepare("PRAGMA table_info(accounts)").all().map((c) => c.name));
180
237
  if (!cols.has("plan"))
@@ -186,6 +243,10 @@ export class Accounts {
186
243
  this.db.exec("ALTER TABLE sessions ADD COLUMN ended_at INTEGER");
187
244
  if (!scols.has("role"))
188
245
  this.db.exec("ALTER TABLE sessions ADD COLUMN role TEXT");
246
+ const vgcols = new Set(this.db.prepare("PRAGMA table_info(viewer_grants)").all().map((c) => c.name));
247
+ if (!vgcols.has("space_id"))
248
+ this.db.exec("ALTER TABLE viewer_grants ADD COLUMN space_id TEXT");
249
+ this.db.exec("DELETE FROM verifications_by_device WHERE purpose = 'viewer'");
189
250
  const spcols = new Set(this.db.prepare("PRAGMA table_info(spaces)").all().map((c) => c.name));
190
251
  if (!spcols.has("owner_deleted_at"))
191
252
  this.db.exec("ALTER TABLE spaces ADD COLUMN owner_deleted_at INTEGER");
@@ -209,7 +270,9 @@ export class Accounts {
209
270
  throw e;
210
271
  }
211
272
  }
212
- close() { this.db.close(); }
273
+ close() { if (this.closed)
274
+ return; this.closed = true; this.db.close(); }
275
+ closed = false;
213
276
  accountByUsername(username) {
214
277
  return this.db.prepare("SELECT * FROM accounts WHERE username = ?").get(normalizeUsername(username)) ?? null;
215
278
  }
@@ -265,7 +328,7 @@ export class Accounts {
265
328
  const t = now();
266
329
  const acct0 = this.accountByEmail(e);
267
330
  const ownKey = !!acct0 && this.deviceByPubkey(key)?.account_id === acct0.id;
268
- const prev = this.db.prepare(ownKey ? "SELECT MAX(created_at) created_at FROM verifications_by_device WHERE email = ? AND pending_pubkey = ?" : "SELECT MAX(created_at) created_at FROM verifications_by_device WHERE email = ? AND ? IS NOT NULL")
331
+ const prev = this.db.prepare(ownKey ? "SELECT MAX(created_at) created_at FROM verifications_by_device WHERE email = ? AND pending_pubkey = ? AND purpose != 'viewer'" : "SELECT MAX(created_at) created_at FROM verifications_by_device WHERE email = ? AND ? IS NOT NULL AND purpose != 'viewer'")
269
332
  .get(e, key);
270
333
  if (prev?.created_at && t - prev.created_at < CODE_RESEND_COOLDOWN_MS)
271
334
  return { ok: false, reason: "too_soon" };
@@ -273,12 +336,12 @@ export class Accounts {
273
336
  return { ok: false, reason: "locked" };
274
337
  this.db.prepare("DELETE FROM verifications_by_device WHERE email = ? AND expires_at < ?").run(e, t);
275
338
  if (ownKey) {
276
- for (const r of this.db.prepare("SELECT pending_pubkey FROM verifications_by_device WHERE email = ? AND pending_pubkey != ?").all(e, key)) {
339
+ for (const r of this.db.prepare("SELECT pending_pubkey FROM verifications_by_device WHERE email = ? AND pending_pubkey != ? AND purpose != 'viewer'").all(e, key)) {
277
340
  if (this.deviceByPubkey(r.pending_pubkey)?.account_id !== acct0.id)
278
341
  this.db.prepare("DELETE FROM verifications_by_device WHERE email = ? AND pending_pubkey = ?").run(e, r.pending_pubkey);
279
342
  }
280
343
  }
281
- const others = this.db.prepare("SELECT pending_pubkey FROM verifications_by_device WHERE email = ? AND pending_pubkey != ? ORDER BY created_at ASC").all(e, key);
344
+ const others = this.db.prepare("SELECT pending_pubkey FROM verifications_by_device WHERE email = ? AND pending_pubkey != ? AND purpose != 'viewer' ORDER BY created_at ASC").all(e, key);
282
345
  for (const r of others.slice(0, Math.max(0, others.length - (MAX_PENDING_PER_EMAIL - 1)))) {
283
346
  this.db.prepare("DELETE FROM verifications_by_device WHERE email = ? AND pending_pubkey = ?").run(e, r.pending_pubkey);
284
347
  }
@@ -308,7 +371,7 @@ export class Accounts {
308
371
  verify(email, code, ip, pubkey, net = null) {
309
372
  const e = normalizeEmail(email);
310
373
  const t = now();
311
- const rows = this.db.prepare("SELECT * FROM verifications_by_device WHERE email = ? ORDER BY created_at ASC").all(e);
374
+ const rows = this.db.prepare("SELECT * FROM verifications_by_device WHERE email = ? AND purpose != 'viewer' ORDER BY created_at ASC").all(e);
312
375
  let v;
313
376
  if (pubkey) {
314
377
  v = rows.find((r) => r.pending_pubkey === pubkey);
@@ -373,8 +436,11 @@ export class Accounts {
373
436
  }
374
437
  const device = this.bindDevice(account.id, v.pending_pubkey, v.pending_label ?? "a device");
375
438
  const session = this.openSession(account.id, device.id, "", ip);
439
+ run("UPDATE sessions SET ended_at = ?, role = 'signin' WHERE id = ?").run(session.created_at, session.id);
440
+ session.ended_at = session.created_at;
441
+ session.role = "signin";
376
442
  if (v.purpose === "signup")
377
- run("DELETE FROM verifications_by_device WHERE email = ?").run(e);
443
+ run("DELETE FROM verifications_by_device WHERE email = ? AND purpose != 'viewer'").run(e);
378
444
  else
379
445
  run(`DELETE FROM verifications_by_device WHERE ${where}`).run(e, v.pending_pubkey);
380
446
  run("DELETE FROM verify_failures WHERE email = ?").run(keyId);
@@ -486,6 +552,157 @@ export class Accounts {
486
552
  this.audit({ actor_account: null, event: "account_deleted", target: acct.username, ip });
487
553
  return true;
488
554
  }
555
+ grantViewer(o) {
556
+ const e = normalizeEmail(o.email);
557
+ if (!validEmail(e) || e.length > 254 || !o.space || !o.spaceId)
558
+ return null;
559
+ const days = Math.min(VIEWER_MAX_DAYS, Math.max(1, Math.floor(Number.isFinite(o.days) ? o.days : VIEWER_DEFAULT_DAYS)));
560
+ const t = now();
561
+ this.db.prepare("UPDATE viewer_grants SET revoked_at = ?, revoked_by = ? WHERE space_id = ? AND email = ? AND revoked_at IS NULL AND expires_at > ?").run(t, `${o.grantedBy} (replaced)`, o.spaceId, e, t);
562
+ const g = { id: newId("vg"), space: o.space, space_id: o.spaceId, email: e, scope: "flow", granted_by: o.grantedBy, granted_at: t, expires_at: t + days * 86_400_000, revoked_at: null, revoked_by: null, last_view_at: null };
563
+ this.db.prepare("INSERT INTO viewer_grants (id, space, space_id, email, scope, granted_by, granted_at, expires_at, revoked_at, revoked_by, last_view_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, NULL)")
564
+ .run(g.id, g.space, g.space_id, g.email, g.scope, g.granted_by, g.granted_at, g.expires_at);
565
+ return g;
566
+ }
567
+ viewerGrant(id) {
568
+ return this.db.prepare("SELECT id, space, space_id, email, scope, granted_by, granted_at, expires_at, revoked_at, revoked_by, last_view_at FROM viewer_grants WHERE id = ?").get(id) ?? null;
569
+ }
570
+ revokeViewer(id, by) {
571
+ const g = this.viewerGrant(id);
572
+ if (!g || g.revoked_at !== null)
573
+ return null;
574
+ const t = now();
575
+ this.db.prepare("UPDATE viewer_grants SET revoked_at = ?, revoked_by = ? WHERE id = ?").run(t, by, id);
576
+ this.endViewerSessionsIfNoAccess(g.email);
577
+ return { ...g, revoked_at: t, revoked_by: by };
578
+ }
579
+ revokeViewersOfTeam(name, spaceId, by) {
580
+ const t = now();
581
+ const rows = this.db.prepare("SELECT id, space, space_id, email, scope, granted_by, granted_at, expires_at, revoked_at, revoked_by, last_view_at FROM viewer_grants WHERE revoked_at IS NULL AND (space_id = ? OR (space_id IS NULL AND space = ?))").all(spaceId ?? "", name);
582
+ for (const r of rows)
583
+ this.db.prepare("UPDATE viewer_grants SET revoked_at = ?, revoked_by = ? WHERE id = ?").run(t, by, r.id);
584
+ for (const e of new Set(rows.map((r) => r.email)))
585
+ this.endViewerSessionsIfNoAccess(e);
586
+ return rows;
587
+ }
588
+ unboundViewerGrants() {
589
+ return this.db.prepare("SELECT id, space, space_id, email, scope, granted_by, granted_at, expires_at, revoked_at, revoked_by, last_view_at FROM viewer_grants WHERE space_id IS NULL AND revoked_at IS NULL").all();
590
+ }
591
+ bindViewerGrant(id, spaceId) { this.db.prepare("UPDATE viewer_grants SET space_id = ? WHERE id = ? AND space_id IS NULL").run(spaceId, id); }
592
+ endViewerSessionsIfNoAccess(email) {
593
+ return this.liveViewerGrants(email).length ? 0 : this.endViewerSessionsFor(email);
594
+ }
595
+ viewerGrants(spaceId) {
596
+ return this.db.prepare("SELECT id, space, space_id, email, scope, granted_by, granted_at, expires_at, revoked_at, revoked_by, last_view_at FROM viewer_grants WHERE space_id = ? ORDER BY granted_at DESC LIMIT 50").all(spaceId);
597
+ }
598
+ liveViewerGrants(email, t = now()) {
599
+ return this.db.prepare("SELECT id, space, space_id, email, scope, granted_by, granted_at, expires_at, revoked_at, revoked_by, last_view_at FROM viewer_grants WHERE email = ? AND revoked_at IS NULL AND expires_at > ? AND space_id IS NOT NULL ORDER BY space").all(normalizeEmail(email), t);
600
+ }
601
+ takeExpiredViewerGrants(email, t = now()) {
602
+ const rows = this.db.prepare("SELECT id, space, space_id, email, scope, granted_by, granted_at, expires_at, revoked_at, revoked_by, last_view_at FROM viewer_grants WHERE email = ? AND revoked_at IS NULL AND expires_at <= ? AND expired_noted IS NULL").all(normalizeEmail(email), t);
603
+ for (const r of rows)
604
+ this.db.prepare("UPDATE viewer_grants SET expired_noted = ? WHERE id = ?").run(t, r.id);
605
+ return rows;
606
+ }
607
+ noteViewerView(id) { this.db.prepare("UPDATE viewer_grants SET last_view_at = ? WHERE id = ?").run(now(), id); }
608
+ startViewerCode(email, nonce) {
609
+ const e = normalizeEmail(email);
610
+ if (!validEmail(e) || e.length > 254 || !/^[A-Za-z0-9_-]{16,64}$/.test(nonce))
611
+ return { ok: false, reason: "bad_email" };
612
+ if (this.isBlocked(e))
613
+ return { ok: false, reason: "blocked" };
614
+ const t = now();
615
+ const prev = this.db.prepare("SELECT MAX(created_at) created_at FROM viewer_codes WHERE email = ?").get(e);
616
+ if (prev?.created_at && t - prev.created_at < CODE_RESEND_COOLDOWN_MS)
617
+ return { ok: false, reason: "too_soon" };
618
+ if (this.failsOf(`key:${e}|viewer:${nonce}`, t) >= MAX_FAILS_PER_KEY)
619
+ return { ok: false, reason: "locked" };
620
+ this.db.prepare("DELETE FROM viewer_codes WHERE expires_at < ?").run(t);
621
+ const others = this.db.prepare("SELECT nonce FROM viewer_codes WHERE email = ? AND nonce != ? ORDER BY created_at ASC").all(e, nonce);
622
+ for (const r of others.slice(0, Math.max(0, others.length - (MAX_PENDING_PER_EMAIL - 1))))
623
+ this.db.prepare("DELETE FROM viewer_codes WHERE email = ? AND nonce = ?").run(e, r.nonce);
624
+ const code = String(randomInt(0, 1_000_000)).padStart(6, "0");
625
+ this.db.prepare(`INSERT INTO viewer_codes (email, nonce, code_hash, expires_at, attempts, created_at) VALUES (?, ?, ?, ?, 0, ?)
626
+ ON CONFLICT(email, nonce) DO UPDATE SET code_hash = excluded.code_hash, expires_at = excluded.expires_at, attempts = 0, created_at = excluded.created_at`).run(e, nonce, sha(code), t + CODE_TTL_MS, t);
627
+ return { ok: true, code };
628
+ }
629
+ verifyViewerCode(email, code, nonce, net = null) {
630
+ const e = normalizeEmail(email);
631
+ if (!/^\d{6}$/.test(code.trim()) || !/^[A-Za-z0-9_-]{16,64}$/.test(nonce))
632
+ return false;
633
+ const t = now();
634
+ const v = this.db.prepare("SELECT code_hash, expires_at, attempts FROM viewer_codes WHERE email = ? AND nonce = ?").get(e, nonce);
635
+ if (!v)
636
+ return false;
637
+ if (t > v.expires_at) {
638
+ this.db.prepare("DELETE FROM viewer_codes WHERE email = ? AND nonce = ?").run(e, nonce);
639
+ return false;
640
+ }
641
+ if (v.attempts >= MAX_ATTEMPTS)
642
+ return false;
643
+ const keyId = `key:${e}|viewer:${nonce}`, netId = net ? `net:${net}` : null;
644
+ if (this.failsOf(keyId, t) >= MAX_FAILS_PER_KEY || (netId && this.failsOf(netId, t) >= MAX_FAILS_PER_NETWORK))
645
+ return false;
646
+ const want = Buffer.from(v.code_hash, "hex"), got = Buffer.from(sha(code.trim()), "hex");
647
+ if (!(want.length === got.length && timingSafeEqual(want, got))) {
648
+ this.db.prepare("UPDATE viewer_codes SET attempts = attempts + 1 WHERE email = ? AND nonce = ?").run(e, nonce);
649
+ this.noteFail(keyId, t);
650
+ if (netId)
651
+ this.noteFail(netId, t);
652
+ return false;
653
+ }
654
+ this.db.prepare("DELETE FROM viewer_codes WHERE email = ? AND nonce = ?").run(e, nonce);
655
+ this.db.prepare("DELETE FROM verify_failures WHERE email = ?").run(keyId);
656
+ return true;
657
+ }
658
+ createViewerSession(email, ip) {
659
+ const secret = randomBytes(32).toString("base64url");
660
+ const t = now();
661
+ this.db.prepare("DELETE FROM viewer_sessions WHERE expires_at < ? OR revoked_at IS NOT NULL").run(t - 86_400_000);
662
+ this.db.prepare("INSERT INTO viewer_sessions (id_hash, email, ip, created_at, last_seen, expires_at, revoked_at) VALUES (?, ?, ?, ?, ?, ?, NULL)")
663
+ .run(sha(secret), normalizeEmail(email), ip, t, t, t + VIEWER_SESSION_MAX_MS);
664
+ return secret;
665
+ }
666
+ viewerSession(secret) {
667
+ if (!secret || secret.length > 200)
668
+ return null;
669
+ const t = now(), h = sha(secret);
670
+ const r = this.db.prepare("SELECT email, last_seen, expires_at, revoked_at FROM viewer_sessions WHERE id_hash = ?").get(h);
671
+ if (!r || r.revoked_at !== null || r.expires_at <= t || t - r.last_seen > VIEWER_SESSION_IDLE_MS)
672
+ return null;
673
+ this.db.prepare("UPDATE viewer_sessions SET last_seen = ? WHERE id_hash = ?").run(t, h);
674
+ return { email: r.email, idHash: h };
675
+ }
676
+ endViewerSession(secret) {
677
+ if (secret)
678
+ this.db.prepare("UPDATE viewer_sessions SET revoked_at = ? WHERE id_hash = ? AND revoked_at IS NULL").run(now(), sha(secret));
679
+ }
680
+ endViewerSessionsFor(email) {
681
+ return Number(this.db.prepare("DELETE FROM viewer_sessions WHERE email = ?").run(normalizeEmail(email)).changes);
682
+ }
683
+ createAdminSession(tokenFp, ip, ttlMs = ADMIN_SESSION_MS) {
684
+ const secret = randomBytes(32).toString("base64url");
685
+ const t = now();
686
+ this.db.prepare("DELETE FROM admin_sessions WHERE expires_at < ? OR revoked_at IS NOT NULL").run(t - 86_400_000);
687
+ this.db.prepare("INSERT INTO admin_sessions (id_hash, token_fp, ip, created_at, last_seen, expires_at, revoked_at) VALUES (?, ?, ?, ?, ?, ?, NULL)")
688
+ .run(sha(secret), tokenFp, ip, t, t, t + ttlMs);
689
+ return secret;
690
+ }
691
+ adminSessionOk(secret, tokenFp) {
692
+ if (!secret || secret.length > 200)
693
+ return false;
694
+ const t = now();
695
+ const r = this.db.prepare("SELECT token_fp, expires_at, revoked_at FROM admin_sessions WHERE id_hash = ?").get(sha(secret));
696
+ if (!r || r.revoked_at !== null || r.expires_at < t || r.token_fp !== tokenFp)
697
+ return false;
698
+ this.db.prepare("UPDATE admin_sessions SET last_seen = ? WHERE id_hash = ?").run(t, sha(secret));
699
+ return true;
700
+ }
701
+ endAdminSession(secret) {
702
+ if (!secret)
703
+ return;
704
+ this.db.prepare("UPDATE admin_sessions SET revoked_at = ? WHERE id_hash = ? AND revoked_at IS NULL").run(now(), sha(secret));
705
+ }
489
706
  audit(row) {
490
707
  this.db.prepare("INSERT INTO audit (id, ts, actor_account, event, space, target, detail, ip) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
491
708
  .run(newId("aud"), now(), row.actor_account ?? null, row.event, row.space ?? null, row.target ?? null, row.detail ?? null, row.ip ?? null);
@@ -540,7 +757,7 @@ export class Accounts {
540
757
  return this.db.prepare("SELECT * FROM blocks ORDER BY created_at DESC").all();
541
758
  }
542
759
  recentVerifications(limit = 50) {
543
- return this.db.prepare("SELECT email, purpose, created_at, expires_at, attempts FROM verifications_by_device ORDER BY created_at DESC LIMIT ?").all(limit);
760
+ return this.db.prepare("SELECT email, purpose, created_at, expires_at, attempts FROM verifications_by_device UNION ALL SELECT email, 'viewer' purpose, created_at, expires_at, attempts FROM viewer_codes ORDER BY created_at DESC LIMIT ?").all(limit);
544
761
  }
545
762
  plans() {
546
763
  const row = this.db.prepare("SELECT json FROM settings WHERE key = 'plans'").get();
@@ -650,7 +867,7 @@ export class Accounts {
650
867
  return this.db.prepare("SELECT * FROM sessions WHERE ended_at IS NULL AND revoked_at IS NULL ORDER BY created_at DESC LIMIT ?").all(limit);
651
868
  }
652
869
  endedSessionsSince(sinceMs) {
653
- return this.db.prepare("SELECT COUNT(*) c FROM sessions WHERE ended_at IS NOT NULL AND ended_at >= ?").get(sinceMs).c;
870
+ return this.db.prepare("SELECT COUNT(*) c FROM sessions WHERE ended_at IS NOT NULL AND ended_at >= ? AND (role IS NULL OR role != 'signin')").get(sinceMs).c;
654
871
  }
655
872
  allSessions(limit = 500) {
656
873
  return this.db.prepare("SELECT * FROM sessions ORDER BY created_at DESC LIMIT ?").all(limit);
@@ -730,6 +947,10 @@ export class Accounts {
730
947
  where.push("space = ?");
731
948
  args.push(opts.space);
732
949
  }
950
+ if (opts.event) {
951
+ where.push("event = ?");
952
+ args.push(opts.event);
953
+ }
733
954
  if (opts.sinceMs) {
734
955
  where.push("ts >= ?");
735
956
  args.push(opts.sinceMs);