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

Kiran Kc706acf3753c
diff --git a/apps/web/app/api/oauth/authorize/route.ts b/apps/web/app/api/oauth/authorize/route.ts
index 6e63cf259e..96b8b142f1 100644
--- a/apps/web/app/api/oauth/authorize/route.ts
+++ b/apps/web/app/api/oauth/authorize/route.ts
@@ -17,13 +17,6 @@ export const POST = withWorkspace(async ({ session, req, workspace }) => {
},
});
- console.info("Authorize request", {
- workspaceId: workspace.id,
- state,
- clientId,
- redirectUri,
- });
-
const { code } = await prisma.oAuthCode.create({
data: {
clientId,
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 975915f569..82958aff64 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
@@ -109,6 +109,30 @@ export const exchangeAuthCodeForToken = async (
const refreshToken = `dub_${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({
+ where: {
+ userId_projectId_clientId: {
+ userId,
+ projectId,
+ clientId,
+ },
+ },
+ }),
+
+ prisma.oAuthAuthorizedApp.delete({
+ where: {
+ userId_projectId_clientId: {
+ userId,
+ projectId,
+ clientId,
+ },
+ },
+ }),
+ ]);
+
await Promise.all([
// Create the access token and refresh token
prisma.restrictedToken.create({
@@ -156,7 +180,5 @@ export const exchangeAuthCodeForToken = async (
expires_in: Math.floor(accessTokenExpires.getTime() / 1000),
};
- console.log("Access token created", response);
-
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 de3ee48a36..70be22e78a 100644
--- a/apps/web/app/api/oauth/token/refresh-access-token.ts
+++ b/apps/web/app/api/oauth/token/refresh-access-token.ts
@@ -142,7 +142,5 @@ export const refreshAccessToken = async (
expires_in: Math.floor(accessTokenExpires.getTime() / 1000), // TODO: Fix this
};
- console.log("Access token refreshed", response);
-
return response;
};
diff --git a/apps/web/app/api/oauth/userinfo/route.ts b/apps/web/app/api/oauth/userinfo/route.ts
index 5e33eb97cd..4cf62c901f 100644
--- a/apps/web/app/api/oauth/userinfo/route.ts
+++ b/apps/web/app/api/oauth/userinfo/route.ts
@@ -30,8 +30,6 @@ export async function GET(req: NextRequest) {
},
});
- console.log("/api/oauth/userinfo", tokenRecord);
-
if (!tokenRecord) {
throw new DubApiError({
code: "unauthorized",
@@ -41,12 +39,14 @@ export async function GET(req: NextRequest) {
const { user } = tokenRecord;
- return NextResponse.json({
+ const userInfo = {
id: user.id,
email: user.email,
name: user.name,
avatar_url: user.image,
- });
+ };
+
+ return NextResponse.json(userInfo);
} catch (e) {
return handleAndReturnErrorResponse(e);
}
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index 2287da6b95..fb585c939f 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -104,15 +104,17 @@ model RestrictedToken {
updatedAt DateTime @updatedAt
userId String
projectId String
- clientId String? // if the token is generated by an OAuth client (access token)
+ clientId String? // if the token is generated by an OAuth client
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
oAuthClient OAuthClient? @relation(fields: [clientId], references: [clientId], onDelete: Cascade)
refreshTokens OAuthRefreshToken[]
+ @@unique([userId, projectId, clientId])
@@index([userId])
@@index([projectId])
+ @@index([clientId])
}
model Project {
@@ -426,7 +428,7 @@ model OAuthAuthorizedApp {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
- @@unique([clientId, userId, projectId])
+ @@unique([userId, projectId, clientId])
}
model jackson_index {