mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 14:38:25 +00:00
refactor(gateway): extract connect and role policy logic
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import { formatControlPlaneActor, resolveControlPlaneActor } from "./control-plane-audit.js";
|
||||
import { consumeControlPlaneWriteBudget } from "./control-plane-rate-limit.js";
|
||||
import {
|
||||
ADMIN_SCOPE,
|
||||
authorizeOperatorScopesForMethod,
|
||||
isNodeRoleMethod,
|
||||
} from "./method-scopes.js";
|
||||
import { ADMIN_SCOPE, authorizeOperatorScopesForMethod } from "./method-scopes.js";
|
||||
import { ErrorCodes, errorShape } from "./protocol/index.js";
|
||||
import { isRoleAuthorizedForMethod, parseGatewayRole } from "./role-policy.js";
|
||||
import { agentHandlers } from "./server-methods/agent.js";
|
||||
import { agentsHandlers } from "./server-methods/agents.js";
|
||||
import { browserHandlers } from "./server-methods/browser.js";
|
||||
@@ -42,19 +39,17 @@ function authorizeGatewayMethod(method: string, client: GatewayRequestOptions["c
|
||||
if (method === "health") {
|
||||
return null;
|
||||
}
|
||||
const role = client.connect.role ?? "operator";
|
||||
const roleRaw = client.connect.role ?? "operator";
|
||||
const role = parseGatewayRole(roleRaw);
|
||||
if (!role) {
|
||||
return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${roleRaw}`);
|
||||
}
|
||||
const scopes = client.connect.scopes ?? [];
|
||||
if (isNodeRoleMethod(method)) {
|
||||
if (role === "node") {
|
||||
return null;
|
||||
}
|
||||
if (!isRoleAuthorizedForMethod(role, method)) {
|
||||
return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${role}`);
|
||||
}
|
||||
if (role === "node") {
|
||||
return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${role}`);
|
||||
}
|
||||
if (role !== "operator") {
|
||||
return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${role}`);
|
||||
return null;
|
||||
}
|
||||
if (scopes.includes(ADMIN_SCOPE)) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user