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.

add token expiration check

Kiran Ke8aff41afd1e
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 f59c027ed0..908ef5d289 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
@@ -57,12 +57,12 @@ export const exchangeAuthCodeForToken = async (
where: {
code,
clientId,
- redirectUri,
},
select: {
userId: true,
projectId: true,
scopes: true,
+ redirectUri: true,
expiresAt: true,
},
});
@@ -83,11 +83,16 @@ export const exchangeAuthCodeForToken = async (
throw new DubApiError({
code: "bad_request",
- message: "Authorization code has expired.",
+ message: "Authorization code has expired",
});
}
- console.log("Found OAuth grant", accessCode);
+ if (redirectUri !== accessCode.redirectUri) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "redirect_uri does not match",
+ });
+ }
const workspace = await prisma.project.findUniqueOrThrow({
where: {
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 08b5f37f3d..de3ee48a36 100644
--- a/apps/web/app/api/oauth/token/refresh-access-token.ts
+++ b/apps/web/app/api/oauth/token/refresh-access-token.ts
@@ -139,7 +139,7 @@ export const refreshAccessToken = async (
access_token: newAccessToken,
refresh_token: newRefreshToken,
token_type: "Bearer",
- expires_in: Math.floor(accessTokenExpires.getTime() / 1000),
+ expires_in: Math.floor(accessTokenExpires.getTime() / 1000), // TODO: Fix this
};
console.log("Access token refreshed", response);
diff --git a/apps/web/lib/api/oauth.ts b/apps/web/lib/api/oauth.ts
index 3cc11c9656..d3cd8733bd 100644
--- a/apps/web/lib/api/oauth.ts
+++ b/apps/web/lib/api/oauth.ts
@@ -7,7 +7,7 @@ export const TOKEN_LENGTH = {
};
export const TOKEN_EXPIRY = {
- code: 5 * 60 * 1000, // 5 minutes
+ code: 2 * 60 * 1000, // 2 minutes
accessToken: 2 * 60 * 60 * 1000, // 2 hours
refreshToken: 60 * 24 * 60 * 60 * 1000, // 60 days
};
diff --git a/apps/web/lib/auth/workspace-edge.ts b/apps/web/lib/auth/workspace-edge.ts
index 3b7d38beb7..97df656f1b 100644
--- a/apps/web/lib/auth/workspace-edge.ts
+++ b/apps/web/lib/auth/workspace-edge.ts
@@ -127,6 +127,7 @@ export const withWorkspaceEdge = (
scopes: true,
rateLimit: true,
projectId: true,
+ expires: true,
}),
user: {
select: {
@@ -152,6 +153,13 @@ export const withWorkspaceEdge = (
});
}
+ if (token.expires && token.expires < new Date()) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Unauthorized: Access token expired.",
+ });
+ }
+
// Rate limit checks for API keys
const rateLimit = token?.rateLimit || 600;
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index a78bdfb936..c72058bf2b 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -129,6 +129,7 @@ export const withWorkspace = (
scopes: true,
rateLimit: true,
projectId: true,
+ expires: true,
}),
user: {
select: {
@@ -154,6 +155,13 @@ export const withWorkspace = (
});
}
+ if (token.expires && token.expires < new Date()) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Unauthorized: Access token expired.",
+ });
+ }
+
// Rate limit checks for API keys
const rateLimit = token.rateLimit || 600;