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: Improve permission checking and scope handling

Kiran K2330903e1dd7
diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.ts
index 17d19d2de9..3286323df9 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -242,32 +242,31 @@ export const PERMISSIONS: Permission[] = [
},
];
-// For each scope, get the permissions it grants access to and return array of permissions
+// Scope to permission mapping
+export const SCOPE_PERMISSIONS_MAP: Record<Scope, PermissionAction[]> = {
+ "links.read": ["links.read"],
+ "links.write": ["links.write", "links.read"],
+ "tags.read": ["tags.read"],
+ "tags.write": ["tags.write", "tags.read"],
+ "analytics.read": ["analytics.read"],
+ "workspaces.read": ["workspaces.read"],
+ "workspaces.write": ["workspaces.write", "workspaces.read"],
+ "domains.read": ["domains.read"],
+ "domains.write": ["domains.write", "domains.read"],
+ "tokens.read": ["tokens.read"],
+ "tokens.write": ["tokens.write", "tokens.read"],
+ "conversions.write": ["conversions.write"],
+ "apis.all": PERMISSION_ACTIONS.map((action) => action),
+ "apis.read": PERMISSION_ACTIONS.filter((action) => action.endsWith(".read")),
+};
+
+// // For each scope, get the permissions it grants access to and return array of permissions
export const mapScopesToPermissions = (scopes: Scope[]) => {
const permissions: PermissionAction[] = [];
scopes.forEach((scope) => {
- if (scope === "apis.all") {
- // Add all individual scopes to permissions
- permissions.push(
- ...SCOPES.filter((s) => s !== "apis.all" && s !== "apis.read").flatMap(
- (s) => PERMISSIONS.filter((p) => p.scope === s).map((p) => p.action),
- ),
- );
- } else if (scope === "apis.read") {
- // Add all read scopes to permissions
- permissions.push(
- ...SCOPES.filter((s) => s.endsWith(".read")).flatMap((s) =>
- PERMISSIONS.filter((p) => p.scope === s).map((p) => p.action),
- ),
- );
- } else {
- // Add the scope as is
- permissions.push(
- ...PERMISSIONS.filter((permission) => permission.scope === scope).map(
- (permission) => permission.action,
- ),
- );
+ if (SCOPE_PERMISSIONS_MAP[scope]) {
+ permissions.push(...SCOPE_PERMISSIONS_MAP[scope]);
}
});
diff --git a/apps/web/lib/auth/workspace-edge.ts b/apps/web/lib/auth/workspace-edge.ts
index 654104fee3..d97520a00f 100644
--- a/apps/web/lib/auth/workspace-edge.ts
+++ b/apps/web/lib/auth/workspace-edge.ts
@@ -13,6 +13,7 @@ import { NextRequest } from "next/server";
import { throwIfNoAccess } from "../api/tokens/permissions";
import {
PermissionAction,
+ Scope,
getPermissionsByRole,
mapScopesToPermissions,
} from "../api/tokens/scopes";
@@ -257,14 +258,17 @@ export const withWorkspaceEdge = (
}
// By default, the user has access to all permissions based on their role
- permissions = getPermissionsByRole(workspace.users[0].role);
+ // Machine users have owner role by default
+ permissions = getPermissionsByRole(
+ session.user.isMachine ? "owner" : workspace.users[0].role,
+ );
// Find the subset of permissions that the user has access to based on the token scopes
if (isRestrictedToken) {
- const tokenScopes = token.scopes.split(" ") || [];
+ const tokenScopes: Scope[] = token.scopes.split(" ") || [];
- mapScopesToPermissions(tokenScopes).filter((permission) =>
- permissions.includes(permission),
+ permissions = mapScopesToPermissions(tokenScopes).filter((p) =>
+ permissions.includes(p),
);
}
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index b332281214..b8d30c0af5 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -7,6 +7,7 @@ import { waitUntil } from "@vercel/functions";
import { throwIfNoAccess } from "../api/tokens/permissions";
import {
PermissionAction,
+ Scope,
getPermissionsByRole,
mapScopesToPermissions,
} from "../api/tokens/scopes";
@@ -282,15 +283,20 @@ export const withWorkspace = (
}
// By default, the user has access to all permissions based on their role
- permissions = getPermissionsByRole(workspace.users[0].role);
+ // Machine users have owner role by default
+ permissions = getPermissionsByRole(
+ session.user.isMachine ? "owner" : workspace.users[0].role,
+ );
// Find the subset of permissions that the user has access to based on the token scopes
if (isRestrictedToken) {
- const tokenScopes = token.scopes.split(" ") || [];
+ const tokenScopes: Scope[] = token.scopes.split(" ") || [];
- mapScopesToPermissions(tokenScopes).filter((permission) =>
- permissions.includes(permission),
+ permissions = mapScopesToPermissions(tokenScopes).filter((p) =>
+ permissions.includes(p),
);
+
+ console.log("permissions", permissions);
}
// Check user has permission to make the action