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 support for multiple callback URLs

Kiran Kca3e03326cb4
diff --git a/apps/web/app/api/oauth/apps/[appId]/route.ts b/apps/web/app/api/oauth/apps/[appId]/route.ts
index 3695b19c05..1e14d1417f 100644
--- a/apps/web/app/api/oauth/apps/[appId]/route.ts
+++ b/apps/web/app/api/oauth/apps/[appId]/route.ts
@@ -40,7 +40,7 @@ export const PATCH = withWorkspace(
website,
description,
readme,
- redirectUri,
+ redirectUris,
logo,
pkce,
} = updateOAuthAppSchema.parse(await parseRequestBody(req));
@@ -57,7 +57,7 @@ export const PATCH = withWorkspace(
website,
description,
readme,
- redirectUri,
+ redirectUris,
logo,
pkce,
},
diff --git a/apps/web/app/api/oauth/apps/route.ts b/apps/web/app/api/oauth/apps/route.ts
index e4282c8e15..2094b72337 100644
--- a/apps/web/app/api/oauth/apps/route.ts
+++ b/apps/web/app/api/oauth/apps/route.ts
@@ -1,3 +1,4 @@
+import { DubApiError } from "@/lib/api/errors";
import { OAUTH_CONFIG } from "@/lib/api/oauth/constants";
import { createToken } from "@/lib/api/oauth/utils";
import { parseRequestBody } from "@/lib/api/utils";
@@ -44,11 +45,24 @@ export const POST = withWorkspace(
website,
description,
readme,
- redirectUri,
+ redirectUris,
logo,
pkce,
} = createOAuthAppSchema.parse(await parseRequestBody(req));
+ const app = await prisma.oAuthApp.findUnique({
+ where: {
+ slug,
+ },
+ });
+
+ if (app) {
+ throw new DubApiError({
+ code: "conflict",
+ message: `The slug "${slug}" is already taken for another integration.`,
+ });
+ }
+
const clientId = createToken({
length: OAUTH_CONFIG.CLIENT_ID_LENGTH,
prefix: OAUTH_CONFIG.CLIENT_ID_PREFIX,
@@ -68,7 +82,7 @@ export const POST = withWorkspace(
website,
description,
readme,
- redirectUri,
+ redirectUris,
logo,
clientId,
hashedClientSecret: await hashToken(clientSecret),
diff --git a/apps/web/app/api/oauth/authorize/route.ts b/apps/web/app/api/oauth/authorize/route.ts
index c29644ced2..dccb34ae77 100644
--- a/apps/web/app/api/oauth/authorize/route.ts
+++ b/apps/web/app/api/oauth/authorize/route.ts
@@ -23,12 +23,14 @@ export const POST = withWorkspace(async ({ session, req, workspace }) => {
clientId,
},
select: {
- redirectUri: true,
+ redirectUris: true,
pkce: true,
},
});
- if (app.redirectUri !== redirectUri) {
+ const redirectUris = (app.redirectUris || []) as string[];
+
+ if (!redirectUris.includes(redirectUri)) {
throw new DubApiError({
code: "bad_request",
message: "Invalid redirect_uri parameter for the application.",
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx
index 0b1fd84c48..d8526a7c94 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/integrations/manage/[integrationId]/page-client.tsx
@@ -142,8 +142,8 @@ export default function IntegrationManagePageClient({
<OAuthAppCredentials
clientId={integration.clientId}
clientSecret={
- searchParams.get("client_secret") ||
result.data?.clientSecret ||
+ searchParams.get("client_secret") ||
null
}
/>
diff --git a/apps/web/lib/api/oauth/actions.ts b/apps/web/lib/api/oauth/actions.ts
index 8a63c8aedf..996f7295d3 100644
--- a/apps/web/lib/api/oauth/actions.ts
+++ b/apps/web/lib/api/oauth/actions.ts
@@ -29,7 +29,9 @@ export const vaidateAuthorizeRequest = async (params: any) => {
};
}
- if (oAuthApp.redirectUri !== redirectUri) {
+ const redirectUris = (oAuthApp.redirectUris || []) as string[];
+
+ if (!redirectUris.includes(redirectUri)) {
return {
error: "Invalid redirect_uri parameter for the application.",
};
diff --git a/apps/web/lib/zod/schemas/oauth.ts b/apps/web/lib/zod/schemas/oauth.ts
index f86ac7c1e7..e6a35582d1 100644
--- a/apps/web/lib/zod/schemas/oauth.ts
+++ b/apps/web/lib/zod/schemas/oauth.ts
@@ -18,17 +18,21 @@ export const createOAuthAppSchema = z.object({
})
.max(190)
.nullable(),
- redirectUri: z
+ redirectUris: z
.string()
- .url({ message: "redirect_uri must be a valid URL" })
- .max(190)
+ .array()
+ .min(1, {
+ message: "At least one redirect URI is required",
+ })
+ .max(5, {
+ message: "only 5 redirect URIs are allowed",
+ })
.refine(
- (url) => {
- if (url.startsWith("http://localhost")) {
- return true;
- }
-
- return url.startsWith("https://");
+ (urls) => {
+ return urls.every(
+ (url) =>
+ url.startsWith("https://") || url.startsWith("http://localhost"),
+ );
},
{
message:
@@ -62,7 +66,7 @@ export const oAuthAppSchema = z.object({
readme: z.string().nullish(),
developer: z.string(),
website: z.string(),
- redirectUri: z.string(),
+ redirectUris: z.array(z.string()),
logo: z.string().nullable(),
pkce: z.boolean(),
verified: z.boolean(),
diff --git a/apps/web/prisma/schema/oauth.prisma b/apps/web/prisma/schema/oauth.prisma
index 9037754b6c..bc6fc20bfd 100644
--- a/apps/web/prisma/schema/oauth.prisma
+++ b/apps/web/prisma/schema/oauth.prisma
@@ -8,7 +8,7 @@ model OAuthApp {
developer String
website String
hashedClientSecret String
- redirectUri String
+ redirectUris Json
logo String?
pkce Boolean @default(false)
verified Boolean @default(false)
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index 8d2723f7df..d77edca92b 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -82,10 +82,10 @@ model SentEmail {
}
model NotificationPreference {
- id String @id @default(cuid())
- projectUserId String @unique
- linkUsageSummary Boolean @default(true)
- domainConfigurationUpdates Boolean @default(true)
+ id String @id @default(cuid())
+ projectUserId String @unique
+ linkUsageSummary Boolean @default(true)
+ domainConfigurationUpdates Boolean @default(true)
projectUser ProjectUsers @relation(fields: [projectUserId], references: [id], onDelete: Cascade)
}
diff --git a/apps/web/ui/integrations/add-edit-integration-form.tsx b/apps/web/ui/integrations/add-edit-integration-form.tsx
index b77997bc16..2c155e638b 100644
--- a/apps/web/ui/integrations/add-edit-integration-form.tsx
+++ b/apps/web/ui/integrations/add-edit-integration-form.tsx
@@ -21,7 +21,7 @@ const defaultValues: NewIntegration = {
readme: "",
developer: "",
website: "",
- redirectUri: "",
+ redirectUris: [],
logo: "",
pkce: false,
createdAt: new Date(),
@@ -88,15 +88,15 @@ export default function AddEditIntegrationForm({
if (response.ok) {
mutate(`/api/oauth/apps/${result.id}?workspaceId=${workspaceId}`, result);
toast.success(endpoint.successMessage);
+
+ if (endpoint.method === "POST") {
+ router.push(
+ `/${workspaceSlug}/integrations/manage/${result.id}?client_secret=${result.clientSecret}`,
+ );
+ }
} else {
toast.error(result.error.message);
}
-
- if (endpoint.method === "POST") {
- router.push(
- `/${workspaceSlug}/integrations/manage/${result.id}?client_secret=${result.clientSecret}`,
- );
- }
};
const {
@@ -106,19 +106,19 @@ export default function AddEditIntegrationForm({
readme,
developer,
website,
- redirectUri,
+ redirectUris,
logo,
pkce,
} = data;
const buttonDisabled =
- !name || !slug || !developer || !website || !redirectUri;
+ !name || !slug || !developer || !website || !redirectUris;
return (
<>
<form
onSubmit={onSubmit}
- className="flex flex-col space-y-4 pb-20 text-left"
+ className="flex flex-col space-y-5 pb-20 text-left"
>
<div>
<label htmlFor="name" className="flex items-center space-x-2">
@@ -251,20 +251,24 @@ export default function AddEditIntegrationForm({
</div>
<div>
- <label htmlFor="redirectUri" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">Callback URL</h2>
- <InfoTooltip content="URL to redirect the user after authentication. Must use HTTPS, except for localhost" />
+ <label htmlFor="redirectUris" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Callback URLs</h2>
+ <InfoTooltip content="All OAuth redirect URLs, separated with newlines. Must use HTTPS, except for localhost." />
</label>
<div className="relative mt-2 rounded-md shadow-sm">
- <input
+ <TextareaAutosize
+ name="redirectUris"
+ minRows={3}
className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- type="url"
+ placeholder="https://acme.com/oauth/callback"
+ value={redirectUris.join("\n")}
required
- value={redirectUri}
- onChange={(e) =>
- setData({ ...data, redirectUri: e.target.value })
- }
- placeholder="https://acme.com/callback"
+ onChange={(e) => {
+ setData({
+ ...data,
+ redirectUris: e.target.value.split("\n"),
+ });
+ }}
/>
</div>
</div>