The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
refactor: Update token route to use ROLE_SCOPES_MAP for scope validation
Kiran K317b464c2e8f
diff --git a/apps/web/app/api/tokens/[id]/route.ts b/apps/web/app/api/tokens/[id]/route.tsindex 80b7e7ceda..515babb2de 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 { getScopesByRole } from "@/lib/api/tokens/scopes";+import { ROLE_SCOPES_MAP } from "@/lib/api/tokens/scopes";import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";@@ -67,11 +67,9 @@ export const PATCH = withWorkspace(});// Check given scopes are valid based on user's role- const userScopes = getScopesByRole(role);-if (scopes?.length &&- scopes.every((scope) => !userScopes.includes(scope))+ scopes.every((scope) => !ROLE_SCOPES_MAP[role].includes(scope))) {throw new DubApiError({code: "unprocessable_entity",diff --git a/apps/web/app/api/tokens/route.ts b/apps/web/app/api/tokens/route.tsindex 8ce7046aed..c661854ce7 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 { getScopesByRole, scopesToName } from "@/lib/api/tokens/scopes";+import { ROLE_SCOPES_MAP, scopesToName } from "@/lib/api/tokens/scopes";import { parseRequestBody } from "@/lib/api/utils";import { hashToken, withWorkspace } from "@/lib/auth";import { generateRandomName } from "@/lib/names";@@ -42,11 +42,9 @@ export const POST = withWorkspace(}// Check given scopes are valid based on user's role- const userScopes = getScopesByRole(role);-if (scopes?.length &&- scopes.every((scope) => !userScopes.includes(scope))+ scopes.every((scope) => !ROLE_SCOPES_MAP[role].includes(scope))) {throw new DubApiError({code: "unprocessable_entity",diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.tsindex 3286323df9..0865d44013 100644--- a/apps/web/lib/api/tokens/scopes.ts+++ b/apps/web/lib/api/tokens/scopes.ts@@ -82,75 +82,62 @@ export const SCOPES_BY_RESOURCE: ScopeByResource = {{type: "read",scope: "links.read",- description: "Read access to links",},{type: "write",scope: "links.write",- description: "Read and Write access to links",},],analytics: [{type: "read",scope: "analytics.read",- description: "Read access to analytics and events",},],workspaces: [{type: "read",scope: "workspaces.read",- description: "Read access to workspace",},{type: "write",scope: "workspaces.write",- description: "Read and Write access to workspace",},],domains: [{type: "read",scope: "domains.read",- description: "Read access to domains",},{type: "write",scope: "domains.write",- description: "Read and Write access to domains",},],tags: [{type: "read",scope: "tags.read",- description: "Read access to tags",},{type: "write",scope: "tags.write",- description: "Read and Write access to tags",},],tokens: [{type: "read",scope: "tokens.read",- description: "Read access to Workspace API keys",},{type: "write",scope: "tokens.write",- description: "Read and Write access to Workspace API keys",},],conversions: [{type: "write",scope: "conversions.write",- description:- "Read and Write access to conversions. Able to track customer, lead, and sales events",},],};@@ -158,84 +145,72 @@ export const SCOPES_BY_RESOURCE: ScopeByResource = {export const PERMISSIONS: Permission[] = [{action: "links.read",- scope: "links.read",roles: ["owner", "member"],resource: "links",betaFeature: false,},{action: "links.write",- scope: "links.write",roles: ["owner", "member"],resource: "links",betaFeature: false,},{action: "analytics.read",- scope: "analytics.read",roles: ["owner", "member"],resource: "analytics",betaFeature: false,},{action: "workspaces.read",- scope: "workspaces.read",roles: ["owner", "member"],resource: "workspaces",betaFeature: false,},{action: "workspaces.write",- scope: "workspaces.write",roles: ["owner"],resource: "workspaces",betaFeature: false,},{action: "domains.read",- scope: "domains.read",roles: ["owner", "member"],resource: "domains",betaFeature: false,},{action: "domains.write",- scope: "domains.write",roles: ["owner"],resource: "domains",betaFeature: false,},{action: "tags.read",- scope: "tags.read",roles: ["owner", "member"],resource: "tags",betaFeature: false,},{action: "tags.write",- scope: "tags.write",roles: ["owner", "member"],resource: "tags",betaFeature: false,},{action: "tokens.read",- scope: "tokens.read",roles: ["owner", "member"],resource: "tokens",betaFeature: false,},{action: "tokens.write",- scope: "tokens.write",roles: ["owner", "member"],resource: "tokens",betaFeature: false,},{action: "conversions.write",- scope: "conversions.write",roles: ["owner"],resource: "conversions",betaFeature: true,@@ -260,6 +235,39 @@ export const SCOPE_PERMISSIONS_MAP: Record<Scope, PermissionAction[]> = {"apis.read": PERMISSION_ACTIONS.filter((action) => action.endsWith(".read")),};+// Role to scopes mapping+export const ROLE_SCOPES_MAP: Record<Role, Scope[]> = {+ owner: [+ "workspaces.read",+ "workspaces.write",+ "links.read",+ "links.write",+ "tags.read",+ "tags.write",+ "analytics.read",+ "domains.read",+ "domains.write",+ "tokens.read",+ "tokens.write",+ "conversions.write",+ "apis.all",+ "apis.read",+ ],+ member: [+ "workspaces.read",+ "links.read",+ "links.write",+ "tags.read",+ "tags.write",+ "analytics.read",+ "domains.read",+ "tokens.read",+ "tokens.write",+ "apis.read",+ "apis.all",+ ],+};+// // For each scope, get the permissions it grants access to and return array of permissionsexport const mapScopesToPermissions = (scopes: Scope[]) => {const permissions: PermissionAction[] = [];@@ -282,46 +290,23 @@ export const getPermissionsByRole = (role: Role) => {// Get SCOPES_BY_RESOURCE based on user role in a workspaceexport const getScopesByResourceForRole = (role: Role) => {- const allowedResources: string[] = [];-- // Filter permissions by the role- const allowedPermissions = PERMISSIONS.filter((permission) =>- permission.roles.includes(role),- );-- // Get resources allowed by permissions- allowedPermissions.forEach((permission) => {- if (!allowedResources.includes(permission.resource)) {- allowedResources.push(permission.resource);- }- });-- // Filter SCOPES_BY_RESOURCE based on allowed resources and scopes+ const allowedScopes = ROLE_SCOPES_MAP[role];// @ts-ignoreconst scopedResources: ScopeByResource = {};- allowedResources.forEach((resource) => {- scopedResources[resource] = SCOPES_BY_RESOURCE[resource].filter((scope) =>- allowedPermissions.some(- (permission) =>- permission.scope === scope.scope && permission.resource === resource,- ),- );- });-- return scopedResources;-};--// Get scopes by role-export const getScopesByRole = (role: Role) => {- const permissions = getPermissionsByRole(role);- const scopes = PERMISSIONS.filter((p) => permissions.includes(p.action)).map(- (permission) => permission.scope,- );+ Object.keys(SCOPES_BY_RESOURCE).forEach((resourceKey) => {+ SCOPES_BY_RESOURCE[resourceKey].forEach((scope) => {+ if (allowedScopes.includes(scope.scope)) {+ if (!scopedResources[resourceKey]) {+ scopedResources[resourceKey] = [];+ }- scopes.push("apis.read", "apis.all");+ scopedResources[resourceKey].push(scope);+ }+ });+ });- return scopes;+ return scopedResources;};export const scopePresets = [@@ -385,7 +370,6 @@ type Resource = {type Permission = {action: PermissionAction;- scope: Scope;roles: Role[];resource: ResourceKeys;betaFeature: boolean;@@ -395,6 +379,5 @@ export type ScopeByResource = {[key in ResourceKeys]: {scope: Scope;type: "read" | "write";- description: string;}[];};diff --git a/apps/web/ui/modals/add-edit-token-modal.tsx b/apps/web/ui/modals/add-edit-token-modal.tsxindex 15368164d0..b5d56dd48e 100644--- a/apps/web/ui/modals/add-edit-token-modal.tsx+++ b/apps/web/ui/modals/add-edit-token-modal.tsx@@ -130,7 +130,7 @@ function AddEditTokenModal({const scopesByResources = transformScopesForUI(getScopesByResourceForRole(isOwner ? "owner" : "member"),- ).filter((resource) => resource.betaFeature === betaTester);+ ).filter((resource) => !resource.betaFeature || betaTester);return (<>