UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

120 lines (119 loc) • 5.58 kB
import { o as capturePluginRegistryLifecycleEpoch, v as isPluginRegistryLifecycleEpochActive } from "./registry-lifecycle-CJjWnl-y.mjs"; import { t as safeEqualSecret } from "./secret-equal-BogABOFr.mjs"; import { t as getOrCreateWeakOwnerMap } from "./weak-owner-map-BGsaUABm.mjs"; import { createHmac, randomBytes } from "node:crypto"; //#region src/gateway/board-view-ticket.ts const BOARD_HTTP_PATH_PREFIX = "/__openclaw__/board/"; const BOARD_VIEW_TICKET_TTL_MS = 12e5; const BOARD_VIEW_TICKET_SCOPE = "board-widget-view"; const BOARD_VIEW_TICKET_MAX_LENGTH = 2048; const ticketSecret = randomBytes(32); const ticketAuthorities = /* @__PURE__ */ new WeakMap(); var BoardGatewayUnavailableError = class extends Error { constructor() { super("dashboard unavailable"); this.name = "BoardGatewayUnavailableError"; } }; function signTicketPayload(e, t) { return createHmac("sha256", t).update(`${BOARD_VIEW_TICKET_SCOPE}\0${e}`).digest("base64url"); } function isValidClaims(e) { if (!e || typeof e !== "object") return false; const t = e; return typeof t.sessionKey === "string" && t.sessionKey.length > 0 && t.sessionKey.length <= 512 && (t.agentId === void 0 || typeof t.agentId === "string" && t.agentId.length > 0 && t.agentId.length <= 64) && typeof t.name === "string" && t.name.length > 0 && t.name.length <= 64 && Number.isSafeInteger(t.revision) && (t.revision ?? 0) >= 1 && typeof t.viewGeneration === "string" && /^[a-f0-9]{32}$/u.test(t.viewGeneration) && typeof t.authorityGeneration === "string" && /^[A-Za-z0-9_-]{32}$/u.test(t.authorityGeneration) && Number.isSafeInteger(t.expiresAtMs) && typeof t.nonce === "string" && /^[A-Za-z0-9_-]{32}$/u.test(t.nonce); } function captureBoardViewTicketAuthority(e) { let t; try { t = e.resolveGatewayContext(); } catch { throw new BoardGatewayUnavailableError(); } if (t !== e.gatewayContext || e.gatewayContext.resolveGatewayContext !== e.resolveGatewayContext) throw new BoardGatewayUnavailableError(); const n = e.gatewayContext.getGatewayMethodRegistry?.(); const r = e.pluginRegistry ? capturePluginRegistryLifecycleEpoch(e.pluginRegistry) : void 0; if (e.pluginRegistry && !r) throw new BoardGatewayUnavailableError(); e.remoteIngressPrincipal?.assertCurrent(); const i = getOrCreateWeakOwnerMap(ticketAuthorities, e.gatewayContext); const a = e.remoteIngressPrincipal?.assertCurrent ?? e.gatewayContext; const o = i.get(a); if (o?.resolveGatewayContext === e.resolveGatewayContext && o.methodRegistry === n && o.pluginRegistry === e.pluginRegistry && o.pluginRegistryEpoch === r) return o; const s = { ...e, generation: randomBytes(24).toString("base64url"), ...n ? { methodRegistry: n } : {}, ...r ? { pluginRegistryEpoch: r } : {} }; i.set(a, s); if (!o && e.remoteIngressPrincipal) { const t = i; e.remoteIngressPrincipal.signal.addEventListener("abort", () => t.delete(a), { once: true }); } return s; } function requireBoardViewTicketAuthority(e, t) { const n = t ? [...ticketAuthorities.get(t)?.values() ?? []].find((t) => t.generation === e.authorityGeneration) : void 0; let r; try { n?.remoteIngressPrincipal?.assertCurrent(); r = n?.resolveGatewayContext(); } catch { throw new BoardGatewayUnavailableError(); } if (!t || !n || n.generation !== e.authorityGeneration || n.gatewayContext !== t || r !== t || t.resolveGatewayContext !== n.resolveGatewayContext || n.methodRegistry && t.getGatewayMethodRegistry?.() !== n.methodRegistry || n.pluginRegistry && (!n.pluginRegistryEpoch || !isPluginRegistryLifecycleEpochActive(n.pluginRegistry, n.pluginRegistryEpoch))) throw new BoardGatewayUnavailableError(); return n; } function createBoardViewTicket(e) { const t = e.nowMs ?? Date.now(); const n = captureBoardViewTicketAuthority(e.authority); const r = { sessionKey: e.sessionKey, ...e.agentId ? { agentId: e.agentId } : {}, name: e.name, revision: e.revision, viewGeneration: e.viewGeneration, authorityGeneration: n.generation, expiresAtMs: t + BOARD_VIEW_TICKET_TTL_MS, nonce: randomBytes(24).toString("base64url") }; if (!Number.isSafeInteger(t) || !isValidClaims(r)) throw new Error("invalid board view ticket binding"); const i = Buffer.from(JSON.stringify(r), "utf8").toString("base64url"); return { ticket: `v1.${i}.${signTicketPayload(i, ticketSecret)}`, expiresAtMs: r.expiresAtMs }; } function verifyBoardViewTicket(e, t = {}) { const n = t.nowMs ?? Date.now(); if (!Number.isSafeInteger(n) || e.length > BOARD_VIEW_TICKET_MAX_LENGTH) return; const r = e.split("."); if (r.length !== 3 || r[0] !== "v1") return; const [, i, a] = r; if (!i || !a) return; const o = signTicketPayload(i, ticketSecret); if (!safeEqualSecret(a, o)) return; let s; try { s = JSON.parse(Buffer.from(i, "base64url").toString("utf8")); } catch { return; } if (!isValidClaims(s) || s.expiresAtMs <= n) return; return s; } function resolveAuthorizedBoardViewTicketClaims(e, t = {}) { const n = verifyBoardViewTicket(e, t); if (!n) return; try { requireBoardViewTicketAuthority(n, t.gatewayContext); return n; } catch { return; } } function buildBoardWidgetFrameUrl(e) { return `${BOARD_HTTP_PATH_PREFIX}${encodeURIComponent(e.sessionKey)}/${encodeURIComponent(e.name)}/index.html?bt=${encodeURIComponent(e.ticket)}`; } //#endregion export { createBoardViewTicket as a, verifyBoardViewTicket as c, buildBoardWidgetFrameUrl as i, BOARD_VIEW_TICKET_TTL_MS as n, requireBoardViewTicketAuthority as o, BoardGatewayUnavailableError as r, resolveAuthorizedBoardViewTicketClaims as s, BOARD_HTTP_PATH_PREFIX as t };