The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
refactor: Update OAuth client scopes in route files
Kiran K42acb047a289
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 570ae02cce..f59c027ed0 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@@ -53,7 +53,7 @@ export const exchangeAuthCodeForToken = async (});}- const oauthGrant = await prisma.oAuthCode.findFirst({+ const accessCode = await prisma.oAuthCode.findUnique({where: {code,clientId,@@ -67,14 +67,14 @@ export const exchangeAuthCodeForToken = async (},});- if (!oauthGrant) {+ if (!accessCode) {throw new DubApiError({code: "unauthorized",message: "Invalid code",});}- if (oauthGrant.expiresAt < new Date()) {+ if (accessCode.expiresAt < new Date()) {await prisma.oAuthCode.delete({where: {code,@@ -87,16 +87,18 @@ export const exchangeAuthCodeForToken = async (});}+ console.log("Found OAuth grant", accessCode);+const workspace = await prisma.project.findUniqueOrThrow({where: {- id: oauthGrant.projectId,+ id: accessCode.projectId,},select: {plan: true,},});- const { userId, projectId, scopes } = oauthGrant;+ const { userId, projectId, scopes } = accessCode;const accessToken = `dub_${nanoid(TOKEN_LENGTH.accessToken)}`;const refreshToken = `dub_${nanoid(TOKEN_LENGTH.refreshToken)}`;@@ -106,6 +108,7 @@ export const exchangeAuthCodeForToken = async (// Create the access token and refresh tokenprisma.restrictedToken.create({data: {+ clientId,userId,projectId,scopes,@@ -139,7 +142,7 @@ export const exchangeAuthCodeForToken = async (expires_in: Math.floor(accessTokenExpires.getTime() / 1000),};- console.log("Token exchanged", response);+ console.log("Access token created", response);return response;};diff --git a/apps/web/app/api/oauth/token/route.ts b/apps/web/app/api/oauth/token/route.tsindex 30b298d422..1c07715886 100644--- a/apps/web/app/api/oauth/token/route.ts+++ b/apps/web/app/api/oauth/token/route.ts@@ -9,14 +9,13 @@ export async function POST(req: NextRequest) {try {const formData = Object.fromEntries(await req.formData());const validatedData = tokenGrantSchema.parse(formData);- const { grant_type } = validatedData;- if (grant_type === "authorization_code") {- const response = await exchangeAuthCodeForToken(req, validatedData);- return NextResponse.json(response);- } else if (grant_type === "refresh_token") {- const response = await refreshAccessToken(req, validatedData);- return NextResponse.json(response);+ if (validatedData.grant_type === "authorization_code") {+ return NextResponse.json(+ await exchangeAuthCodeForToken(req, validatedData),+ );+ } else if (validatedData.grant_type === "refresh_token") {+ return NextResponse.json(await refreshAccessToken(req, validatedData));}} catch (error) {return handleAndReturnErrorResponse(error);diff --git a/apps/web/app/api/tokens/route.ts b/apps/web/app/api/tokens/route.tsindex 09035ea2cf..47964b4677 100644--- a/apps/web/app/api/tokens/route.ts+++ b/apps/web/app/api/tokens/route.ts@@ -90,6 +90,7 @@ export const GET = withWorkspace(const tokens = await prisma.restrictedToken.findMany({where: {projectId: workspace.id,+ clientId: null, // Hide OAuth tokens from workspace tokens},select: {id: true,diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsxindex d9c5491547..829e5866a2 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx@@ -46,7 +46,11 @@ export const AuthorizeForm = (props: AuthorizeFormProps) => {<input type="hidden" name="redirect_uri" value={redirect_uri} /><input type="hidden" name="response_type" value={response_type} /><input type="hidden" name="state" value={state} />- <input type="hidden" name="workspaceId" value={selectedWorkspace?.id} />+ <input+ type="hidden"+ name="workspaceId"+ value={selectedWorkspace?.id.replace("ws_", "")}+ /><p className="text-sm text-gray-500">Select a workspace to grant API access to</p>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.tsxindex 3ac4345aa3..07ce3ae35b 100644--- 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@@ -9,6 +9,7 @@ interface ScopesProps {export const ScopesRequested = (props: ScopesProps) => {const { scopes } = props;+const scopesArray = scopes?.split(" ") ?? [];const requestedScopes = permissions.filter((p) =>diff --git a/apps/web/lib/oauth.ts b/apps/web/lib/oauth.tsindex f4ce463285..99a84a83fe 100644--- a/apps/web/lib/oauth.ts+++ b/apps/web/lib/oauth.ts@@ -49,7 +49,7 @@ export const handleAuthorize = async (formData: FormData) => {Object.fromEntries(formData),);- console.info("Authorize request", request);+ console.info("Authorize request", { ...request, workspaceId });const { code } = await prisma.oAuthCode.create({data: {diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prismaindex 34845ca98f..3bf8e8bbe5 100644--- a/apps/web/prisma/schema.prisma+++ b/apps/web/prisma/schema.prisma@@ -30,6 +30,7 @@ model User {subscribed Boolean @default(true)source String?defaultWorkspace String?+ oAuthCodes OAuthCode[]@@index(source)@@index(defaultWorkspace)@@ -149,6 +150,7 @@ model Project {defaultDomains DefaultDomains[]restrictedTokens RestrictedToken[]oAuthClients OAuthClient[]+ oAuthCodes OAuthCode[]@@index(usageLastChecked(sort: Asc))}@@ -372,7 +374,7 @@ model OAuthClient {projectId Stringproject Project @relation(fields: [projectId], references: [id], onDelete: Cascade)- accessCodes OAuthCode[]+ oAuthCodes OAuthCode[]accessTokens RestrictedToken[]refreshTokens OAuthRefreshToken[]@@ -391,6 +393,8 @@ model OAuthCode {createdAt DateTime @default(now())oAuthClient OAuthClient @relation(fields: [clientId], references: [clientId], onDelete: Cascade)+ user User @relation(fields: [userId], references: [id], onDelete: Cascade)+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)}model OAuthRefreshToken {