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.

Add hasLegacyScopeGrant helper

Rafi8911c559d647
diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.ts
index f4bd897367..8d09d51e2e 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -389,6 +389,25 @@ export const grantLegacyScopePermissions = ({
return [...new Set<PermissionAction>([...permissions, ...legacyPermissions])];
};
+// True when the legacy grant gives a key permissions that its own scopes do
+// not include, so the key has more access than the dashboard shows. "All
+// access" keys already have every permission and return false.
+export const hasLegacyScopeGrant = ({
+ scopes,
+ createdAt,
+}: {
+ scopes: string[];
+ createdAt: Date | string;
+}) => {
+ const permissions = new Set(mapScopesToPermissions(scopes as Scope[]));
+
+ return (
+ new Set(
+ grantLegacyScopePermissions({ permissions: [...permissions], createdAt }),
+ ).size > permissions.size
+ );
+};
+
// Get SCOPES_BY_RESOURCE based on user role in a workspace
export const getScopesByResourceForRole = (role: WorkspaceRole) => {
const groupedByResource = {};
diff --git a/apps/web/tests/tokens/grant-legacy-scope-permissions.test.ts b/apps/web/tests/tokens/grant-legacy-scope-permissions.test.ts
index 977f8bac6c..e0f73dd4b1 100644
--- a/apps/web/tests/tokens/grant-legacy-scope-permissions.test.ts
+++ b/apps/web/tests/tokens/grant-legacy-scope-permissions.test.ts
@@ -1,5 +1,6 @@
import {
grantLegacyScopePermissions,
+ hasLegacyScopeGrant,
LEGACY_SCOPE_GRANTS,
PARTNER_SCOPES_ENFORCED_AT,
} from "@/lib/api/tokens/scopes";
@@ -152,3 +153,53 @@ describe("grantLegacyScopePermissions", () => {
});
});
});
+
+describe("hasLegacyScopeGrant", () => {
+ const before = new Date(ENFORCED_AT.getTime() - 1);
+
+ test("is true for restricted and read-only keys made before the cutoff", () => {
+ expect(
+ hasLegacyScopeGrant({ scopes: ["links.read"], createdAt: before }),
+ ).toBe(true);
+
+ expect(
+ hasLegacyScopeGrant({ scopes: ["apis.read"], createdAt: before }),
+ ).toBe(true);
+ });
+
+ test("is false for all access keys", () => {
+ expect(
+ hasLegacyScopeGrant({ scopes: ["apis.all"], createdAt: before }),
+ ).toBe(false);
+ });
+
+ test("is false when the scopes already cover every legacy permission", () => {
+ expect(
+ hasLegacyScopeGrant({
+ scopes: [
+ "partners.write",
+ "program_applications.write",
+ "customers.write",
+ "discount_codes.write",
+ "commissions.write",
+ "bounties.write",
+ "payouts.read",
+ ],
+ createdAt: before,
+ }),
+ ).toBe(false);
+ });
+
+ test("is false for keys made at or after the cutoff", () => {
+ expect(
+ hasLegacyScopeGrant({ scopes: ["links.read"], createdAt: ENFORCED_AT }),
+ ).toBe(false);
+
+ expect(
+ hasLegacyScopeGrant({
+ scopes: ["apis.read"],
+ createdAt: ENFORCED_AT.toISOString(),
+ }),
+ ).toBe(false);
+ });
+});