The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Implement Singular event authentication and workspace validation
Kiran K1519a61e48fe
diff --git a/apps/web/.env.example b/apps/web/.env.exampleindex 3dc639d9d8..724b0f681e 100644--- a/apps/web/.env.example+++ b/apps/web/.env.example@@ -147,4 +147,7 @@ STRIPE_APP_SECRET_KEY_TEST=STRIPE_APP_SECRET_KEY=# Program lander generation-FIRECRAWL_API_KEY=\ No newline at end of file+FIRECRAWL_API_KEY=++# Set a global token to identify Singular events+SINGULAR_TOKEN=\ No newline at end of filediff --git a/apps/web/app/(ee)/api/singular/webhook/route.ts b/apps/web/app/(ee)/api/singular/webhook/route.tsindex 13883f0764..f2271bae28 100644--- a/apps/web/app/(ee)/api/singular/webhook/route.ts+++ b/apps/web/app/(ee)/api/singular/webhook/route.ts@@ -1,8 +1,10 @@import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";import { trackSingularLeadEvent } from "@/lib/integrations/singular/track-lead";import { trackSingularSaleEvent } from "@/lib/integrations/singular/track-sale";+import { prisma } from "@dub/prisma";import { getSearchParams } from "@dub/utils";import { NextResponse } from "next/server";+import { z } from "zod";const singularToDubEvent = {sng_complete_registration: "lead",@@ -14,13 +16,60 @@ const singularToDubEvent = {const supportedEvents = Object.keys(singularToDubEvent);+const authSchema = z.object({+ dub_token: z+ .string()+ .min(1)+ .describe("Global token to identify Singular events."),+ dub_workspace_id: z+ .string()+ .min(1)+ .describe(" Unique to identify the advertiser."),+});++const singularToken = process.env.SINGULAR_TOKEN;+// GET /api/singular/webhook – listen to Postback events from Singularexport const GET = async (req: Request) => {try {- const searchParams = getSearchParams(req.url);+ if (!singularToken) {+ throw new DubApiError({+ code: "bad_request",+ message: "SINGULAR_TOKEN is not set in the environment variables.",+ });+ }+ const searchParams = getSearchParams(req.url);console.log("[Singular] Postback received", searchParams);+ const { dub_token: token, dub_workspace_id: workspaceId } =+ authSchema.parse(searchParams);++ if (token !== singularToken) {+ throw new DubApiError({+ code: "unauthorized",+ message: "Invalid token.",+ });+ }++ const workspace = await prisma.project.findUnique({+ where: {+ id: workspaceId,+ },+ select: {+ id: true,+ stripeConnectId: true,+ webhookEnabled: true,+ },+ });++ if (!workspace) {+ throw new DubApiError({+ code: "not_found",+ message: `Workspace ${workspaceId} not found.`,+ });+ }+const { event_name: eventName } = searchParams;if (!supportedEvents.includes(eventName)) {@@ -33,9 +82,15 @@ export const GET = async (req: Request) => {const dubEvent = singularToDubEvent[eventName];if (dubEvent === "lead") {- await trackSingularLeadEvent(searchParams);+ await trackSingularLeadEvent({+ searchParams,+ workspace,+ });} else if (dubEvent === "sale") {- await trackSingularSaleEvent(searchParams);+ await trackSingularSaleEvent({+ searchParams,+ workspace,+ });} else {throw new DubApiError({code: "bad_request",diff --git a/apps/web/lib/integrations/singular/track-lead.ts b/apps/web/lib/integrations/singular/track-lead.tsindex 7b087f4c7f..9a05c8ffb0 100644--- a/apps/web/lib/integrations/singular/track-lead.ts+++ b/apps/web/lib/integrations/singular/track-lead.ts@@ -1,6 +1,6 @@import { trackLead } from "@/lib/api/conversions/track-lead";+import { WorkspaceProps } from "@/lib/types";import { trackLeadRequestSchema } from "@/lib/zod/schemas/leads";-import { prisma } from "@dub/prisma";import { z } from "zod";const singularLeadEventSchema = z.object({@@ -27,11 +27,13 @@ const singularLeadEventSchema = z.object({),});-export const trackSingularLeadEvent = async (- searchParams: Record<string, string>,-) => {- console.log("[Singular] Lead event received", searchParams);-+export const trackSingularLeadEvent = async ({+ searchParams,+ workspace,+}: {+ searchParams: Record<string, string>;+ workspace: Pick<WorkspaceProps, "id" | "stripeConnectId" | "webhookEnabled">;+}) => {const {dub_id: clickId,event_name: eventName,@@ -45,20 +47,6 @@ export const trackSingularLeadEvent = async (},} = singularLeadEventSchema.parse(searchParams);- // TODO: Fix this- const workspaceId = "clrei1gld0002vs9mzn93p8ik";-- const workspace = await prisma.project.findUniqueOrThrow({- where: {- id: workspaceId,- },- select: {- id: true,- stripeConnectId: true,- webhookEnabled: true,- },- });-return await trackLead({clickId,eventName,diff --git a/apps/web/lib/integrations/singular/track-sale.ts b/apps/web/lib/integrations/singular/track-sale.tsindex 1f84948c1e..a25f5a395a 100644--- a/apps/web/lib/integrations/singular/track-sale.ts+++ b/apps/web/lib/integrations/singular/track-sale.ts@@ -1,6 +1,6 @@import { trackSale } from "@/lib/api/conversions/track-sale";+import { WorkspaceProps } from "@/lib/types";import { trackSaleRequestSchema } from "@/lib/zod/schemas/sales";-import { prisma } from "@dub/prisma";import { z } from "zod";// TODO:@@ -32,11 +32,13 @@ const singularRevenueEventSchema = z.object({),});-export const trackSingularSaleEvent = async (- searchParams: Record<string, string>,-) => {- console.log("[Singular] Revenue event received", searchParams);-+export const trackSingularSaleEvent = async ({+ searchParams,+ workspace,+}: {+ searchParams: Record<string, string>;+ workspace: Pick<WorkspaceProps, "id" | "stripeConnectId" | "webhookEnabled">;+}) => {const {event_name: eventName,revenue: amount,@@ -49,20 +51,6 @@ export const trackSingularSaleEvent = async (},} = singularRevenueEventSchema.parse(searchParams);- // TODO: Fix this- const workspaceId = "clrei1gld0002vs9mzn93p8ik";-- const workspace = await prisma.project.findUniqueOrThrow({- where: {- id: workspaceId,- },- select: {- id: true,- stripeConnectId: true,- webhookEnabled: true,- },- });-return await trackSale({customerExternalId,amount,