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: Update OAuth app API routes and schemas

Kiran K47d029348a20
diff --git a/apps/web/app/api/oauth-apps/route.ts b/apps/web/app/api/oauth-apps/route.ts
index 7697a7c99f..035d3afb61 100644
--- a/apps/web/app/api/oauth-apps/route.ts
+++ b/apps/web/app/api/oauth-apps/route.ts
@@ -1,3 +1,4 @@
+import { TOKEN_LENGTHS } from "@/lib/api/oauth";
import { parseRequestBody } from "@/lib/api/utils";
import { hashToken, withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
@@ -14,7 +15,7 @@ export const POST = withWorkspace(async ({ req, workspace }) => {
const { name, description, website, redirectUri, scopes } =
createOAuthAppSchema.parse(await parseRequestBody(req));
- const clientSecret = `dub_${nanoid(32)}`;
+ const clientSecret = `dub_${nanoid(TOKEN_LENGTHS.clientSecret)}`;
const app = await prisma.oAuthClient.create({
data: {
@@ -24,7 +25,7 @@ export const POST = withWorkspace(async ({ req, workspace }) => {
website,
redirectUri,
scopes: scopes ? scopes.join(",") : null,
- clientId: nanoid(32),
+ clientId: nanoid(TOKEN_LENGTHS.clientId),
clientSecretHashed: await hashToken(clientSecret),
},
});
diff --git a/apps/web/lib/api/oauth.ts b/apps/web/lib/api/oauth.ts
new file mode 100644
index 0000000000..188c2d5c56
--- /dev/null
+++ b/apps/web/lib/api/oauth.ts
@@ -0,0 +1,11 @@
+export const TOKEN_LENGTHS = {
+ clientId: 32,
+ clientSecret: 40,
+ accessToken: 64,
+ refreshToken: 64,
+};
+
+export const TOKEN_EXPIRY = {
+ accessToken: 1000 * 60 * 60 * 24, // 24 hours
+ refreshToken: 1000 * 60 * 60 * 24 * 30 * 6, // 6 months
+};
diff --git a/apps/web/lib/zod/schemas/oauth-app.ts b/apps/web/lib/zod/schemas/oauth-app.ts
index 5a8c7fc9f6..fec0553f56 100644
--- a/apps/web/lib/zod/schemas/oauth-app.ts
+++ b/apps/web/lib/zod/schemas/oauth-app.ts
@@ -19,3 +19,38 @@ export const createOAuthAppSchema = z.object({
});
export const updateOAuthAppSchema = createOAuthAppSchema.partial();
+
+// Schema for OAuth2.0 Authorization code request
+export const authRequestSchema = z.object({
+ client_id: z.string().min(1, "Missing client_id"),
+ redirect_uri: z.string().url({ message: "redirect_uri must be a valid URL" }),
+ response_type: z.string().refine((responseType) => responseType === "code", {
+ message: "response_type must be 'code'",
+ }),
+ state: z.string().max(255).optional(),
+});
+
+// Schema for OAuth2.0 code exchange request
+export const codeExchangeSchema = z.object({
+ client_id: z.string().min(1, "Missing client_id"),
+ client_secret: z.string().min(1, "Missing client_secret"),
+ code: z.string().min(1, "Missing code"),
+ redirect_uri: z.string().url({ message: "redirect_uri must be a valid URL" }),
+ grant_type: z
+ .literal("authorization_code")
+ .refine((grantType) => grantType === "authorization_code", {
+ message: "grant_type must be 'authorization_code'",
+ }),
+});
+
+// Schema for OAuth2.0 token refresh request
+export const tokenRefreshSchema = z.object({
+ client_id: z.string().min(1, "Missing client_id"),
+ client_secret: z.string().min(1, "Missing client_secret"),
+ refresh_token: z.string().min(1, "Missing refresh_token"),
+ grant_type: z
+ .literal("refresh_token")
+ .refine((grantType) => grantType === "refresh_token", {
+ message: "grant_type must be 'refresh_token'",
+ }),
+});