// AK-P16d · PandaDoc BYOK status webhook. PUBLIC endpoint (PandaDoc posts // here, no user auth). The tenant configures this URL in their PandaDoc // account: /webhooks/pandadoc/{tenantId}/{dataConnectionId}?signature=... // Security = HMAC-SHA256 of the raw body with the connection's // webhookSharedKey (timing-safe compare in verifyPandaDocWebhookSignature). // Unverifiable requests are answered 200 without processing so probing // reveals nothing about tenants or connections (mirrors the calendar // webhook's quiet-ack posture). import { type ActionFunctionArgs } from "react-router"; import { verifyPandaDocWebhookSignature } from "@hivegrid/integrations"; import { runWithConnectionCredentials } from "~/utils/integrations/appsEngineService.server"; import { handlePandaDocWebhook } from "~/modules/signature/services/signatureService.server"; const OK = new Response(null, { status: 200 }); const PANDADOC_APP_KEY = "pandadoc-byok"; export const action = async ({ request, params }: ActionFunctionArgs) => { if (request.method !== "POST") { return new Response(null, { status: 405 }); } const tenantId = params.tenantId ?? ""; const connectionId = params.connectionId ?? ""; if (!tenantId || !connectionId) { return OK; } // PandaDoc sends the HMAC as ?signature=; accept a header fallback for // manual replay tooling. const url = new URL(request.url); const signature = url.searchParams.get("signature") ?? request.headers.get("x-pandadoc-signature"); const rawBody = await request.text(); if (!signature || !rawBody) { return OK; } try { await runWithConnectionCredentials({ tenantId, connectionId, expectedAppId: PANDADOC_APP_KEY }, async (credentials) => { const sharedKey = typeof credentials.webhookSharedKey === "string" ? credentials.webhookSharedKey : null; const verified = verifyPandaDocWebhookSignature(rawBody, signature, sharedKey); if (!verified) return; await handlePandaDocWebhook({ tenantId, rawBody, verified }); }); } catch { // Unknown connection / unavailable credentials / malformed payload — // quiet-ack, never leak existence. } return OK; }; // Resource route: no default component. A GET (e.g. health probe) just 200s. export const loader = () => OK;