Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

refactor: Update token route to use validateScopesForRole for scope validation

Kiran K5cd62eff9658
diff --git a/apps/web/app/api/tokens/[id]/route.ts b/apps/web/app/api/tokens/[id]/route.ts
index 515babb2de..91f9572b86 100644
--- a/apps/web/app/api/tokens/[id]/route.ts
+++ b/apps/web/app/api/tokens/[id]/route.ts
@@ -1,5 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
-import { ROLE_SCOPES_MAP } from "@/lib/api/tokens/scopes";
+import { validateScopesForRole } from "@/lib/api/tokens/scopes";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
@@ -66,11 +66,7 @@ export const PATCH = withWorkspace(
},
});
- // Check given scopes are valid based on user's role
- if (
- scopes?.length &&
- scopes.every((scope) => !ROLE_SCOPES_MAP[role].includes(scope))
- ) {
+ if (!validateScopesForRole(scopes, role)) {
throw new DubApiError({
code: "unprocessable_entity",
message: "Some of the given scopes are not available for your role.",
diff --git a/apps/web/app/api/tokens/route.ts b/apps/web/app/api/tokens/route.ts
index c661854ce7..171a23a67b 100644
--- a/apps/web/app/api/tokens/route.ts
+++ b/apps/web/app/api/tokens/route.ts
@@ -1,5 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
-import { ROLE_SCOPES_MAP, scopesToName } from "@/lib/api/tokens/scopes";
+import { scopesToName, validateScopesForRole } from "@/lib/api/tokens/scopes";
import { parseRequestBody } from "@/lib/api/utils";
import { hashToken, withWorkspace } from "@/lib/auth";
import { generateRandomName } from "@/lib/names";
@@ -41,11 +41,7 @@ export const POST = withWorkspace(
});
}
- // Check given scopes are valid based on user's role
- if (
- scopes?.length &&
- scopes.every((scope) => !ROLE_SCOPES_MAP[role].includes(scope))
- ) {
+ if (!validateScopesForRole(scopes || [], role)) {
throw new DubApiError({
code: "unprocessable_entity",
message: "Some of the given scopes are not available for your role.",
diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.ts
index 7a294b0cfe..9f9a64e942 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -10,8 +10,6 @@ export const SCOPES_NAMES = [
"analytics.read",
"domains.read",
"domains.write",
- "tokens.read",
- "tokens.write",
"conversions.write",
"apis.all", // All API scopes
"apis.read", // All read scopes
@@ -63,12 +61,6 @@ export const RESOURCES: Resource[] = [
description: "Create, read, update, and delete tags",
betaFeature: false,
},
- {
- name: "API Keys",
- key: "tokens",
- description: "Create, read, update, and delete API keys",
- betaFeature: false,
- },
{
name: "Conversions",
key: "conversions",
@@ -134,20 +126,6 @@ export const SCOPES: MasterScope[] = [
type: "write",
resource: "workspaces",
},
- {
- scope: "tokens.read",
- roles: ["owner", "member"],
- permissions: ["tokens.read"],
- type: "read",
- resource: "tokens",
- },
- {
- scope: "tokens.write",
- roles: ["owner", "member"],
- permissions: ["tokens.write", "tokens.read"],
- type: "write",
- resource: "tokens",
- },
{
scope: "analytics.read",
roles: ["owner", "member"],
@@ -377,6 +355,15 @@ export const scopesToName = (scopes: string[]) => {
};
};
+export const validateScopesForRole = (scopes: Scope[], role: Role) => {
+ const allowedScopes = ROLE_SCOPES_MAP[role];
+ const invalidScopes = scopes.filter(
+ (scope) => !allowedScopes.includes(scope),
+ );
+
+ return !(invalidScopes.length > 0);
+};
+
export type Scope = (typeof SCOPES_NAMES)[number];
export type PermissionAction = (typeof PERMISSION_ACTIONS)[number];