UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

95 lines (94 loc) • 4.79 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as stableStringify } from "./stable-stringify-vqde_3tT.mjs"; import { i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; import { a as tableExists } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs"; import { c as validateStoredDeviceIdentity } from "./device-identity-material-BGzHHiUD.mjs"; import { p as stripExecApprovalsRawSocketCredentials } from "./exec-approvals-config-BBzEUJlE.mjs"; import { createHash } from "node:crypto"; //#region src/infra/client-credential-migration-data.ts /** Canonical predecessors consume legacy inputs even when the retained JSON changes. */ function resolveClientCredentialLegacyImportClaims(e, t) { const n = {}; for (const e of t.identities) if (e.identity_key === "primary" || e.identity_key === "node" || e.identity_key === "shareExtension") { n[`identity:${e.identity_key}`] = { sha256: null }; if (t.authTablePresent) n[`auth:${e.identity_key}`] = { sha256: null }; } if (!t.nativeBootstrap && tableExists(e, "migration_sources") && e.prepare("SELECT 1 FROM migration_sources WHERE migration_kind = 'legacy-device-identity-json' LIMIT 1").get()) n["identity:primary"] = { sha256: null }; return n; } function sourceRows(e, t) { const n = getNodeSqliteKysely(e); const r = { identities: t && !tableExists(e, "device_identities") ? [] : executeSqliteQuerySync(e, n.selectFrom("device_identities").selectAll()).rows, tokens: t && !tableExists(e, "device_auth_tokens") ? [] : executeSqliteQuerySync(e, n.selectFrom("device_auth_tokens").selectAll()).rows, origins: t && !tableExists(e, "gateway_origin_device_tokens") ? [] : executeSqliteQuerySync(e, n.selectFrom("gateway_origin_device_tokens").selectAll()).rows, approvals: t && !tableExists(e, "exec_approvals_config") ? [] : executeSqliteQuerySync(e, n.selectFrom("exec_approvals_config").selectAll()).rows }; const compare = (e, t) => Buffer.compare(Buffer.from(e), Buffer.from(t)); r.identities.sort((e, t) => compare(e.identity_key, t.identity_key)); r.tokens.sort((e, t) => compare(e.device_id, t.device_id) || compare(e.role, t.role)); r.origins.sort((e, t) => compare(e.gateway_scope, t.gateway_scope) || compare(e.device_id, t.device_id) || compare(e.role, t.role)); r.approvals.sort((e, t) => compare(e.config_key, t.config_key)); return r; } function validateSource(e, t = "require-valid") { if (t === "require-valid") for (const t of e.identities) { validateStoredDeviceIdentity({ deviceId: t.device_id, publicKeyPem: t.public_key_pem, privateKeyPem: t.private_key_pem, createdAtMs: t.created_at_ms }, t.identity_key); if (!Number.isSafeInteger(t.updated_at_ms) || t.updated_at_ms < 0) throw new Error("Client credential migration found an invalid identity timestamp."); } for (const t of [...e.tokens, ...e.origins]) { let e; try { e = JSON.parse(t.scopes_json); } catch { throw new Error("Client credential migration found invalid token scopes."); } if (!Array.isArray(e) || !e.every((e) => typeof e === "string") || !Number.isSafeInteger(t.updated_at_ms) || t.updated_at_ms < 0) throw new Error("Client credential migration found an invalid token record."); } if (e.approvals.some((e) => e.config_key !== "current") || e.approvals.length > 1) throw new Error("Client credential migration found a noncanonical approval singleton."); } function hashClientCredentialRows(e, t) { return createHash("sha256").update(stableStringify(t ? { authTablePresent: t.authTablePresent, identities: e.identities, tokens: e.tokens } : e)).digest("hex"); } function extractSocket(e) { if (!e) return { socket: void 0, policy: void 0 }; const invalid = () => Object.assign(/* @__PURE__ */ new Error("Client credential migration found invalid socket credentials or projections."), { code: "CLIENT_CREDENTIAL_SOCKET_INVALID" }); let t; try { t = JSON.parse(e.raw_json); } catch { throw new Error("Client credential migration found invalid approval JSON."); } const n = isRecord(t) ? t.socket : void 0; if (n !== void 0 && !isRecord(n)) throw invalid(); const r = isRecord(n) ? n.path : void 0; const i = isRecord(n) ? n.token : void 0; if (r !== void 0 && typeof r !== "string" || i !== void 0 && typeof i !== "string") throw invalid(); if (r === void 0 && i === void 0) return { socket: void 0, policy: e.raw_json }; return { socket: { socket_key: "current", socket_path: r ?? null, socket_token: i ?? null, updated_at_ms: e.updated_at_ms }, policy: stripExecApprovalsRawSocketCredentials(e.raw_json) }; } //#endregion export { validateSource as a, sourceRows as i, hashClientCredentialRows as n, resolveClientCredentialLegacyImportClaims as r, extractSocket as t };