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

clientRpcPermissions.ts

59 lines3.0 KB

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