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.

rename

Kiran K2ba358ebb410
diff --git a/apps/web/app/api/analytics/events/export/route.ts b/apps/web/app/api/analytics/events/export/route.ts
index 6a6496d70b..04715f4afe 100644
--- a/apps/web/app/api/analytics/events/export/route.ts
+++ b/apps/web/app/api/analytics/events/export/route.ts
@@ -39,7 +39,7 @@ const columnAccessors = {
// GET /api/analytics/events/export – get export data for analytics
export const GET = withWorkspace(
async ({ searchParams, workspace, link, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["analytics.read"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["analytics.read"] });
const parsedParams = eventsQuerySchema
.and(
diff --git a/apps/web/app/api/analytics/events/route.ts b/apps/web/app/api/analytics/events/route.ts
index 5febb32cc9..261d3997f2 100644
--- a/apps/web/app/api/analytics/events/route.ts
+++ b/apps/web/app/api/analytics/events/route.ts
@@ -8,7 +8,7 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(
async ({ searchParams, workspace, link, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["analytics.read"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["analytics.read"] });
const parsedParams = eventsQuerySchema.parse(searchParams);
diff --git a/apps/web/app/api/analytics/export/route.ts b/apps/web/app/api/analytics/export/route.ts
index e6c1b3499d..733cfed781 100644
--- a/apps/web/app/api/analytics/export/route.ts
+++ b/apps/web/app/api/analytics/export/route.ts
@@ -10,7 +10,7 @@ import JSZip from "jszip";
// GET /api/analytics/[endpoint]/export – get export data for analytics
export const GET = withWorkspace(
async ({ searchParams, workspace, link, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["analytics.read"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["analytics.read"] });
const parsedParams = analyticsQuerySchema.parse(searchParams);
const { domain, key, interval, start, end } = parsedParams;
diff --git a/apps/web/app/api/analytics/public-stats/route.ts b/apps/web/app/api/analytics/public-stats/route.ts
index 8bd83add84..51966dc0ac 100644
--- a/apps/web/app/api/analytics/public-stats/route.ts
+++ b/apps/web/app/api/analytics/public-stats/route.ts
@@ -12,7 +12,7 @@ const updatePublicStatsSchema = z.object({
// GET /api/analytics – get the publicStats setting for a link
export const GET = withWorkspace(async ({ searchParams, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["links.read"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["links.read"] });
const { domain, key } = domainKeySchema.parse(searchParams);
const response = await getDomainOrLink({ domain, key });
@@ -21,7 +21,7 @@ export const GET = withWorkspace(async ({ searchParams, scopes }) => {
// PUT /api/analytics – update the publicStats setting for a link
export const PUT = withWorkspace(async ({ req, searchParams, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["links.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["links.write"] });
const { domain, key } = domainKeySchema.parse(searchParams);
const { publicStats } = updatePublicStatsSchema.parse(await req.json());
diff --git a/apps/web/app/api/analytics/route.ts b/apps/web/app/api/analytics/route.ts
index 899adeacb1..5d2d1596cf 100644
--- a/apps/web/app/api/analytics/route.ts
+++ b/apps/web/app/api/analytics/route.ts
@@ -13,7 +13,7 @@ import { NextResponse } from "next/server";
// GET /api/analytics – get analytics
export const GET = withWorkspace(
async ({ params, searchParams, workspace, link, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["analytics.read"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["analytics.read"] });
let { eventType: oldEvent, endpoint: oldType } =
analyticsPathParamsSchema.parse(params);
diff --git a/apps/web/app/api/domains/[domain]/primary/route.ts b/apps/web/app/api/domains/[domain]/primary/route.ts
index 72d9f7cb50..6f8e1e073f 100644
--- a/apps/web/app/api/domains/[domain]/primary/route.ts
+++ b/apps/web/app/api/domains/[domain]/primary/route.ts
@@ -7,7 +7,7 @@ import { NextResponse } from "next/server";
// POST /api/domains/[domain]/primary – set a domain as primary
export const POST = withWorkspace(
async ({ headers, workspace, domain, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.write"] });
const responses = await Promise.all([
prisma.domain.update({
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index 51a54dafd3..bc9b89c64e 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -20,7 +20,7 @@ import { NextResponse } from "next/server";
// GET /api/domains/[domain] – get a workspace's domain
export const GET = withWorkspace(
async ({ domain, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.read"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.read"] });
const data = await prisma.domain.findUnique({
where: {
@@ -59,7 +59,7 @@ export const GET = withWorkspace(
// PUT /api/domains/[domain] – edit a workspace's domain
export const PATCH = withWorkspace(
async ({ req, workspace, domain, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.write"] });
const body = await parseRequestBody(req);
const {
@@ -140,7 +140,7 @@ export const PATCH = withWorkspace(
// DELETE /api/domains/[domain] - delete a workspace's domain
export const DELETE = withWorkspace(
async ({ domain, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.write"] });
await deleteDomainAndLinks(domain);
return NextResponse.json({ slug: domain });
diff --git a/apps/web/app/api/domains/[domain]/transfer/route.ts b/apps/web/app/api/domains/[domain]/transfer/route.ts
index 3a51177453..7bc0cf490a 100644
--- a/apps/web/app/api/domains/[domain]/transfer/route.ts
+++ b/apps/web/app/api/domains/[domain]/transfer/route.ts
@@ -15,7 +15,7 @@ import { NextResponse } from "next/server";
// POST /api/domains/[domain]/transfer – transfer a domain to another workspace
export const POST = withWorkspace(
async ({ req, headers, session, params, workspace, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.write"] });
const { domain } = params;
const { newWorkspaceId } = transferDomainBodySchema.parse(await req.json());
diff --git a/apps/web/app/api/domains/[domain]/verify/route.ts b/apps/web/app/api/domains/[domain]/verify/route.ts
index c04c44e032..7791bc27d0 100644
--- a/apps/web/app/api/domains/[domain]/verify/route.ts
+++ b/apps/web/app/api/domains/[domain]/verify/route.ts
@@ -13,7 +13,7 @@ export const maxDuration = 30;
// GET /api/domains/[domain]/verify - get domain verification status
export const GET = withWorkspace(async ({ domain, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.write"] });
let status: DomainVerificationStatusProps = "Valid Configuration";
diff --git a/apps/web/app/api/domains/default/route.ts b/apps/web/app/api/domains/default/route.ts
index 07f7129104..0cce985f24 100644
--- a/apps/web/app/api/domains/default/route.ts
+++ b/apps/web/app/api/domains/default/route.ts
@@ -7,7 +7,7 @@ import { NextResponse } from "next/server";
// GET /api/domains/default - get default domains
export const GET = withWorkspace(async ({ workspace, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.write"] });
const defaultDomains = await prisma.defaultDomains.findUnique({
where: {
@@ -42,7 +42,7 @@ const updateDefaultDomainsSchema = z.object({
// PUT /api/domains/default - edit default domains
export const PUT = withWorkspace(async ({ req, workspace, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.write"] });
const { defaultDomains } = await updateDefaultDomainsSchema.parseAsync(
await req.json(),
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index 96f1a374f6..9c8f60626c 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -15,7 +15,7 @@ import { NextResponse } from "next/server";
// GET /api/domains – get all domains for a workspace
export const GET = withWorkspace(async ({ workspace, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.read"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.read"] });
const domains = await prisma.domain.findMany({
where: {
@@ -28,7 +28,7 @@ export const GET = withWorkspace(async ({ workspace, scopes }) => {
// POST /api/domains - add a domain
export const POST = withWorkspace(async ({ req, workspace, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["domains.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["domains.write"] });
const body = await parseRequestBody(req);
const {
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index f1bd8879c2..c20950e827 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -14,7 +14,7 @@ import { NextResponse } from "next/server";
// GET /api/links – get all links for a workspace
export const GET = withWorkspace(
async ({ req, headers, workspace, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["links.read", "links.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["links.read", "links.write"] });
const searchParams = getSearchParamsWithArray(req.url);
@@ -54,7 +54,7 @@ export const GET = withWorkspace(
// POST /api/links – create a new link
export const POST = withWorkspace(
async ({ req, headers, session, workspace, scopes }) => {
- throwIfNoAccess({ scopes, requiredAnyOf: ["links.write"] });
+ throwIfNoAccess({ scopes, requiredScopes: ["links.write"] });
const bodyRaw = await parseRequestBody(req);
const body = createLinkBodySchema.parse(bodyRaw);
diff --git a/apps/web/lib/api/tokens/permissions.ts b/apps/web/lib/api/tokens/permissions.ts
index a1b05f9112..34b680d815 100644
--- a/apps/web/lib/api/tokens/permissions.ts
+++ b/apps/web/lib/api/tokens/permissions.ts
@@ -3,23 +3,19 @@ import { Scope } from "./scopes";
export const throwIfNoAccess = ({
scopes,
- requiredAnyOf,
+ requiredScopes,
}: {
scopes: Scope[];
- requiredAnyOf: Scope | Scope[];
+ requiredScopes: Scope[];
}) => {
- const requiredScopes = Array.isArray(requiredAnyOf)
- ? requiredAnyOf
- : [requiredAnyOf];
-
for (const requiredScope of requiredScopes) {
- if (scopes.includes(requiredScope)) {
- return;
+ // Check if the required scope is in the list of scopes
+ // If not, throw an error immediately with a message
+ if (!scopes.includes(requiredScope)) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: `You do not have permission to make this action.`,
+ });
}
}
-
- throw new DubApiError({
- code: "forbidden",
- message: `You do not have permission to make this action.`,
- });
};