The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
move to constants
Kiran Kb389b6aae272
diff --git a/apps/web/app/api/oauth-apps/route.ts b/apps/web/app/api/oauth-apps/route.tsindex d943b46cbb..1d401e5f9e 100644--- a/apps/web/app/api/oauth-apps/route.ts+++ b/apps/web/app/api/oauth-apps/route.ts@@ -1,4 +1,8 @@-import { TOKEN_LENGTH, TOKEN_PREFIX } from "@/lib/api/oauth";+import {+ OAUTH_CLIENT_ID_LENGTH,+ OAUTH_CLIENT_SECRET_LENGTH,+ OAUTH_CLIENT_SECRET_PREFIX,+} from "@/lib/api/oauth";import { parseRequestBody } from "@/lib/api/utils";import { hashToken, withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";@@ -16,7 +20,7 @@ export const POST = withWorkspace(const { name, developer, website, redirectUri, scopes } =createOAuthClientSchema.parse(await parseRequestBody(req));- const clientSecret = `${TOKEN_PREFIX.clientSecret}${nanoid(TOKEN_LENGTH.clientSecret)}`;+ const clientSecret = `${OAUTH_CLIENT_SECRET_PREFIX}${nanoid(OAUTH_CLIENT_SECRET_LENGTH)}`;const client = await prisma.oAuthClient.create({data: {@@ -26,7 +30,7 @@ export const POST = withWorkspace(website,redirectUri,scopes: [...new Set(scopes)].join(" "),- clientId: nanoid(TOKEN_LENGTH.clientId),+ clientId: nanoid(OAUTH_CLIENT_ID_LENGTH),clientSecretHashed: await hashToken(clientSecret),},});diff --git a/apps/web/app/api/oauth/authorize/route.ts b/apps/web/app/api/oauth/authorize/route.tsindex 96b8b142f1..9ce34fc066 100644--- a/apps/web/app/api/oauth/authorize/route.ts+++ b/apps/web/app/api/oauth/authorize/route.ts@@ -1,4 +1,4 @@-import { TOKEN_EXPIRY, TOKEN_LENGTH } from "@/lib/api/oauth";+import { OAUTH_CODE_LENGTH, OAUTH_CODE_LIFETIME } from "@/lib/api/oauth";import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";@@ -24,8 +24,8 @@ export const POST = withWorkspace(async ({ session, req, workspace }) => {projectId: workspace.id,userId: session.user.id,scopes: oAuthClient.scopes,- code: nanoid(TOKEN_LENGTH.code),- expiresAt: new Date(Date.now() + TOKEN_EXPIRY.code),+ code: nanoid(OAUTH_CODE_LENGTH),+ expiresAt: new Date(Date.now() + OAUTH_CODE_LIFETIME * 1000),},});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.tsindex bcc55707fb..ed81a81716 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,12 @@import { DubApiError } from "@/lib/api/errors";-import { TOKEN_EXPIRY, TOKEN_LENGTH, TOKEN_PREFIX } from "@/lib/api/oauth";+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";import { getAuthTokenOrThrow, hashToken } from "@/lib/auth";import { prisma } from "@/lib/prisma";import z from "@/lib/zod";@@ -105,9 +112,11 @@ export const exchangeAuthCodeForToken = async (const { userId, projectId, scopes } = accessCode;- 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);+ const accessToken = `${OAUTH_ACCESS_TOKEN_PREFIX}${nanoid(OAUTH_ACCESS_TOKEN_LENGTH)}`;+ const refreshToken = `${OAUTH_REFRESH_TOKEN_PREFIX}${nanoid(OAUTH_REFRESH_TOKEN_LENGTH)}`;+ const accessTokenExpires = new Date(+ Date.now() + OAUTH_ACCESS_TOKEN_LIFETIME * 1000,+ );// 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@@ -146,7 +155,9 @@ export const exchangeAuthCodeForToken = async (create: {clientId,refreshTokenHashed: await hashToken(refreshToken),- expiresAt: new Date(Date.now() + TOKEN_EXPIRY.refreshToken),+ expiresAt: new Date(+ Date.now() + OAUTH_REFRESH_TOKEN_LIFETIME * 1000,+ ),},},},@@ -173,7 +184,7 @@ export const exchangeAuthCodeForToken = async (access_token: accessToken,refresh_token: refreshToken,token_type: "Bearer",- expires_in: Math.floor(accessTokenExpires.getTime() / 1000), // TODO: Fix this+ expires_in: OAUTH_ACCESS_TOKEN_LIFETIME,};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.tsindex e1b67ee4db..853ff02aca 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,12 @@import { DubApiError } from "@/lib/api/errors";-import { TOKEN_EXPIRY, TOKEN_LENGTH, TOKEN_PREFIX } from "@/lib/api/oauth";+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";import { getAuthTokenOrThrow, hashToken } from "@/lib/auth";import { prisma } from "@/lib/prisma";import z from "@/lib/zod";@@ -94,9 +101,11 @@ export const refreshAccessToken = async (id: accessTokenId,} = accessToken;- 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);+ const newAccessToken = `${OAUTH_ACCESS_TOKEN_PREFIX}${nanoid(OAUTH_ACCESS_TOKEN_LENGTH)}`;+ const newRefreshToken = `${OAUTH_REFRESH_TOKEN_PREFIX}${nanoid(OAUTH_REFRESH_TOKEN_LENGTH)}`;+ const accessTokenExpires = new Date(+ Date.now() + OAUTH_ACCESS_TOKEN_LIFETIME * 1000,+ );await Promise.all([// Create the access token and refresh token@@ -114,7 +123,9 @@ export const refreshAccessToken = async (create: {clientId,refreshTokenHashed: await hashToken(newRefreshToken),- expiresAt: new Date(Date.now() + TOKEN_EXPIRY.refreshToken),+ expiresAt: new Date(+ Date.now() + OAUTH_REFRESH_TOKEN_LIFETIME * 1000,+ ),},},},@@ -128,6 +139,7 @@ export const refreshAccessToken = async (}),// Delete the old refresh token+ // TODO: We we may not do this, cascade delete should take care of thisprisma.oAuthRefreshToken.delete({where: {id: refreshTokenId,@@ -139,7 +151,7 @@ export const refreshAccessToken = async (access_token: newAccessToken,refresh_token: newRefreshToken,token_type: "Bearer",- expires_in: Math.floor(accessTokenExpires.getTime() / 1000), // TODO: Fix this+ expires_in: OAUTH_ACCESS_TOKEN_LIFETIME,};return response;diff --git a/apps/web/lib/api/oauth.ts b/apps/web/lib/api/oauth.tsindex 1bb8e1e75a..78832b45b2 100644--- a/apps/web/lib/api/oauth.ts+++ b/apps/web/lib/api/oauth.ts@@ -1,21 +1,16 @@-export const TOKEN_LENGTH = {- clientId: 32,- clientSecret: 32,- accessToken: 40,- refreshToken: 40,- code: 40,-};+// 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-export const TOKEN_EXPIRY = {- code: 2 * 60 * 1000, // 2 minutes- accessToken: 2 * 60 * 60 * 1000, // 2 hours- refreshToken: 60 * 24 * 60 * 60 * 1000, // 60 days-};+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;// 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_",-};+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_";diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prismaindex fb585c939f..cd9daabf6a 100644--- a/apps/web/prisma/schema.prisma+++ b/apps/web/prisma/schema.prisma@@ -374,6 +374,7 @@ model OAuthClient {clientSecretHashed StringredirectUri Stringscopes String+ logo String?createdAt DateTime @default(now())updatedAt DateTime @updatedAtprojectId String