Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

clientRpcPermissions.ts

61 lines3.1 KB

1import * as Schema from "effect/Schema";
2import { GitPreparePullRequestThreadInput } from "./git.ts";
3import {
4 AuthOrchestrationOperateScope,
5 AuthSettingsWriteScope,
6 AuthSourceControlWriteScope,
7 type AuthEnvironmentScope,
8} from "./auth.ts";
9import { WS_METHODS } from "./rpc.ts";
10​
11/** Incremental client enforcement; the server still authorizes every request. */
12export const CLIENT_GUARDED_RPC_SCOPES = {
13 [WS_METHODS.serverRunStorageCleanup]: AuthSettingsWriteScope,
14 [WS_METHODS.pullRequestsRunAction]: AuthSourceControlWriteScope,
15 [WS_METHODS.pullRequestsUpdate]: AuthSourceControlWriteScope,
16 [WS_METHODS.pullRequestsComment]: AuthSourceControlWriteScope,
17 [WS_METHODS.pullRequestsUpdateComment]: AuthSourceControlWriteScope,
18 [WS_METHODS.pullRequestsSubmitReview]: AuthSourceControlWriteScope,
19 [WS_METHODS.pullRequestsReplyToThread]: AuthSourceControlWriteScope,
20 [WS_METHODS.pullRequestsSetThreadResolution]: AuthSourceControlWriteScope,
21 [WS_METHODS.pullRequestsSetReaction]: AuthSourceControlWriteScope,
22 [WS_METHODS.pullRequestsSetFilesViewed]: AuthSourceControlWriteScope,
23 [WS_METHODS.pullRequestsRequestReviewers]: AuthSourceControlWriteScope,
24 [WS_METHODS.pullRequestsSetLabels]: AuthSourceControlWriteScope,
25 [WS_METHODS.sourceControlCloneRepository]: AuthSourceControlWriteScope,
26 [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope,
27 [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope,
28 [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope,
29 [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope,
30 [WS_METHODS.vcsPull]: AuthSourceControlWriteScope,
31 [WS_METHODS.gitRunStackedAction]: AuthSourceControlWriteScope,
32 [WS_METHODS.gitPreparePullRequestThread]: AuthSourceControlWriteScope,
33 [WS_METHODS.vcsCreateWorktree]: AuthSourceControlWriteScope,
34 [WS_METHODS.vcsRemoveWorktree]: AuthSourceControlWriteScope,
35 [WS_METHODS.vcsCreateRef]: AuthSourceControlWriteScope,
36 [WS_METHODS.vcsSwitchRef]: AuthSourceControlWriteScope,
37 [WS_METHODS.vcsInit]: AuthSourceControlWriteScope,
38​
39 [WS_METHODS.scheduledTasksUpsert]: AuthOrchestrationOperateScope,
40 [WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope,
41 [WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
42 [WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
43 [WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
44} as const;
45export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
46​
47const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput);
48​
49export function clientRpcRequiredScopes(
50 method: string,
51 input: unknown,
52): readonly AuthEnvironmentScope[] {
53 if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
54 const payload = decodePrepareThread(input);
55 if (payload.mode === "worktree" && payload.threadId !== undefined)
56 return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope];
57 }
58 return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method)
59 ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]]
60 : [];