UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

98 lines (97 loc) • 3.2 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { b as registerOpenClawStateDatabaseAsyncResource } from "./openclaw-state-db-cache-CkwtTqzF.mjs"; import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { n as executeDevicePairingMutation, t as DevicePairingAuthorityRefusedError } from "./device-pairing-worker-SqWhlBR1.mjs"; import { a as readCurrentPairedDevice } from "./device-pairing-store-readonly-CIEEZowO.mjs"; import { r as verifyPairedDeviceToken, t as reusablePairedDeviceToken } from "./device-pairing-token-policy-BQS0XwDZ.mjs"; //#region src/infra/device-pairing-tokens.ts async function verifyDeviceToken(e) { const { baseDir: t, ...n } = e; return readCurrentPairedDevice(n.deviceId, (e) => { const r = verifyPairedDeviceToken(e, n); if (r.ok) recordDeviceTokenUse(n, t); return r; }, t); } async function ensureDeviceToken(e) { const { isIssuanceCurrent: t, ...n } = e; return readCurrentPairedDevice(n.deviceId, (e) => { if (t?.() === false) return null; const r = reusablePairedDeviceToken(e, n); if (r) return r; return executeDevicePairingMutation({ type: "devicePairing.ensureToken", input: { ...n, nowMs: Date.now() } }, { onAuthorityRefused: () => null, assertCurrent: () => { if (t?.() === false) throw new DevicePairingAuthorityRefusedError("Device token issuance authority changed"); } }); }); } async function rotateDeviceToken(e) { const { baseDir: t, ...n } = e; return await executeDevicePairingMutation({ type: "devicePairing.rotateToken", input: { ...n, nowMs: Date.now() } }, { baseDir: t }); } async function revokeDeviceToken(e) { const { baseDir: t, ...n } = e; return await executeDevicePairingMutation({ type: "devicePairing.revokeToken", input: { ...n, nowMs: Date.now() } }, { baseDir: t }); } const uses = resolveGlobalSingleton(Symbol.for("openclaw.deviceTokenUses"), () => { const e = /* @__PURE__ */ new Map(); registerOpenClawStateDatabaseAsyncResource({ async close(t) { await Promise.all([...e].flatMap(([e, n]) => !t || e.startsWith(`${t.key}:`) ? [n.done] : [])); } }); return e; }); const log = createSubsystemLogger("gateway/device-token"); function recordDeviceTokenUse(e, t) { const n = captureOpenClawStateWorkerContext(t ? { env: { ...process.env, OPENCLAW_STATE_DIR: t } } : {}); const r = `${n.admission.identity.key}:${e.deviceId.trim()}:${e.role.trim()}`; const i = { ...e, nowMs: Date.now() }; const a = uses.get(r); if (a) { a.input = i; return; } const o = { input: i, done: Promise.resolve() }; uses.set(r, o); o.done = (async () => { while (o.input) { const e = o.input; o.input = void 0; await executeDevicePairingMutation({ type: "devicePairing.verifyToken", input: e }, { context: n }); } })().catch((e) => log.warn(`Device token last-use recording failed: ${String(e)}`)).finally(() => uses.delete(r)); } //#endregion export { verifyDeviceToken as i, revokeDeviceToken as n, rotateDeviceToken as r, ensureDeviceToken as t };