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: Refactor OAuth token creation and deletion

Kiran K4a72ab48fe06
diff --git a/apps/web/app/api/oauth-apps/[clientId]/disconnect/route.ts b/apps/web/app/api/oauth-apps/[clientId]/disconnect/route.ts
new file mode 100644
index 0000000000..58b753a7c5
--- /dev/null
+++ b/apps/web/app/api/oauth-apps/[clientId]/disconnect/route.ts
@@ -0,0 +1,39 @@
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import { NextResponse } from "next/server";
+
+// DELETE /api/oauth-clients/[clientId]/disconnect - Disconnect an authorized OAuth app from the workspace
+export const DELETE = withWorkspace(
+ async ({ params, workspace, session }) => {
+ const { clientId } = params;
+ const { id: userId } = session.user;
+ const { id: projectId } = workspace;
+
+ await prisma.$transaction([
+ prisma.oAuthAuthorizedApp.delete({
+ where: {
+ userId_projectId_clientId: {
+ userId,
+ projectId,
+ clientId,
+ },
+ },
+ }),
+
+ prisma.restrictedToken.delete({
+ where: {
+ userId_projectId_clientId: {
+ userId,
+ projectId,
+ clientId,
+ },
+ },
+ }),
+ ]);
+
+ return NextResponse.json({ id: params.clientId });
+ },
+ {
+ requiredScopes: ["oauth_apps.write"],
+ },
+);
diff --git a/apps/web/app/api/oauth-apps/route.ts b/apps/web/app/api/oauth-apps/route.ts
index 3e4dc73045..d943b46cbb 100644
--- a/apps/web/app/api/oauth-apps/route.ts
+++ b/apps/web/app/api/oauth-apps/route.ts
@@ -1,4 +1,4 @@
-import { TOKEN_LENGTH } from "@/lib/api/oauth";
+import { TOKEN_LENGTH, TOKEN_PREFIX } from "@/lib/api/oauth";
import { parseRequestBody } from "@/lib/api/utils";
import { hashToken, withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
@@ -16,7 +16,7 @@ export const POST = withWorkspace(
const { name, developer, website, redirectUri, scopes } =
createOAuthClientSchema.parse(await parseRequestBody(req));
- const clientSecret = `dub_secret_${nanoid(TOKEN_LENGTH.clientSecret)}`;
+ const clientSecret = `${TOKEN_PREFIX.clientSecret}${nanoid(TOKEN_LENGTH.clientSecret)}`;
const client = await prisma.oAuthClient.create({
data: {
diff --git a/apps/web/app/api/oauth/token/exchange-code-for-token.ts b/apps/web/app/api/oauth/token/exchange-code-for-token.ts
index 82958aff64..bcc55707fb 100644
--- a/apps/web/app/api/oauth/token/exchange-code-for-token.ts
+++ b/apps/web/app/api/oauth/token/exchange-code-for-token.ts
@@ -1,5 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
-import { TOKEN_EXPIRY, TOKEN_LENGTH } from "@/lib/api/oauth";
+import { TOKEN_EXPIRY, TOKEN_LENGTH, TOKEN_PREFIX } from "@/lib/api/oauth";
import { getAuthTokenOrThrow, hashToken } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import z from "@/lib/zod";
@@ -105,35 +105,31 @@ export const exchangeAuthCodeForToken = async (
const { userId, projectId, scopes } = accessCode;
- const accessToken = `dub_${nanoid(TOKEN_LENGTH.accessToken)}`;
- const refreshToken = `dub_${nanoid(TOKEN_LENGTH.refreshToken)}`;
+ const accessToken = `${TOKEN_PREFIX.accessToken}${nanoid(TOKEN_LENGTH.accessToken)}`;
+ const refreshToken = `${TOKEN_PREFIX.refreshToken}${nanoid(TOKEN_LENGTH.refreshToken)}`;
const accessTokenExpires = new Date(Date.now() + TOKEN_EXPIRY.accessToken);
// Delete the existing token issued to the client for the user for the selected workspace before creating a new one
// We only support one token per client per user per workspace at a time
- await Promise.all([
- prisma.restrictedToken.delete({
+ await prisma.$transaction([
+ prisma.restrictedToken.deleteMany({
where: {
- userId_projectId_clientId: {
- userId,
- projectId,
- clientId,
- },
+ userId,
+ projectId,
+ clientId,
},
}),
- prisma.oAuthAuthorizedApp.delete({
+ prisma.oAuthAuthorizedApp.deleteMany({
where: {
- userId_projectId_clientId: {
- userId,
- projectId,
- clientId,
- },
+ userId,
+ projectId,
+ clientId,
},
}),
]);
- await Promise.all([
+ await prisma.$transaction([
// Create the access token and refresh token
prisma.restrictedToken.create({
data: {
@@ -177,7 +173,7 @@ export const exchangeAuthCodeForToken = async (
access_token: accessToken,
refresh_token: refreshToken,
token_type: "Bearer",
- expires_in: Math.floor(accessTokenExpires.getTime() / 1000),
+ expires_in: Math.floor(accessTokenExpires.getTime() / 1000), // TODO: Fix this
};
return response;
diff --git a/apps/web/app/api/oauth/token/refresh-access-token.ts b/apps/web/app/api/oauth/token/refresh-access-token.ts
index 70be22e78a..e1b67ee4db 100644
--- a/apps/web/app/api/oauth/token/refresh-access-token.ts
+++ b/apps/web/app/api/oauth/token/refresh-access-token.ts
@@ -1,5 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
-import { TOKEN_EXPIRY, TOKEN_LENGTH } from "@/lib/api/oauth";
+import { TOKEN_EXPIRY, TOKEN_LENGTH, TOKEN_PREFIX } from "@/lib/api/oauth";
import { getAuthTokenOrThrow, hashToken } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import z from "@/lib/zod";
@@ -94,8 +94,8 @@ export const refreshAccessToken = async (
id: accessTokenId,
} = accessToken;
- const newAccessToken = `dub_${nanoid(TOKEN_LENGTH.accessToken)}`;
- const newRefreshToken = `dub_${nanoid(TOKEN_LENGTH.refreshToken)}`;
+ const newAccessToken = `${TOKEN_PREFIX.accessToken}${nanoid(TOKEN_LENGTH.accessToken)}`;
+ const newRefreshToken = `${TOKEN_PREFIX.refreshToken}${nanoid(TOKEN_LENGTH.refreshToken)}`;
const accessTokenExpires = new Date(Date.now() + TOKEN_EXPIRY.accessToken);
await Promise.all([
diff --git a/apps/web/lib/api/oauth.ts b/apps/web/lib/api/oauth.ts
index 2813e5a02f..1bb8e1e75a 100644
--- a/apps/web/lib/api/oauth.ts
+++ b/apps/web/lib/api/oauth.ts
@@ -11,3 +11,11 @@ export const TOKEN_EXPIRY = {
accessToken: 2 * 60 * 60 * 1000, // 2 hours
refreshToken: 60 * 24 * 60 * 60 * 1000, // 60 days
};
+
+// TODO:
+// Keep it short and simple (Discuss with Steven)
+export const TOKEN_PREFIX = {
+ clientSecret: "dub_app_secret_",
+ accessToken: "dub_access_token_",
+ refreshToken: "dub_refresh_token_",
+};