UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

91 lines (90 loc) • 3.3 kB
import { n as executeDevicePairingMutation, t as DevicePairingAuthorityRefusedError } from "./device-pairing-worker-SqWhlBR1.mjs"; import { s as withDevicePairingLock } from "./device-pairing-store-readonly-CIEEZowO.mjs"; import { h as resolvePairingRequestExpiry } from "./device-pairing-identity-BqPJxbIo.mjs"; import { h as publishDevicePairingResolution } from "./device-pairing-D--Fzrek.mjs"; //#region src/infra/device-pairing-approval.ts function formatDevicePairingForbiddenMessage(e) { switch (e.reason) { case "caller-scopes-required": return `missing scope: ${e.scope ?? "callerScopes-required"}`; case "caller-missing-scope": return `missing scope: ${e.scope ?? "unknown"}`; case "scope-outside-requested-roles": return `invalid scope for requested roles: ${e.scope ?? "unknown"}`; case "approval-policy-changed": return "automatic pairing policy changed; retry pairing or request manual approval"; case "bootstrap-role-not-allowed": return `bootstrap profile does not allow role: ${e.role ?? "unknown"}`; case "bootstrap-scope-not-allowed": return `bootstrap profile does not allow scope: ${e.scope ?? "unknown"}`; } throw new Error("Unsupported device pairing forbidden reason"); } function approvalAdmission(e) { let t = false; return { get refusedResult() { return t ? null : { status: "forbidden", reason: "approval-policy-changed" }; }, admit: (n) => { if (n.kind !== "pairing-approval") return; if (Date.now() > resolvePairingRequestExpiry(n.pending.refreshedAtMs ?? n.pending.ts)) { t = true; throw new DevicePairingAuthorityRefusedError("Pairing request expired before approval commit"); } if (e?.isApprovalCurrent?.({ pending: n.pending, existing: n.existing }) === false) throw new DevicePairingAuthorityRefusedError("Pairing approval policy changed"); } }; } function publishApproval(e, t, n) { if (t?.status === "approved") publishDevicePairingResolution({ requestId: e, deviceId: t.device.deviceId }, "approved", n); return t; } async function approveDevicePairing(e, t, n) { const r = typeof t === "object" ? t : void 0; const i = typeof t === "string" ? t : n; const { isApprovalCurrent: a, ...o } = r ?? {}; return await withDevicePairingLock(async () => { const t = approvalAdmission(r); return publishApproval(e, await executeDevicePairingMutation({ type: "devicePairing.approve", input: { requestId: e, options: o, nowMs: Date.now() } }, { baseDir: i, onAuthorityRefused: () => t.refusedResult, admit: t.admit }), i); }); } async function approveBootstrapDevicePairing(e, t, n = {}, r) { return await withDevicePairingLock(async () => { const i = approvalAdmission(n); const { result: a } = await executeDevicePairingMutation({ type: "devicePairing.approveBootstrap", input: { requestId: e, bootstrapProfile: t, accessMetadata: n?.accessMetadata, nowMs: Date.now() } }, { baseDir: r, onTokensReplaced: n?.onTokensReplaced, onAuthorityRefused: () => ({ result: i.refusedResult, replacedRoles: [] }), admit: i.admit }); return publishApproval(e, a, r); }); } //#endregion export { approveDevicePairing as n, formatDevicePairingForbiddenMessage as r, approveBootstrapDevicePairing as t };