Files
openclaw/src/gateway/server-methods.ts

122 lines
4.4 KiB
TypeScript
Raw Normal View History

2026-01-04 14:34:23 +01:00
import { ErrorCodes, errorShape } from "./protocol/index.js";
import { agentHandlers } from "./server-methods/agent.js";
import { agentsHandlers } from "./server-methods/agents.js";
import { channelsHandlers } from "./server-methods/channels.js";
2026-01-04 14:34:23 +01:00
import { chatHandlers } from "./server-methods/chat.js";
import { configHandlers } from "./server-methods/config.js";
import { connectHandlers } from "./server-methods/connect.js";
import { cronHandlers } from "./server-methods/cron.js";
2026-01-19 02:31:18 +00:00
import { deviceHandlers } from "./server-methods/devices.js";
import { execApprovalsHandlers } from "./server-methods/exec-approvals.js";
2026-01-04 14:34:23 +01:00
import { healthHandlers } from "./server-methods/health.js";
2026-01-08 03:43:46 +00:00
import { logsHandlers } from "./server-methods/logs.js";
2026-01-04 14:34:23 +01:00
import { modelsHandlers } from "./server-methods/models.js";
import { nodeHandlers } from "./server-methods/nodes.js";
import { sendHandlers } from "./server-methods/send.js";
import { sessionsHandlers } from "./server-methods/sessions.js";
import { skillsHandlers } from "./server-methods/skills.js";
import { systemHandlers } from "./server-methods/system.js";
import { talkHandlers } from "./server-methods/talk.js";
import type { GatewayRequestHandlers, GatewayRequestOptions } from "./server-methods/types.js";
import { updateHandlers } from "./server-methods/update.js";
2026-01-07 11:42:41 +01:00
import { usageHandlers } from "./server-methods/usage.js";
2026-01-04 14:34:23 +01:00
import { voicewakeHandlers } from "./server-methods/voicewake.js";
import { webHandlers } from "./server-methods/web.js";
import { wizardHandlers } from "./server-methods/wizard.js";
2026-01-19 02:31:18 +00:00
const ADMIN_SCOPE = "operator.admin";
const APPROVALS_SCOPE = "operator.approvals";
const PAIRING_SCOPE = "operator.pairing";
const APPROVAL_METHODS = new Set(["exec.approval.request", "exec.approval.resolve"]);
2026-01-19 04:50:07 +00:00
const NODE_ROLE_METHODS = new Set(["node.invoke.result", "node.event"]);
2026-01-19 02:31:18 +00:00
const PAIRING_METHODS = new Set([
"node.pair.request",
"node.pair.list",
"node.pair.approve",
"node.pair.reject",
"node.pair.verify",
"device.pair.list",
"device.pair.approve",
"device.pair.reject",
]);
const ADMIN_METHOD_PREFIXES = ["exec.approvals."];
function authorizeGatewayMethod(method: string, client: GatewayRequestOptions["client"]) {
if (!client?.connect) return null;
const role = client.connect.role ?? "operator";
const scopes = client.connect.scopes ?? [];
2026-01-19 04:50:07 +00:00
if (role === "node") {
if (NODE_ROLE_METHODS.has(method)) return null;
return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${role}`);
}
2026-01-19 02:31:18 +00:00
if (role !== "operator") {
return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${role}`);
}
if (scopes.includes(ADMIN_SCOPE)) return null;
if (APPROVAL_METHODS.has(method) && !scopes.includes(APPROVALS_SCOPE)) {
return errorShape(ErrorCodes.INVALID_REQUEST, "missing scope: operator.approvals");
}
if (PAIRING_METHODS.has(method) && !scopes.includes(PAIRING_SCOPE)) {
return errorShape(ErrorCodes.INVALID_REQUEST, "missing scope: operator.pairing");
}
if (ADMIN_METHOD_PREFIXES.some((prefix) => method.startsWith(prefix))) {
return errorShape(ErrorCodes.INVALID_REQUEST, "missing scope: operator.admin");
}
return null;
}
2026-01-11 12:11:12 +00:00
export const coreGatewayHandlers: GatewayRequestHandlers = {
2026-01-04 14:34:23 +01:00
...connectHandlers,
2026-01-08 03:43:46 +00:00
...logsHandlers,
2026-01-04 14:34:23 +01:00
...voicewakeHandlers,
...healthHandlers,
...channelsHandlers,
2026-01-04 14:34:23 +01:00
...chatHandlers,
...cronHandlers,
2026-01-19 02:31:18 +00:00
...deviceHandlers,
...execApprovalsHandlers,
2026-01-04 14:34:23 +01:00
...webHandlers,
...modelsHandlers,
...configHandlers,
...wizardHandlers,
...talkHandlers,
...skillsHandlers,
...sessionsHandlers,
...systemHandlers,
...updateHandlers,
2026-01-04 14:34:23 +01:00
...nodeHandlers,
...sendHandlers,
2026-01-07 11:42:41 +01:00
...usageHandlers,
2026-01-04 14:34:23 +01:00
...agentHandlers,
...agentsHandlers,
2026-01-03 18:14:07 +01:00
};
export async function handleGatewayRequest(
2026-01-11 12:11:12 +00:00
opts: GatewayRequestOptions & { extraHandlers?: GatewayRequestHandlers },
2026-01-03 18:14:07 +01:00
): Promise<void> {
const { req, respond, client, isWebchatConnect, context } = opts;
2026-01-19 02:31:18 +00:00
const authError = authorizeGatewayMethod(req.method, client);
if (authError) {
respond(false, undefined, authError);
return;
}
const handler = opts.extraHandlers?.[req.method] ?? coreGatewayHandlers[req.method];
2026-01-04 14:34:23 +01:00
if (!handler) {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, `unknown method: ${req.method}`),
);
return;
2026-01-03 18:14:07 +01:00
}
2026-01-04 14:34:23 +01:00
await handler({
req,
params: (req.params ?? {}) as Record<string, unknown>,
client,
isWebchatConnect,
respond,
context,
});
2026-01-03 18:14:07 +01:00
}