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.

chore: Add OAuth app API routes and schemas

Kiran K7d89696580c6
diff --git a/apps/web/app/api/oauth-apps/[appId]/route.ts b/apps/web/app/api/oauth-apps/[appId]/route.ts
new file mode 100644
index 0000000000..c1b0ed268e
--- /dev/null
+++ b/apps/web/app/api/oauth-apps/[appId]/route.ts
@@ -0,0 +1,75 @@
+import { DubApiError } from "@/lib/api/errors";
+import { parseRequestBody } from "@/lib/api/utils";
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import {
+ oAuthAppSchema,
+ updateOAuthAppSchema,
+} from "@/lib/zod/schemas/oauth-app";
+import { NextResponse } from "next/server";
+
+// GET /api/oauth-apps/[appId] – get an OAuth app
+export const GET = withWorkspace(async ({ params, workspace }) => {
+ const app = await prisma.oAuthClient.findUnique({
+ where: {
+ id: params.appId,
+ projectId: workspace.id,
+ },
+ });
+
+ if (!app) {
+ throw new DubApiError({
+ code: "not_found",
+ message: `OAuth app with id ${params.appId} not found.`,
+ });
+ }
+
+ return NextResponse.json(app);
+});
+
+// PATCH /api/oauth-apps/[appId] – update an OAuth app
+export const PATCH = withWorkspace(async ({ req, params, workspace }) => {
+ const { name, description, website, redirectUri, scopes } =
+ updateOAuthAppSchema.parse(await parseRequestBody(req));
+
+ const app = await prisma.oAuthClient.update({
+ where: {
+ id: params.appId,
+ projectId: workspace.id,
+ },
+ data: {
+ name,
+ description,
+ website,
+ redirectUri,
+ scopes: scopes ? scopes.join(",") : null,
+ },
+ });
+
+ return NextResponse.json(oAuthAppSchema.parse(app));
+});
+
+// DELETE /api/oauth-apps/[appId] - delete an OAuth app
+export const DELETE = withWorkspace(async ({ req, params, workspace }) => {
+ const app = await prisma.oAuthClient.findUnique({
+ where: {
+ id: params.appId,
+ projectId: workspace.id,
+ },
+ });
+
+ if (!app) {
+ throw new DubApiError({
+ code: "not_found",
+ message: `OAuth app with id ${params.appId} not found.`,
+ });
+ }
+
+ await prisma.oAuthClient.delete({
+ where: {
+ id: params.appId,
+ },
+ });
+
+ return NextResponse.json({ id: params.appId });
+});
diff --git a/apps/web/app/api/oauth-apps/route.ts b/apps/web/app/api/oauth-apps/route.ts
new file mode 100644
index 0000000000..7697a7c99f
--- /dev/null
+++ b/apps/web/app/api/oauth-apps/route.ts
@@ -0,0 +1,50 @@
+import { parseRequestBody } from "@/lib/api/utils";
+import { hashToken, withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import {
+ createOAuthAppSchema,
+ oAuthAppSchema,
+} from "@/lib/zod/schemas/oauth-app";
+import { nanoid } from "@dub/utils";
+import { NextResponse } from "next/server";
+import { z } from "zod";
+
+// POST /api/oauth-apps - create a new oauth app
+export const POST = withWorkspace(async ({ req, workspace }) => {
+ const { name, description, website, redirectUri, scopes } =
+ createOAuthAppSchema.parse(await parseRequestBody(req));
+
+ const clientSecret = `dub_${nanoid(32)}`;
+
+ const app = await prisma.oAuthClient.create({
+ data: {
+ projectId: workspace.id,
+ name,
+ description,
+ website,
+ redirectUri,
+ scopes: scopes ? scopes.join(",") : null,
+ clientId: nanoid(32),
+ clientSecretHashed: await hashToken(clientSecret),
+ },
+ });
+
+ return NextResponse.json(
+ {
+ ...oAuthAppSchema.parse(app),
+ clientSecret,
+ },
+ { status: 201 },
+ );
+});
+
+// GET /api/oauth-apps - get all oauth apps for a specific workspace
+export const GET = withWorkspace(async ({ req, workspace }) => {
+ const apps = await prisma.oAuthClient.findMany({
+ where: {
+ projectId: workspace.id,
+ },
+ });
+
+ return NextResponse.json(z.array(oAuthAppSchema).parse(apps));
+});
diff --git a/apps/web/lib/zod/schemas/oauth-app.ts b/apps/web/lib/zod/schemas/oauth-app.ts
new file mode 100644
index 0000000000..5a8c7fc9f6
--- /dev/null
+++ b/apps/web/lib/zod/schemas/oauth-app.ts
@@ -0,0 +1,21 @@
+import { z } from "zod";
+
+export const oAuthAppSchema = z.object({
+ id: z.string(),
+ name: z.string(),
+ description: z.string().nullable(),
+ website: z.string(),
+ redirectUri: z.string(),
+ scopes: z.array(z.string()).nullable(),
+ clientId: z.string(),
+});
+
+export const createOAuthAppSchema = z.object({
+ name: z.string().min(1).max(255),
+ description: z.string().max(255).nullable(),
+ website: z.string().url().max(255),
+ redirectUri: z.string().url().max(255),
+ scopes: z.array(z.string()).nullable(), // TODO: validate scopes and should be array
+});
+
+export const updateOAuthAppSchema = createOAuthAppSchema.partial();
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index d14b9821f4..3f465950e8 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -370,11 +370,11 @@ model OAuthClient {
projectId String
name String
website String
+ description String?
clientId String @unique
clientSecretHashed String
- clientSecretAlias String
- redirectUris Json
- scopes String
+ redirectUri String
+ scopes String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt