|
| 1 | +import crypto from 'crypto' |
| 2 | +import { db, webhook } from '@sim/db' |
| 3 | +import { createLogger } from '@sim/logger' |
| 4 | +import { and, eq, isNull } from 'drizzle-orm' |
| 5 | +import type { NextRequest } from 'next/server' |
| 6 | +import { NextResponse } from 'next/server' |
| 7 | +import { safeCompare } from '@/lib/core/security/encryption' |
| 8 | +import type { |
| 9 | + AuthContext, |
| 10 | + EventMatchContext, |
| 11 | + WebhookProviderHandler, |
| 12 | +} from '@/lib/webhooks/providers/types' |
| 13 | + |
| 14 | +const logger = createLogger('WebhookProvider:Zoom') |
| 15 | + |
| 16 | +/** |
| 17 | + * Validate Zoom webhook signature using HMAC-SHA256. |
| 18 | + * Zoom sends `x-zm-signature` as `v0=<hex>` and `x-zm-request-timestamp`. |
| 19 | + * The message to hash is `v0:{timestamp}:{rawBody}`. |
| 20 | + */ |
| 21 | +function validateZoomSignature( |
| 22 | + secretToken: string, |
| 23 | + signature: string, |
| 24 | + timestamp: string, |
| 25 | + body: string |
| 26 | +): boolean { |
| 27 | + try { |
| 28 | + if (!secretToken || !signature || !timestamp || !body) { |
| 29 | + return false |
| 30 | + } |
| 31 | + |
| 32 | + const message = `v0:${timestamp}:${body}` |
| 33 | + const computedHash = crypto.createHmac('sha256', secretToken).update(message).digest('hex') |
| 34 | + const expectedSignature = `v0=${computedHash}` |
| 35 | + |
| 36 | + return safeCompare(expectedSignature, signature) |
| 37 | + } catch (err) { |
| 38 | + logger.error('Zoom signature validation error', err) |
| 39 | + return false |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export const zoomHandler: WebhookProviderHandler = { |
| 44 | + verifyAuth({ request, rawBody, requestId, providerConfig }: AuthContext) { |
| 45 | + const secretToken = providerConfig.secretToken as string | undefined |
| 46 | + if (!secretToken) { |
| 47 | + return null |
| 48 | + } |
| 49 | + |
| 50 | + const signature = request.headers.get('x-zm-signature') |
| 51 | + const timestamp = request.headers.get('x-zm-request-timestamp') |
| 52 | + |
| 53 | + if (!signature || !timestamp) { |
| 54 | + logger.warn(`[${requestId}] Zoom webhook missing signature or timestamp header`) |
| 55 | + return new NextResponse('Unauthorized - Missing Zoom signature', { status: 401 }) |
| 56 | + } |
| 57 | + |
| 58 | + if (!validateZoomSignature(secretToken, signature, timestamp, rawBody)) { |
| 59 | + logger.warn(`[${requestId}] Zoom webhook signature verification failed`) |
| 60 | + return new NextResponse('Unauthorized - Invalid Zoom signature', { status: 401 }) |
| 61 | + } |
| 62 | + |
| 63 | + return null |
| 64 | + }, |
| 65 | + |
| 66 | + async matchEvent({ webhook: wh, workflow, body, requestId, providerConfig }: EventMatchContext) { |
| 67 | + const triggerId = providerConfig.triggerId as string | undefined |
| 68 | + const obj = body as Record<string, unknown> |
| 69 | + const event = obj.event as string | undefined |
| 70 | + |
| 71 | + if (triggerId) { |
| 72 | + const { isZoomEventMatch } = await import('@/triggers/zoom/utils') |
| 73 | + if (!isZoomEventMatch(triggerId, event || '')) { |
| 74 | + logger.debug( |
| 75 | + `[${requestId}] Zoom event mismatch for trigger ${triggerId}. Event: ${event}. Skipping execution.`, |
| 76 | + { |
| 77 | + webhookId: wh.id, |
| 78 | + workflowId: workflow.id, |
| 79 | + triggerId, |
| 80 | + receivedEvent: event, |
| 81 | + } |
| 82 | + ) |
| 83 | + return false |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return true |
| 88 | + }, |
| 89 | + |
| 90 | + /** |
| 91 | + * Handle Zoom endpoint URL validation challenges. |
| 92 | + * Zoom sends an `endpoint.url_validation` event with a `plainToken` that must |
| 93 | + * be hashed with the app's secret token and returned alongside the original token. |
| 94 | + */ |
| 95 | + async handleChallenge(body: unknown, _request: NextRequest, requestId: string, path: string) { |
| 96 | + const obj = body as Record<string, unknown> | null |
| 97 | + if (obj?.event !== 'endpoint.url_validation') { |
| 98 | + return null |
| 99 | + } |
| 100 | + |
| 101 | + const payload = obj.payload as Record<string, unknown> | undefined |
| 102 | + const plainToken = payload?.plainToken as string | undefined |
| 103 | + if (!plainToken) { |
| 104 | + return null |
| 105 | + } |
| 106 | + |
| 107 | + logger.info(`[${requestId}] Zoom URL validation request received for path: ${path}`) |
| 108 | + |
| 109 | + // Look up the webhook record to get the secret token from providerConfig |
| 110 | + let secretToken = '' |
| 111 | + try { |
| 112 | + const webhooks = await db |
| 113 | + .select() |
| 114 | + .from(webhook) |
| 115 | + .where(and(eq(webhook.path, path), isNull(webhook.deletedAt))) |
| 116 | + if (webhooks.length > 0) { |
| 117 | + const config = webhooks[0].providerConfig as Record<string, unknown> | null |
| 118 | + secretToken = (config?.secretToken as string) || '' |
| 119 | + } |
| 120 | + } catch (err) { |
| 121 | + logger.warn(`[${requestId}] Failed to look up webhook secret for Zoom validation`, err) |
| 122 | + } |
| 123 | + |
| 124 | + const hashForValidate = crypto |
| 125 | + .createHmac('sha256', secretToken) |
| 126 | + .update(plainToken) |
| 127 | + .digest('hex') |
| 128 | + |
| 129 | + return NextResponse.json({ |
| 130 | + plainToken, |
| 131 | + encryptedToken: hashForValidate, |
| 132 | + }) |
| 133 | + }, |
| 134 | +} |
0 commit comments