UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

560 lines (559 loc) • 18.9 kB
import { n as readJsonFile, r as writeJsonAtomic, t as createAsyncLock } from "./json-files-2-D1jkJB.js"; import { n as normalizeDeviceAuthScopes } from "./device-auth-DUVzz7ar.js"; import { a as resolvePairingPaths, i as pruneExpiredPending, n as verifyPairingToken, r as rejectPendingPairingRequest, t as generatePairingToken } from "./pairing-token-DAszGmwD.js"; import { randomUUID } from "node:crypto"; //#region src/shared/operator-scope-compat.ts const OPERATOR_ROLE = "operator"; const OPERATOR_ADMIN_SCOPE = "operator.admin"; const OPERATOR_READ_SCOPE = "operator.read"; const OPERATOR_WRITE_SCOPE = "operator.write"; const OPERATOR_SCOPE_PREFIX$1 = "operator."; function normalizeScopeList(scopes) { const out = /* @__PURE__ */ new Set(); for (const scope of scopes) { const trimmed = scope.trim(); if (trimmed) out.add(trimmed); } return [...out]; } function operatorScopeSatisfied(requestedScope, granted) { if (granted.has(OPERATOR_ADMIN_SCOPE) && requestedScope.startsWith(OPERATOR_SCOPE_PREFIX$1)) return true; if (requestedScope === OPERATOR_READ_SCOPE) return granted.has(OPERATOR_READ_SCOPE) || granted.has(OPERATOR_WRITE_SCOPE); if (requestedScope === OPERATOR_WRITE_SCOPE) return granted.has(OPERATOR_WRITE_SCOPE); return granted.has(requestedScope); } function roleScopesAllow(params) { const requested = normalizeScopeList(params.requestedScopes); if (requested.length === 0) return true; const allowed = normalizeScopeList(params.allowedScopes); if (allowed.length === 0) return false; const allowedSet = new Set(allowed); if (params.role.trim() !== OPERATOR_ROLE) return requested.every((scope) => allowedSet.has(scope)); return requested.every((scope) => operatorScopeSatisfied(scope, allowedSet)); } function resolveMissingRequestedScope(params) { for (const scope of params.requestedScopes) if (!roleScopesAllow({ role: params.role, requestedScopes: [scope], allowedScopes: params.allowedScopes })) return scope; return null; } //#endregion //#region src/infra/device-pairing.ts const PENDING_TTL_MS = 300 * 1e3; const OPERATOR_SCOPE_PREFIX = "operator."; const withLock = createAsyncLock(); async function loadState(baseDir) { const { pendingPath, pairedPath } = resolvePairingPaths(baseDir, "devices"); const [pending, paired] = await Promise.all([readJsonFile(pendingPath), readJsonFile(pairedPath)]); const state = { pendingById: pending ?? {}, pairedByDeviceId: paired ?? {} }; pruneExpiredPending(state.pendingById, Date.now(), PENDING_TTL_MS); return state; } async function persistState(state, baseDir) { const { pendingPath, pairedPath } = resolvePairingPaths(baseDir, "devices"); await Promise.all([writeJsonAtomic(pendingPath, state.pendingById), writeJsonAtomic(pairedPath, state.pairedByDeviceId)]); } function normalizeDeviceId(deviceId) { return deviceId.trim(); } function normalizeRole(role) { const trimmed = role?.trim(); return trimmed ? trimmed : null; } function mergeRoles(...items) { const roles = /* @__PURE__ */ new Set(); for (const item of items) { if (!item) continue; if (Array.isArray(item)) for (const role of item) { const trimmed = role.trim(); if (trimmed) roles.add(trimmed); } else { const trimmed = item.trim(); if (trimmed) roles.add(trimmed); } } if (roles.size === 0) return; return [...roles]; } function mergeScopes(...items) { const scopes = /* @__PURE__ */ new Set(); for (const item of items) { if (!item) continue; for (const scope of item) { const trimmed = scope.trim(); if (trimmed) scopes.add(trimmed); } } if (scopes.size === 0) return; return [...scopes]; } function sameStringSet(left, right) { if (left.length !== right.length) return false; const rightSet = new Set(right); for (const value of left) if (!rightSet.has(value)) return false; return true; } function resolveRequestedRoles(input) { return mergeRoles(input.roles, input.role) ?? []; } function resolveRequestedScopes(input) { return normalizeDeviceAuthScopes(input.scopes); } function samePendingApprovalSnapshot(existing, incoming) { if (existing.publicKey !== incoming.publicKey) return false; if (normalizeRole(existing.role) !== normalizeRole(incoming.role)) return false; if (!sameStringSet(resolveRequestedRoles(existing), resolveRequestedRoles(incoming)) || !sameStringSet(resolveRequestedScopes(existing), resolveRequestedScopes(incoming))) return false; return true; } function refreshPendingDevicePairingRequest(existing, incoming, isRepair) { return { ...existing, publicKey: incoming.publicKey, displayName: incoming.displayName ?? existing.displayName, platform: incoming.platform ?? existing.platform, deviceFamily: incoming.deviceFamily ?? existing.deviceFamily, clientId: incoming.clientId ?? existing.clientId, clientMode: incoming.clientMode ?? existing.clientMode, remoteIp: incoming.remoteIp ?? existing.remoteIp, silent: Boolean(existing.silent && incoming.silent), isRepair: existing.isRepair || isRepair, ts: Date.now() }; } function resolveSupersededPendingSilent(params) { return Boolean(params.incomingSilent && params.existing.every((pending) => pending.silent === true)); } function buildPendingDevicePairingRequest(params) { const role = normalizeRole(params.req.role) ?? void 0; return { requestId: params.requestId ?? randomUUID(), deviceId: params.deviceId, publicKey: params.req.publicKey, displayName: params.req.displayName, platform: params.req.platform, deviceFamily: params.req.deviceFamily, clientId: params.req.clientId, clientMode: params.req.clientMode, role, roles: mergeRoles(params.req.roles, role), scopes: mergeScopes(params.req.scopes), remoteIp: params.req.remoteIp, silent: params.req.silent, isRepair: params.isRepair, ts: Date.now() }; } function resolvePendingApprovalRole(pending) { const role = normalizeRole(pending.role); if (role) return role; if (!Array.isArray(pending.roles)) return null; for (const candidate of pending.roles) { const normalized = normalizeRole(candidate); if (normalized) return normalized; } return null; } function newToken() { return generatePairingToken(); } function getPairedDeviceFromState(state, deviceId) { return state.pairedByDeviceId[normalizeDeviceId(deviceId)] ?? null; } function cloneDeviceTokens(device) { return device.tokens ? { ...device.tokens } : {}; } function buildDeviceAuthToken(params) { return { token: newToken(), role: params.role, scopes: params.scopes, createdAtMs: params.existing?.createdAtMs ?? params.now, rotatedAtMs: params.rotatedAtMs, revokedAtMs: void 0, lastUsedAtMs: params.existing?.lastUsedAtMs }; } function resolveApprovedDeviceScopeBaseline(device) { const baseline = device.approvedScopes ?? device.scopes; if (!Array.isArray(baseline)) return null; return normalizeDeviceAuthScopes(baseline); } function scopesWithinApprovedDeviceBaseline(params) { if (!params.approvedScopes) return false; return roleScopesAllow({ role: params.role, requestedScopes: params.scopes, allowedScopes: params.approvedScopes }); } async function listDevicePairing(baseDir) { const state = await loadState(baseDir); return { pending: Object.values(state.pendingById).toSorted((a, b) => b.ts - a.ts), paired: Object.values(state.pairedByDeviceId).toSorted((a, b) => b.approvedAtMs - a.approvedAtMs) }; } async function getPairedDevice(deviceId, baseDir) { return (await loadState(baseDir)).pairedByDeviceId[normalizeDeviceId(deviceId)] ?? null; } async function requestDevicePairing(req, baseDir) { return await withLock(async () => { const state = await loadState(baseDir); const deviceId = normalizeDeviceId(req.deviceId); if (!deviceId) throw new Error("deviceId required"); const isRepair = Boolean(state.pairedByDeviceId[deviceId]); const pendingForDevice = Object.values(state.pendingById).filter((pending) => pending.deviceId === deviceId).toSorted((left, right) => right.ts - left.ts); const latestPending = pendingForDevice[0]; if (latestPending && pendingForDevice.length === 1) { if (samePendingApprovalSnapshot(latestPending, req)) { const refreshed = refreshPendingDevicePairingRequest(latestPending, req, isRepair); state.pendingById[latestPending.requestId] = refreshed; await persistState(state, baseDir); return { status: "pending", request: refreshed, created: false }; } } if (pendingForDevice.length > 0) { const mergedRoles = mergeRoles(...pendingForDevice.flatMap((pending) => [pending.roles, pending.role]), req.roles, req.role); const mergedScopes = mergeScopes(...pendingForDevice.map((pending) => pending.scopes), req.scopes); for (const pending of pendingForDevice) delete state.pendingById[pending.requestId]; const superseded = buildPendingDevicePairingRequest({ deviceId, isRepair, req: { ...req, role: normalizeRole(req.role) ?? latestPending?.role, roles: mergedRoles, scopes: mergedScopes, silent: resolveSupersededPendingSilent({ existing: pendingForDevice, incomingSilent: req.silent }) } }); state.pendingById[superseded.requestId] = superseded; await persistState(state, baseDir); return { status: "pending", request: superseded, created: true }; } const request = buildPendingDevicePairingRequest({ deviceId, isRepair, req }); state.pendingById[request.requestId] = request; await persistState(state, baseDir); return { status: "pending", request, created: true }; }); } async function approveDevicePairing(requestId, optionsOrBaseDir, maybeBaseDir) { const options = typeof optionsOrBaseDir === "string" || optionsOrBaseDir === void 0 ? void 0 : optionsOrBaseDir; const baseDir = typeof optionsOrBaseDir === "string" ? optionsOrBaseDir : maybeBaseDir; return await withLock(async () => { const state = await loadState(baseDir); const pending = state.pendingById[requestId]; if (!pending) return null; const approvalRole = resolvePendingApprovalRole(pending); if (approvalRole && options?.callerScopes) { const missingScope = resolveMissingRequestedScope({ role: approvalRole, requestedScopes: normalizeDeviceAuthScopes(pending.scopes).filter((scope) => scope.startsWith(OPERATOR_SCOPE_PREFIX)), allowedScopes: options.callerScopes }); if (missingScope) return { status: "forbidden", missingScope }; } const now = Date.now(); const existing = state.pairedByDeviceId[pending.deviceId]; const roles = mergeRoles(existing?.roles, existing?.role, pending.roles, pending.role); const approvedScopes = mergeScopes(existing?.approvedScopes ?? existing?.scopes, pending.scopes); const tokens = existing?.tokens ? { ...existing.tokens } : {}; const roleForToken = normalizeRole(pending.role); if (roleForToken) { const existingToken = tokens[roleForToken]; const requestedScopes = normalizeDeviceAuthScopes(pending.scopes); const nextScopes = requestedScopes.length > 0 ? requestedScopes : normalizeDeviceAuthScopes(existingToken?.scopes ?? approvedScopes ?? existing?.approvedScopes ?? existing?.scopes); const now = Date.now(); tokens[roleForToken] = { token: newToken(), role: roleForToken, scopes: nextScopes, createdAtMs: existingToken?.createdAtMs ?? now, rotatedAtMs: existingToken ? now : void 0, revokedAtMs: void 0, lastUsedAtMs: existingToken?.lastUsedAtMs }; } const device = { deviceId: pending.deviceId, publicKey: pending.publicKey, displayName: pending.displayName, platform: pending.platform, deviceFamily: pending.deviceFamily, clientId: pending.clientId, clientMode: pending.clientMode, role: pending.role, roles, scopes: approvedScopes, approvedScopes, remoteIp: pending.remoteIp, tokens, createdAtMs: existing?.createdAtMs ?? now, approvedAtMs: now }; delete state.pendingById[requestId]; state.pairedByDeviceId[device.deviceId] = device; await persistState(state, baseDir); return { status: "approved", requestId, device }; }); } async function rejectDevicePairing(requestId, baseDir) { return await withLock(async () => { return await rejectPendingPairingRequest({ requestId, idKey: "deviceId", loadState: () => loadState(baseDir), persistState: (state) => persistState(state, baseDir), getId: (pending) => pending.deviceId }); }); } async function removePairedDevice(deviceId, baseDir) { return await withLock(async () => { const state = await loadState(baseDir); const normalized = normalizeDeviceId(deviceId); if (!normalized || !state.pairedByDeviceId[normalized]) return null; delete state.pairedByDeviceId[normalized]; await persistState(state, baseDir); return { deviceId: normalized }; }); } async function updatePairedDeviceMetadata(deviceId, patch, baseDir) { return await withLock(async () => { const state = await loadState(baseDir); const existing = state.pairedByDeviceId[normalizeDeviceId(deviceId)]; if (!existing) return; const roles = mergeRoles(existing.roles, existing.role, patch.role); const scopes = mergeScopes(existing.scopes, patch.scopes); state.pairedByDeviceId[deviceId] = { ...existing, ...patch, deviceId: existing.deviceId, createdAtMs: existing.createdAtMs, approvedAtMs: existing.approvedAtMs, approvedScopes: existing.approvedScopes, role: patch.role ?? existing.role, roles, scopes }; await persistState(state, baseDir); }); } function summarizeDeviceTokens(tokens) { if (!tokens) return; const summaries = Object.values(tokens).map((token) => ({ role: token.role, scopes: token.scopes, createdAtMs: token.createdAtMs, rotatedAtMs: token.rotatedAtMs, revokedAtMs: token.revokedAtMs, lastUsedAtMs: token.lastUsedAtMs })).toSorted((a, b) => a.role.localeCompare(b.role)); return summaries.length > 0 ? summaries : void 0; } async function verifyDeviceToken(params) { return await withLock(async () => { const state = await loadState(params.baseDir); const device = getPairedDeviceFromState(state, params.deviceId); if (!device) return { ok: false, reason: "device-not-paired" }; const role = normalizeRole(params.role); if (!role) return { ok: false, reason: "role-missing" }; const entry = device.tokens?.[role]; if (!entry) return { ok: false, reason: "token-missing" }; if (entry.revokedAtMs) return { ok: false, reason: "token-revoked" }; if (!verifyPairingToken(params.token, entry.token)) return { ok: false, reason: "token-mismatch" }; const approvedScopes = resolveApprovedDeviceScopeBaseline(device); if (!scopesWithinApprovedDeviceBaseline({ role, scopes: entry.scopes, approvedScopes })) return { ok: false, reason: "scope-mismatch" }; if (!roleScopesAllow({ role, requestedScopes: normalizeDeviceAuthScopes(params.scopes), allowedScopes: entry.scopes })) return { ok: false, reason: "scope-mismatch" }; entry.lastUsedAtMs = Date.now(); device.tokens ??= {}; device.tokens[role] = entry; state.pairedByDeviceId[device.deviceId] = device; await persistState(state, params.baseDir); return { ok: true }; }); } async function ensureDeviceToken(params) { return await withLock(async () => { const state = await loadState(params.baseDir); const requestedScopes = normalizeDeviceAuthScopes(params.scopes); const context = resolveDeviceTokenUpdateContext({ state, deviceId: params.deviceId, role: params.role }); if (!context) return null; const { device, role, tokens, existing } = context; const approvedScopes = resolveApprovedDeviceScopeBaseline(device); if (!scopesWithinApprovedDeviceBaseline({ role, scopes: requestedScopes, approvedScopes })) return null; if (existing && !existing.revokedAtMs) { if (scopesWithinApprovedDeviceBaseline({ role, scopes: existing.scopes, approvedScopes }) && roleScopesAllow({ role, requestedScopes, allowedScopes: existing.scopes })) return existing; } const now = Date.now(); const next = buildDeviceAuthToken({ role, scopes: requestedScopes, existing, now, rotatedAtMs: existing ? now : void 0 }); tokens[role] = next; device.tokens = tokens; state.pairedByDeviceId[device.deviceId] = device; await persistState(state, params.baseDir); return next; }); } function resolveDeviceTokenUpdateContext(params) { const device = getPairedDeviceFromState(params.state, params.deviceId); if (!device) return null; const role = normalizeRole(params.role); if (!role) return null; const tokens = cloneDeviceTokens(device); return { device, role, tokens, existing: tokens[role] }; } async function rotateDeviceToken(params) { return await withLock(async () => { const state = await loadState(params.baseDir); const context = resolveDeviceTokenUpdateContext({ state, deviceId: params.deviceId, role: params.role }); if (!context) return { ok: false, reason: "unknown-device-or-role" }; const { device, role, tokens, existing } = context; const requestedScopes = normalizeDeviceAuthScopes(params.scopes ?? existing?.scopes ?? device.scopes); const approvedScopes = resolveApprovedDeviceScopeBaseline(device); if (!approvedScopes) return { ok: false, reason: "missing-approved-scope-baseline" }; if (!scopesWithinApprovedDeviceBaseline({ role, scopes: requestedScopes, approvedScopes })) return { ok: false, reason: "scope-outside-approved-baseline" }; const now = Date.now(); const next = buildDeviceAuthToken({ role, scopes: requestedScopes, existing, now, rotatedAtMs: now }); tokens[role] = next; device.tokens = tokens; state.pairedByDeviceId[device.deviceId] = device; await persistState(state, params.baseDir); return { ok: true, entry: next }; }); } async function revokeDeviceToken(params) { return await withLock(async () => { const state = await loadState(params.baseDir); const device = state.pairedByDeviceId[normalizeDeviceId(params.deviceId)]; if (!device) return null; const role = normalizeRole(params.role); if (!role) return null; if (!device.tokens?.[role]) return null; const tokens = { ...device.tokens }; const entry = { ...tokens[role], revokedAtMs: Date.now() }; tokens[role] = entry; device.tokens = tokens; state.pairedByDeviceId[device.deviceId] = device; await persistState(state, params.baseDir); return entry; }); } //#endregion export { rejectDevicePairing as a, revokeDeviceToken as c, updatePairedDeviceMetadata as d, verifyDeviceToken as f, listDevicePairing as i, rotateDeviceToken as l, roleScopesAllow as m, ensureDeviceToken as n, removePairedDevice as o, resolveMissingRequestedScope as p, getPairedDevice as r, requestDevicePairing as s, approveDevicePairing as t, summarizeDeviceTokens as u };