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 the permission check

Kiran Ke70838f6fe4f
diff --git a/apps/web/app/api/analytics/public-stats/route.ts b/apps/web/app/api/analytics/public-stats/route.ts
index 9fd1a70985..d04cb989aa 100644
--- a/apps/web/app/api/analytics/public-stats/route.ts
+++ b/apps/web/app/api/analytics/public-stats/route.ts
@@ -17,7 +17,7 @@ export const GET = withWorkspace(
return NextResponse.json(response);
},
{
- requiredScopes: ["links.read", "links.write"],
+ requiredScopes: ["links.read"],
},
);
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index c0cff15854..ccf18a0a8e 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -50,7 +50,7 @@ export const GET = withWorkspace(
},
{
domainChecks: true,
- requiredScopes: ["domains.read", "domains.write"],
+ requiredScopes: ["domains.read"],
},
);
diff --git a/apps/web/app/api/domains/default/route.ts b/apps/web/app/api/domains/default/route.ts
index 98bfad089c..6eb0fd9577 100644
--- a/apps/web/app/api/domains/default/route.ts
+++ b/apps/web/app/api/domains/default/route.ts
@@ -34,7 +34,7 @@ export const GET = withWorkspace(
return NextResponse.json(defaultDomainsArray);
},
{
- requiredScopes: ["domains.read", "domains.write"],
+ requiredScopes: ["domains.read"],
},
);
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index 38e54e6165..f0f72857e5 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -27,7 +27,7 @@ export const GET = withWorkspace(
return NextResponse.json(z.array(DomainSchema).parse(domains));
},
{
- requiredScopes: ["domains.read", "domains.write"],
+ requiredScopes: ["domains.read"],
},
);
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 6b2d2711a7..2e4fcad9c1 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -48,7 +48,7 @@ export const GET = withWorkspace(
return NextResponse.json(response, { headers });
},
{
- requiredScopes: ["links.read", "links.write"],
+ requiredScopes: ["links.read"],
},
);
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index 14a5db10b2..22f4292d3d 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -19,6 +19,6 @@ export const GET = withWorkspace(
});
},
{
- requiredScopes: ["links.read", "links.write"],
+ requiredScopes: ["links.read"],
},
);
diff --git a/apps/web/app/api/links/info/route.ts b/apps/web/app/api/links/info/route.ts
index d9185e908c..64bf3a277f 100644
--- a/apps/web/app/api/links/info/route.ts
+++ b/apps/web/app/api/links/info/route.ts
@@ -61,6 +61,6 @@ export const GET = withWorkspace(
});
},
{
- requiredScopes: ["links.read", "links.write"],
+ requiredScopes: ["links.read"],
},
);
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index acbf2913bb..c263daa653 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -47,7 +47,7 @@ export const GET = withWorkspace(
});
},
{
- requiredScopes: ["links.read", "links.write"],
+ requiredScopes: ["links.read"],
},
);
diff --git a/apps/web/app/api/tags/route.ts b/apps/web/app/api/tags/route.ts
index 2b79cd2a58..887d58867d 100644
--- a/apps/web/app/api/tags/route.ts
+++ b/apps/web/app/api/tags/route.ts
@@ -24,7 +24,7 @@ export const GET = withWorkspace(
return NextResponse.json(tags, { headers });
},
{
- requiredScopes: ["tags.read", "tags.write"],
+ requiredScopes: ["tags.read"],
},
);
diff --git a/apps/web/app/api/tokens/[id]/route.ts b/apps/web/app/api/tokens/[id]/route.ts
index 3b9d02ac47..f023a3ba66 100644
--- a/apps/web/app/api/tokens/[id]/route.ts
+++ b/apps/web/app/api/tokens/[id]/route.ts
@@ -42,7 +42,7 @@ export const GET = withWorkspace(
return NextResponse.json(tokenSchema.parse(token));
},
{
- requiredScopes: ["tokens.read", "tokens.write"],
+ requiredScopes: ["tokens.read"],
},
);
diff --git a/apps/web/app/api/tokens/route.ts b/apps/web/app/api/tokens/route.ts
index 712daa72d0..03dc311104 100644
--- a/apps/web/app/api/tokens/route.ts
+++ b/apps/web/app/api/tokens/route.ts
@@ -105,6 +105,6 @@ export const GET = withWorkspace(
return NextResponse.json(tokenSchema.array().parse(tokens));
},
{
- requiredScopes: ["tokens.read", "tokens.write"],
+ requiredScopes: ["tokens.read"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/invites/route.tsx b/apps/web/app/api/workspaces/[idOrSlug]/invites/route.tsx
index 83a763b81f..6e35de3a4a 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/invites/route.tsx
+++ b/apps/web/app/api/workspaces/[idOrSlug]/invites/route.tsx
@@ -24,7 +24,7 @@ export const GET = withWorkspace(
return NextResponse.json(invites);
},
{
- requiredScopes: ["workspaces.read", "workspaces.write"],
+ requiredScopes: ["workspaces.read"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
index 38b65ee8d4..953ab19d33 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
@@ -47,7 +47,7 @@ export const GET = withWorkspace(
);
},
{
- requiredScopes: ["workspaces.read", "workspaces.write"],
+ requiredScopes: ["workspaces.read"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
index fbb56bc153..a799bc51fd 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
@@ -45,7 +45,7 @@ export const GET = withWorkspace(
return NextResponse.json(response);
},
{
- requiredScopes: ["workspaces.read", "workspaces.write"],
+ requiredScopes: ["workspaces.read"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
index ed0eb90e5b..e44ae37ad5 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
@@ -35,7 +35,7 @@ export const GET = withWorkspace(
});
},
{
- requiredScopes: ["workspaces.read", "workspaces.write"],
+ requiredScopes: ["workspaces.read"],
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
index a7bd969e6d..aa49f88d94 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
@@ -47,7 +47,7 @@ export const GET = withWorkspace(
);
},
{
- requiredScopes: ["workspaces.read", "workspaces.write"],
+ requiredScopes: ["workspaces.read"],
},
);
diff --git a/apps/web/lib/api/tokens/permissions.ts b/apps/web/lib/api/tokens/permissions.ts
index 836a74dd93..4505508369 100644
--- a/apps/web/lib/api/tokens/permissions.ts
+++ b/apps/web/lib/api/tokens/permissions.ts
@@ -1,8 +1,7 @@
import { DubApiError } from "../errors";
-import { Scope } from "./scopes";
+import { Scope, scopeMapping } from "./scopes";
-// Check if the required scope is in the list of scopes
-// If not, throw an error immediately with a message
+// Check if the required scope is in the list of user scopes
export const throwIfNoAccess = ({
scopes,
requiredScopes,
@@ -16,18 +15,20 @@ export const throwIfNoAccess = ({
return;
}
- for (const requiredScope of requiredScopes) {
- if (scopes.includes(requiredScope)) {
- return;
- }
- }
+ const userScopes: Scope[] = scopes
+ .map((scope) => scopeMapping[scope] || scope)
+ .flat();
const missingScopes = requiredScopes
- .filter((requiredScope) => !scopes.includes(requiredScope))
+ .filter((requiredScope) => !userScopes.includes(requiredScope))
.join(", ");
- throw new DubApiError({
- code: "forbidden",
- message: `The provided key does not have the required permissions for this endpoint on the workspace 'w_${workspaceId}'. Having the '${missingScopes}' permission would allow this request to continue.`,
- });
+ for (const requiredScope of requiredScopes) {
+ if (!userScopes.includes(requiredScope)) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: `The provided key does not have the required permissions for this endpoint on the workspace 'w_${workspaceId}'. Having the '${missingScopes}' permission would allow this request to continue.`,
+ });
+ }
+ }
};
diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.ts
index 5761344c59..e1c341db49 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -161,3 +161,10 @@ export const roleScopesMapping = resourcePermissions.reduce<
},
{} as Record<Role, Scope[]>,
);
+
+export const scopeMapping = {
+ "workspaces.write": ["workspaces.write", "workspaces.read"],
+ "links.write": ["links.write", "links.read"],
+ "tags.write": ["tags.write", "tags.read"],
+ "domains.write": ["domains.write", "domains.read"],
+};