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: Update OAuth client scopes in route files

Kiran K31d0e1d42477
diff --git a/apps/web/app/api/oauth-clients/[clientId]/route.ts b/apps/web/app/api/oauth-clients/[clientId]/route.ts
index 0bf3ae442a..db48233695 100644
--- a/apps/web/app/api/oauth-clients/[clientId]/route.ts
+++ b/apps/web/app/api/oauth-clients/[clientId]/route.ts
@@ -28,7 +28,7 @@ export const GET = withWorkspace(
return NextResponse.json(oAuthClientSchema.parse(app));
},
{
- requiredScopes: ["oauth_apps.read"],
+ requiredScopes: ["oauth_clients.read"],
},
);
@@ -57,7 +57,7 @@ export const PATCH = withWorkspace(
return NextResponse.json(oAuthClientSchema.parse(app));
},
{
- requiredScopes: ["oauth_apps.write"],
+ requiredScopes: ["oauth_clients.write"],
},
);
@@ -87,6 +87,6 @@ export const DELETE = withWorkspace(
return NextResponse.json({ id: params.clientId });
},
{
- requiredScopes: ["oauth_apps.write"],
+ requiredScopes: ["oauth_clients.write"],
},
);
diff --git a/apps/web/app/api/oauth-clients/route.ts b/apps/web/app/api/oauth-clients/route.ts
index 7228e77b14..42e18d3d92 100644
--- a/apps/web/app/api/oauth-clients/route.ts
+++ b/apps/web/app/api/oauth-clients/route.ts
@@ -41,7 +41,7 @@ export const POST = withWorkspace(
);
},
{
- requiredScopes: ["oauth_apps.write"],
+ requiredScopes: ["oauth_clients.write"],
},
);
@@ -57,6 +57,6 @@ export const GET = withWorkspace(
return NextResponse.json(z.array(oAuthClientSchema).parse(clients));
},
{
- requiredScopes: ["oauth_apps.read"],
+ requiredScopes: ["oauth_clients.read"],
},
);
diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/Scopes.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/Scopes.tsx
deleted file mode 100644
index d150a647da..0000000000
--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/Scopes.tsx
+++ /dev/null
@@ -1,32 +0,0 @@
-"use client";
-
-import { Check } from "lucide-react";
-
-interface ScopesProps {
- scopes: string | null // space separated scopes
-}
-
-export const Scopes = (props: ScopesProps) => {
- const { scopes } = props;
- console.log(scopes);
-
- return (
- <>
- <span className="text-gray-600">Grant permissions:</span>
- <ul className="text-md space-y-2">
- <li className="flex items-center gap-2">
- <Check className="h-4 w-4 text-green-500" />
- <span className="font-bold">Read</span> access to workspace data
- </li>
- <li className="flex items-center gap-2">
- <Check className="h-4 w-4 text-green-500" />
- <span className="font-bold">Read</span> access to workspace data
- </li>
- <li className="flex items-center gap-2">
- <Check className="h-4 w-4 text-green-500" />
- <span className="font-bold">Read</span> access to workspace data
- </li>
- </ul>
- </>
- );
-};
diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/AuthorizeForm.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx
similarity index 81%
rename from apps/web/app/app.dub.co/(auth)/oauth/authorize/AuthorizeForm.tsx
rename to apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx
index 23a0b59dd5..d9c5491547 100644
--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/AuthorizeForm.tsx
+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx
@@ -7,8 +7,7 @@ import { authorizeSchema } from "@/lib/zod/schemas/oauth";
import { Button, InputSelect, InputSelectItemProps } from "@dub/ui";
import { DICEBEAR_AVATAR_URL } from "@dub/utils";
import { OAuthClient } from "@prisma/client";
-import { useSession } from "next-auth/react";
-import { useEffect, useMemo, useState } from "react";
+import { useMemo, useState } from "react";
interface AuthorizeFormProps extends z.infer<typeof authorizeSchema> {
oAuthClient: Pick<OAuthClient, "name">;
@@ -18,7 +17,6 @@ export const AuthorizeForm = (props: AuthorizeFormProps) => {
const { oAuthClient, client_id, redirect_uri, response_type, state } = props;
const { workspaces } = useWorkspaces();
- const { data: session } = useSession();
const [selectedWorkspace, setSelectedWorkspace] =
useState<InputSelectItemProps | null>(null);
@@ -32,13 +30,15 @@ export const AuthorizeForm = (props: AuthorizeFormProps) => {
: [];
}, [workspaces]);
- useEffect(() => {
- setSelectedWorkspace(
- selectOptions.find(
- (option) => option.id === session?.user?.["defaultWorkspace"],
- ) || selectOptions[0],
- );
- }, [selectOptions, session]);
+ // Decline
+ const handleDecline = () => {
+ const searchParams = new URLSearchParams({
+ error: "access_denied",
+ ...(state && { state }),
+ });
+
+ window.location.href = `${redirect_uri}?${searchParams.toString()}`;
+ };
return (
<form action={handleAuthorize}>
@@ -62,13 +62,16 @@ export const AuthorizeForm = (props: AuthorizeFormProps) => {
<Button
text="Decline"
type="button"
- onClick={() => {}}
+ onClick={handleDecline}
variant="secondary"
/>
<Button
text={`Authorize ${oAuthClient.name}`}
type="submit"
- // loading={clickedGithub}
+ disabled={!selectedWorkspace}
+ disabledTooltip={
+ !selectedWorkspace ? "Please select a workspace to continue" : ""
+ }
/>
</div>
</form>
diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx
index a2c5a25061..eb16733951 100644
--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx
@@ -7,8 +7,8 @@ import { HOME_DOMAIN } from "@dub/utils";
import { ArrowLeftRight } from "lucide-react";
import { redirect } from "next/navigation";
import { Suspense } from "react";
-import { AuthorizeForm } from "./AuthorizeForm";
-import { Scopes } from "./Scopes";
+import { AuthorizeForm } from "./authorize-form";
+import { ScopesRequested } from "./scopes-requested";
export const runtime = "nodejs";
@@ -34,23 +34,30 @@ export default async function Authorize({
return (
<div className="relative z-10 mt-[calc(30vh)] h-fit w-full max-w-md overflow-hidden border-y border-gray-200 sm:rounded-2xl sm:border sm:shadow-xl">
- <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 bg-white px-4 py-6 pt-8 text-center sm:px-16">
+ <div className="flex flex-col items-center justify-center space-y-6 border-b border-gray-200 bg-white px-4 py-6 pt-8 text-center sm:px-16">
<div className="flex items-center gap-3">
<a href={oAuthClient.website} target="_blank" rel="noreferrer">
- <Logo className="h-10 w-10" />
+ <Logo className="h-14 w-14" />
</a>
<ArrowLeftRight className="h-6 w-6 text-gray-500" />
<a href={HOME_DOMAIN} target="_blank" rel="noreferrer">
- <Logo className="h-10 w-10" />
+ <Logo className="h-14 w-14" />
</a>
</div>
- <p className="text-md text-gray-500">
- <span className="font-bold">{oAuthClient.name}</span> is requesting
- API access to a workspace.
+ <p className="text-md">
+ <a
+ className="font-bold text-blue-500"
+ href={oAuthClient.website}
+ target="_blank"
+ rel="noreferrer"
+ >
+ {oAuthClient.name}
+ </a>{" "}
+ is requesting API access to a workspace.
</p>
</div>
<div className="flex flex-col space-y-3 bg-white px-2 py-6 sm:px-10">
- <Scopes scopes={oAuthClient.scopes} />
+ <ScopesRequested scopes={oAuthClient.scopes} />
</div>
<div className="flex flex-col space-y-2 border-t border-gray-200 bg-white px-2 py-6 sm:px-10">
<Suspense fallback={<></>}>
diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/scopes-requested.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/scopes-requested.tsx
new file mode 100644
index 0000000000..3ac4345aa3
--- /dev/null
+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/scopes-requested.tsx
@@ -0,0 +1,36 @@
+"use client";
+
+import { permissions } from "@/lib/api/tokens/scopes";
+import { Check } from "lucide-react";
+
+interface ScopesProps {
+ scopes: string | null; // space separated scopes
+}
+
+export const ScopesRequested = (props: ScopesProps) => {
+ const { scopes } = props;
+ const scopesArray = scopes?.split(" ") ?? [];
+
+ const requestedScopes = permissions.filter((p) =>
+ scopesArray.includes(p.scope),
+ );
+
+ return (
+ <>
+ <span className="text-gray-600">Grant permissions:</span>
+ <ul className="text-md space-y-1">
+ {requestedScopes.map((requestedScope) => {
+ return (
+ <li
+ className="flex items-center gap-2 text-gray-600"
+ key={requestedScope.scope}
+ >
+ <Check className="h-4 w-4 text-green-500" />
+ {requestedScope.description}
+ </li>
+ );
+ })}
+ </ul>
+ </>
+ );
+};
diff --git a/apps/web/lib/api/tokens/scopes.ts b/apps/web/lib/api/tokens/scopes.ts
index 68fe0a1f1b..b3eba9d61f 100644
--- a/apps/web/lib/api/tokens/scopes.ts
+++ b/apps/web/lib/api/tokens/scopes.ts
@@ -13,14 +13,32 @@ export const availableScopes = [
"tokens.read",
"tokens.write",
"conversions.write",
- "oauth_apps.write",
- "oauth_apps.read",
+ "oauth_clients.write",
+ "oauth_clients.read",
"apis.all", // All API scopes
"apis.read", // All read scopes
] as const;
export type Scope = (typeof availableScopes)[number];
+type Resource =
+ | "links"
+ | "analytics"
+ | "workspaces"
+ | "domains"
+ | "tags"
+ | "tokens"
+ | "conversions"
+ | "oauth_clients";
+
+type Permission = {
+ permission: string;
+ scope: Scope;
+ description: string;
+ roles: Role[];
+ resource: Resource;
+};
+
export const resourcePermissions = [
{
name: "Links",
@@ -152,19 +170,19 @@ export const resourcePermissions = [
},
{
name: "OAuth Apps",
- key: "oauth_apps",
+ key: "oauth_clients",
description: "OAuth apps (create, read, update, delete)",
betaFeature: true,
permissions: [
{
permission: "Read",
- scope: "oauth_apps.read",
+ scope: "oauth_clients.read",
description: "Read OAuth apps",
roles: ["owner", "member"],
},
{
permission: "Write",
- scope: "oauth_apps.write",
+ scope: "oauth_clients.write",
description: "Create, update, and delete OAuth apps",
roles: ["owner"],
},
@@ -242,3 +260,122 @@ export const scopesToName = (scopes: string[]) => {
description: "restricted access to some resources",
};
};
+
+export const permissions: Permission[] = [
+ {
+ permission: "Read",
+ scope: "links.read",
+ description: "Read access to links",
+ roles: ["owner", "member"],
+ resource: "links",
+ },
+ {
+ permission: "Write",
+ scope: "links.write",
+ description: "Read and Write access to links",
+ roles: ["owner", "member"],
+ resource: "links",
+ },
+ {
+ permission: "Read",
+ scope: "analytics.read",
+ description: "Read access to analytics and events",
+ roles: ["owner", "member"],
+ resource: "analytics",
+ },
+ {
+ permission: "Read",
+ scope: "workspaces.read",
+ description: "Read access to workspace",
+ roles: ["owner", "member"],
+ resource: "workspaces",
+ },
+ {
+ permission: "Write",
+ scope: "workspaces.write",
+ description: "Read and Write access to workspace",
+ roles: ["owner"],
+ resource: "workspaces",
+ },
+ {
+ permission: "Read",
+ scope: "domains.read",
+ description: "Read access to domains",
+ roles: ["owner", "member"],
+ resource: "domains",
+ },
+ {
+ permission: "Write",
+ scope: "domains.write",
+ description: "Read and Write access to domains",
+ roles: ["owner"],
+ resource: "domains",
+ },
+ {
+ permission: "Read",
+ scope: "tags.read",
+ description: "Read access to tags",
+ roles: ["owner", "member"],
+ resource: "tags",
+ },
+ {
+ permission: "Write",
+ scope: "tags.write",
+ description: "Read and Write access to tags",
+ roles: ["owner", "member"],
+ resource: "tags",
+ },
+ {
+ permission: "Read",
+ scope: "tokens.read",
+ description: "Read access to Workspace API keys",
+ roles: ["owner", "member"],
+ resource: "tokens",
+ },
+ {
+ permission: "Write",
+ scope: "tokens.write",
+ description: "Read and Write access to Workspace API keys",
+ roles: ["owner", "member"],
+ resource: "tokens",
+ },
+ {
+ permission: "Write",
+ scope: "conversions.write",
+ description:
+ "Read and Write access to conversions. Able to track customer, lead, and sales events",
+ roles: ["owner"],
+ resource: "conversions",
+ },
+ {
+ permission: "Read",
+ scope: "oauth_clients.read",
+ description: "Read access to OAuth clients",
+ roles: ["owner", "member"],
+ resource: "oauth_clients",
+ },
+ {
+ permission: "Write",
+ scope: "oauth_clients.write",
+ description: "Read and Write access to OAuth clients",
+ roles: ["owner"],
+ resource: "oauth_clients",
+ },
+];
+
+export const scopesByResource = permissions.reduce<
+ Record<Resource, Permission[]>
+>(
+ (acc, permission) => {
+ const { resource } = permission;
+
+ if (!acc[resource]) {
+ acc[resource] = [];
+ }
+
+ acc[resource].push(permission);
+
+ return acc;
+ },
+ {} as Record<Resource, Permission[]>,
+);