The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
chore: Refactor token permissions logic
Kiran Kfeeb945ded9c
diff --git a/apps/web/lib/api/tokens/permissions.ts b/apps/web/lib/api/tokens/permissions.tsindex 749aa4b75e..5536437628 100644--- a/apps/web/lib/api/tokens/permissions.ts+++ b/apps/web/lib/api/tokens/permissions.ts@@ -1,5 +1,5 @@import { DubApiError } from "../errors";-import { Scope, scopeMapping } from "./scopes";+import { Scope } from "./scopes";// Check if the required scope is in the list of user scopesexport const throwIfNoAccess = ({@@ -15,16 +15,12 @@ export const throwIfNoAccess = ({return;}- const userScopes: Scope[] = scopes- .map((scope) => scopeMapping[scope] || scope)- .flat();-const missingScopes = requiredScopes- .filter((requiredScope) => !userScopes.includes(requiredScope))+ .filter((requiredScope) => !scopes.includes(requiredScope)).join(", ");for (const requiredScope of requiredScopes) {- if (!userScopes.includes(requiredScope)) {+ if (!scopes.includes(requiredScope)) {throw new DubApiError({code: "forbidden",message: `The provided key does not have the required permissions for this endpoint on the workspace 'ws_${workspaceId}'. Having the '${missingScopes}' permission would allow this request to continue.`,diff --git a/apps/web/lib/auth/workspace-edge.ts b/apps/web/lib/auth/workspace-edge.tsindex a1c3960bfa..647510ff9f 100644--- a/apps/web/lib/auth/workspace-edge.ts+++ b/apps/web/lib/auth/workspace-edge.ts@@ -11,7 +11,7 @@ import { StreamingTextResponse } from "ai";import { getToken } from "next-auth/jwt";import { NextRequest } from "next/server";import { throwIfNoAccess } from "../api/tokens/permissions";-import { Scope, roleScopesMapping } from "../api/tokens/scopes";+import { Scope, getScopesByRole, normalizeScopes } from "../api/tokens/scopes";import { isBetaTester } from "../edge-config";import { prismaEdge } from "../prisma/edge";import { hashToken } from "./hash-token";@@ -252,11 +252,19 @@ export const withWorkspaceEdge = (});}- // Find scopes based on the token or user's role- if (token && "scopes" in token) {- scopes = (token.scopes?.split(" ") as Scope[]) || [];+ // Find the subset of permissions that the user has access to based on their role+ // Only owner can create machine users now+ const userScopes = getScopesByRole(+ session.user.isMachine ? "owner" : workspace.users[0].role,+ );++ if (isRestrictedToken) {+ const tokenScopes = (token.scopes.split(" ") || []) as Scope[];+ scopes = normalizeScopes(tokenScopes).filter((scope) =>+ userScopes.includes(scope),+ );} else {- scopes = roleScopesMapping[workspace.users[0].role];+ scopes = userScopes;}// Check user has permission to make the actiondiff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.tsindex aa7c213f2a..2d5e78ee02 100644--- a/apps/web/lib/auth/workspace.ts+++ b/apps/web/lib/auth/workspace.ts@@ -5,7 +5,7 @@ import { ratelimit } from "@/lib/upstash";import { API_DOMAIN, getSearchParams } from "@dub/utils";import { waitUntil } from "@vercel/functions";import { throwIfNoAccess } from "../api/tokens/permissions";-import { Scope, roleScopesMapping } from "../api/tokens/scopes";+import { Scope, getScopesByRole, normalizeScopes } from "../api/tokens/scopes";import { isBetaTester } from "../edge-config";import { hashToken } from "./hash-token";import { Session, getSession } from "./utils";@@ -277,11 +277,24 @@ export const withWorkspace = (}}- // Find scopes based on the token or user's role- if (token && "scopes" in token) {- scopes = (token.scopes?.split(" ") as Scope[]) || [];- } else if (workspace.users.length > 0) {- scopes = roleScopesMapping[workspace.users[0].role];+ // Find the subset of permissions that the user has access to based on their role+ // Only owner can create machine users now+ const userScopes = getScopesByRole(+ session.user.isMachine ? "owner" : workspace.users[0].role,+ );++ if (isRestrictedToken) {+ const tokenScopes = (token.scopes.split(" ") || []) as Scope[];+ scopes = normalizeScopes(tokenScopes).filter((scope) =>+ userScopes.includes(scope),+ );+ } else {+ scopes = userScopes;+ }++ if (isRestrictedToken) {+ console.log("session", session);+ console.log("scopes", scopes || []);}// Check user has permission to make the action