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.

move to OAUTH_CONFIG

Kiran K1e66c897bdde
diff --git a/apps/web/app/api/oauth-apps/route.ts b/apps/web/app/api/oauth-apps/route.ts
index 1a404a4ddb..4f05ab749d 100644
--- a/apps/web/app/api/oauth-apps/route.ts
+++ b/apps/web/app/api/oauth-apps/route.ts
@@ -1,13 +1,9 @@
-import {
- OAUTH_CLIENT_ID_LENGTH,
- OAUTH_CLIENT_SECRET_LENGTH,
- OAUTH_CLIENT_SECRET_PREFIX,
-} from "@/lib/api/oauth/constants";
+import { OAUTH_CONFIG } from "@/lib/api/oauth/constants";
+import { createToken } from "@/lib/api/oauth/utils";
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";
-import { nanoid } from "@dub/utils";
import { NextResponse } from "next/server";
import { z } from "zod";
@@ -17,7 +13,14 @@ export const POST = withWorkspace(
const { name, developer, website, redirectUri, logo, pkce } =
createOAuthAppSchema.parse(await parseRequestBody(req));
- const clientSecret = `${OAUTH_CLIENT_SECRET_PREFIX}${nanoid(OAUTH_CLIENT_SECRET_LENGTH)}`;
+ const clientSecret = createToken({
+ length: OAUTH_CONFIG.CLIENT_SECRET_LENGTH,
+ prefix: OAUTH_CONFIG.CLIENT_SECRET_PREFIX,
+ });
+
+ const clientId = createToken({
+ length: OAUTH_CONFIG.CLIENT_ID_LENGTH,
+ });
const client = await prisma.oAuthApp.create({
data: {
@@ -27,7 +30,7 @@ export const POST = withWorkspace(
website,
redirectUri,
logo,
- clientId: nanoid(OAUTH_CLIENT_ID_LENGTH),
+ clientId,
clientSecretHashed: await hashToken(clientSecret),
createdBy: session.user.id,
pkce,
diff --git a/apps/web/app/api/oauth/authorize/route.ts b/apps/web/app/api/oauth/authorize/route.ts
index c91c2880e4..c29644ced2 100644
--- a/apps/web/app/api/oauth/authorize/route.ts
+++ b/apps/web/app/api/oauth/authorize/route.ts
@@ -1,13 +1,10 @@
import { DubApiError } from "@/lib/api/errors";
-import {
- OAUTH_CODE_LENGTH,
- OAUTH_CODE_LIFETIME,
-} from "@/lib/api/oauth/constants";
+import { OAUTH_CONFIG } from "@/lib/api/oauth/constants";
+import { createToken } from "@/lib/api/oauth/utils";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { authorizeRequestSchema } from "@/lib/zod/schemas/oauth";
-import { nanoid } from "@dub/utils";
import { NextResponse } from "next/server";
// POST /api/oauth/authorize - approve OAuth authorization request
@@ -53,8 +50,8 @@ export const POST = withWorkspace(async ({ session, req, workspace }) => {
projectId: workspace.id,
userId: session.user.id,
scopes: scope.join(" "),
- code: nanoid(OAUTH_CODE_LENGTH),
- expiresAt: new Date(Date.now() + OAUTH_CODE_LIFETIME * 1000),
+ code: createToken({ length: OAUTH_CONFIG.CODE_LENGTH }),
+ expiresAt: new Date(Date.now() + OAUTH_CONFIG.CODE_LIFETIME * 1000),
...(app.pkce && { codeChallenge, codeChallengeMethod }),
},
});
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 f55b3c401f..9b9759f2a8 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,18 +1,11 @@
import { DubApiError } from "@/lib/api/errors";
-import {
- OAUTH_ACCESS_TOKEN_LENGTH,
- OAUTH_ACCESS_TOKEN_LIFETIME,
- OAUTH_ACCESS_TOKEN_PREFIX,
- OAUTH_REFRESH_TOKEN_LENGTH,
- OAUTH_REFRESH_TOKEN_LIFETIME,
- OAUTH_REFRESH_TOKEN_PREFIX,
-} from "@/lib/api/oauth/constants";
-import { generateCodeChallengeHash } from "@/lib/api/oauth/utils";
+import { OAUTH_CONFIG } from "@/lib/api/oauth/constants";
+import { createToken, generateCodeChallengeHash } from "@/lib/api/oauth/utils";
import { hashToken } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import z from "@/lib/zod";
import { authCodeExchangeSchema } from "@/lib/zod/schemas/oauth";
-import { getCurrentPlan, nanoid } from "@dub/utils";
+import { getCurrentPlan } from "@dub/utils";
import { NextRequest } from "next/server";
// Exchange authorization code with access token
@@ -166,10 +159,16 @@ export const exchangeAuthCodeForToken = async (
const { userId, projectId, scopes } = accessCode;
- const accessToken = `${OAUTH_ACCESS_TOKEN_PREFIX}${nanoid(OAUTH_ACCESS_TOKEN_LENGTH)}`;
- const refreshToken = `${OAUTH_REFRESH_TOKEN_PREFIX}${nanoid(OAUTH_REFRESH_TOKEN_LENGTH)}`;
+ const accessToken = createToken({
+ length: OAUTH_CONFIG.ACCESS_TOKEN_LENGTH,
+ prefix: OAUTH_CONFIG.ACCESS_TOKEN_PREFIX,
+ });
+ const refreshToken = createToken({
+ length: OAUTH_CONFIG.REFRESH_TOKEN_LENGTH,
+ prefix: OAUTH_CONFIG.REFRESH_TOKEN_PREFIX,
+ });
const accessTokenExpires = new Date(
- Date.now() + OAUTH_ACCESS_TOKEN_LIFETIME * 1000,
+ Date.now() + OAUTH_CONFIG.ACCESS_TOKEN_LIFETIME * 1000,
);
// Delete the existing token issued to the client for the user for the selected workspace before creating a new one
@@ -210,7 +209,7 @@ export const exchangeAuthCodeForToken = async (
clientId,
refreshTokenHashed: await hashToken(refreshToken),
expiresAt: new Date(
- Date.now() + OAUTH_REFRESH_TOKEN_LIFETIME * 1000,
+ Date.now() + OAUTH_CONFIG.REFRESH_TOKEN_LIFETIME * 1000,
),
},
},
@@ -239,7 +238,7 @@ export const exchangeAuthCodeForToken = async (
access_token: accessToken,
refresh_token: refreshToken,
token_type: "Bearer",
- expires_in: OAUTH_ACCESS_TOKEN_LIFETIME,
+ expires_in: OAUTH_CONFIG.ACCESS_TOKEN_LIFETIME,
scope: scopes,
};
};
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 166820cac6..c978027a24 100644
--- a/apps/web/app/api/oauth/token/refresh-access-token.ts
+++ b/apps/web/app/api/oauth/token/refresh-access-token.ts
@@ -1,17 +1,11 @@
import { DubApiError } from "@/lib/api/errors";
-import {
- OAUTH_ACCESS_TOKEN_LENGTH,
- OAUTH_ACCESS_TOKEN_LIFETIME,
- OAUTH_ACCESS_TOKEN_PREFIX,
- OAUTH_REFRESH_TOKEN_LENGTH,
- OAUTH_REFRESH_TOKEN_LIFETIME,
- OAUTH_REFRESH_TOKEN_PREFIX,
-} from "@/lib/api/oauth/constants";
+import { OAUTH_CONFIG } from "@/lib/api/oauth/constants";
+import { createToken } from "@/lib/api/oauth/utils";
import { hashToken } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import z from "@/lib/zod";
import { refreshTokenSchema } from "@/lib/zod/schemas/oauth";
-import { getCurrentPlan, nanoid } from "@dub/utils";
+import { getCurrentPlan } from "@dub/utils";
import { NextRequest } from "next/server";
// Get new access token using refresh token
@@ -130,10 +124,16 @@ export const refreshAccessToken = async (
id: accessTokenId,
} = refreshTokenRecord.accessToken;
- const newAccessToken = `${OAUTH_ACCESS_TOKEN_PREFIX}${nanoid(OAUTH_ACCESS_TOKEN_LENGTH)}`;
- const newRefreshToken = `${OAUTH_REFRESH_TOKEN_PREFIX}${nanoid(OAUTH_REFRESH_TOKEN_LENGTH)}`;
+ const newAccessToken = createToken({
+ length: OAUTH_CONFIG.ACCESS_TOKEN_LENGTH,
+ prefix: OAUTH_CONFIG.ACCESS_TOKEN_PREFIX,
+ });
+ const newRefreshToken = createToken({
+ length: OAUTH_CONFIG.REFRESH_TOKEN_LENGTH,
+ prefix: OAUTH_CONFIG.REFRESH_TOKEN_PREFIX,
+ });
const accessTokenExpires = new Date(
- Date.now() + OAUTH_ACCESS_TOKEN_LIFETIME * 1000,
+ Date.now() + OAUTH_CONFIG.ACCESS_TOKEN_LIFETIME * 1000,
);
await prisma.$transaction([
@@ -161,7 +161,7 @@ export const refreshAccessToken = async (
clientId,
refreshTokenHashed: await hashToken(newRefreshToken),
expiresAt: new Date(
- Date.now() + OAUTH_REFRESH_TOKEN_LIFETIME * 1000,
+ Date.now() + OAUTH_CONFIG.REFRESH_TOKEN_LIFETIME * 1000,
),
},
},
@@ -174,6 +174,6 @@ export const refreshAccessToken = async (
access_token: newAccessToken,
refresh_token: newRefreshToken,
token_type: "Bearer",
- expires_in: OAUTH_ACCESS_TOKEN_LIFETIME,
+ expires_in: OAUTH_CONFIG.ACCESS_TOKEN_LIFETIME,
};
};
diff --git a/apps/web/lib/api/oauth/constants.ts b/apps/web/lib/api/oauth/constants.ts
index 26a7922c68..fd2412ae8a 100644
--- a/apps/web/lib/api/oauth/constants.ts
+++ b/apps/web/lib/api/oauth/constants.ts
@@ -1,19 +1,21 @@
-// Lifetime in seconds
-export const OAUTH_ACCESS_TOKEN_LIFETIME = 2 * 60 * 60; // 2 hours
-export const OAUTH_REFRESH_TOKEN_LIFETIME = 60 * 24 * 60 * 60; // 60 days
-export const OAUTH_CODE_LIFETIME = 2 * 60; // 2 minutes
+// OAuth configuration
+export const OAUTH_CONFIG = {
+ ACCESS_TOKEN_LIFETIME: 2 * 60 * 60, // 2 hours
+ REFRESH_TOKEN_LIFETIME: 60 * 24 * 60 * 60, // 60 days
+ CODE_LIFETIME: 2 * 60, // 2 minutes
-export const OAUTH_CLIENT_ID_LENGTH = 32;
-export const OAUTH_CLIENT_SECRET_LENGTH = 32;
-export const OAUTH_ACCESS_TOKEN_LENGTH = 40;
-export const OAUTH_REFRESH_TOKEN_LENGTH = 40;
-export const OAUTH_CODE_LENGTH = 40;
+ CLIENT_ID_LENGTH: 32,
+ CLIENT_SECRET_LENGTH: 32,
+ ACCESS_TOKEN_LENGTH: 40,
+ REFRESH_TOKEN_LENGTH: 40,
+ CODE_LENGTH: 40,
-// TODO:
-// Keep it short and simple (Discuss with Steven)
-export const OAUTH_CLIENT_SECRET_PREFIX = "dub_app_secret_";
-export const OAUTH_ACCESS_TOKEN_PREFIX = "dub_access_token_";
-export const OAUTH_REFRESH_TOKEN_PREFIX = "dub_refresh_token_";
+ // TODO:
+ // Keep it short and simple (Discuss with Steven)
+ CLIENT_SECRET_PREFIX: "dub_app_secret_",
+ ACCESS_TOKEN_PREFIX: "dub_access_token_",
+ REFRESH_TOKEN_PREFIX: "dub_refresh_token_",
+};
// These are the scopes an OAuth app can request on behalf of an user
// Keep it separate from the actual scopes to avoid confusion
diff --git a/apps/web/lib/api/oauth/utils.ts b/apps/web/lib/api/oauth/utils.ts
index 13c1426c6e..89f2fc1b69 100644
--- a/apps/web/lib/api/oauth/utils.ts
+++ b/apps/web/lib/api/oauth/utils.ts
@@ -1,3 +1,5 @@
+import { nanoid } from "ai";
+
export const generateCodeChallengeHash = async (codeVerifier: string) => {
const encoder = new TextEncoder();
const data = encoder.encode(codeVerifier);
@@ -12,3 +14,13 @@ export const generateCodeChallengeHash = async (codeVerifier: string) => {
return hashBase64;
};
+
+export const createToken = ({
+ prefix,
+ length,
+}: {
+ prefix?: string;
+ length: number;
+}) => {
+ return `${prefix || ""}${nanoid(length)}`;
+};