|
| 1 | +import crypto from 'crypto' |
| 2 | +import { createLogger } from '@sim/logger' |
| 3 | +import { NextResponse } from 'next/server' |
| 4 | +import { safeCompare } from '@/lib/core/security/encryption' |
| 5 | +import type { |
| 6 | + EventMatchContext, |
| 7 | + FormatInputContext, |
| 8 | + FormatInputResult, |
| 9 | + WebhookProviderHandler, |
| 10 | +} from '@/lib/webhooks/providers/types' |
| 11 | +import { createHmacVerifier } from '@/lib/webhooks/providers/utils' |
| 12 | +import { isGreenhouseEventMatch } from '@/triggers/greenhouse/utils' |
| 13 | + |
| 14 | +const logger = createLogger('WebhookProvider:Greenhouse') |
| 15 | + |
| 16 | +/** |
| 17 | + * Validates the Greenhouse HMAC-SHA256 signature. |
| 18 | + * Greenhouse sends: `Signature: sha256 <hexdigest>` |
| 19 | + */ |
| 20 | +function validateGreenhouseSignature(secretKey: string, signature: string, body: string): boolean { |
| 21 | + try { |
| 22 | + if (!secretKey || !signature || !body) { |
| 23 | + return false |
| 24 | + } |
| 25 | + const prefix = 'sha256 ' |
| 26 | + if (!signature.startsWith(prefix)) { |
| 27 | + return false |
| 28 | + } |
| 29 | + const providedDigest = signature.substring(prefix.length) |
| 30 | + const computedDigest = crypto.createHmac('sha256', secretKey).update(body, 'utf8').digest('hex') |
| 31 | + return safeCompare(computedDigest, providedDigest) |
| 32 | + } catch { |
| 33 | + logger.error('Error validating Greenhouse signature') |
| 34 | + return false |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export const greenhouseHandler: WebhookProviderHandler = { |
| 39 | + verifyAuth: createHmacVerifier({ |
| 40 | + configKey: 'secretKey', |
| 41 | + headerName: 'signature', |
| 42 | + validateFn: validateGreenhouseSignature, |
| 43 | + providerLabel: 'Greenhouse', |
| 44 | + }), |
| 45 | + |
| 46 | + async formatInput({ body }: FormatInputContext): Promise<FormatInputResult> { |
| 47 | + const b = body as Record<string, unknown> |
| 48 | + return { |
| 49 | + input: { |
| 50 | + action: b.action, |
| 51 | + payload: b.payload || {}, |
| 52 | + }, |
| 53 | + } |
| 54 | + }, |
| 55 | + |
| 56 | + async matchEvent({ webhook, body, requestId, providerConfig }: EventMatchContext) { |
| 57 | + const triggerId = providerConfig.triggerId as string | undefined |
| 58 | + const b = body as Record<string, unknown> |
| 59 | + const action = b.action as string | undefined |
| 60 | + |
| 61 | + if (triggerId && triggerId !== 'greenhouse_webhook') { |
| 62 | + if (!isGreenhouseEventMatch(triggerId, action || '')) { |
| 63 | + logger.debug( |
| 64 | + `[${requestId}] Greenhouse event mismatch for trigger ${triggerId}. Action: ${action}. Skipping execution.`, |
| 65 | + { |
| 66 | + webhookId: webhook.id, |
| 67 | + triggerId, |
| 68 | + receivedAction: action, |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + return NextResponse.json({ |
| 73 | + message: 'Event type does not match trigger configuration. Ignoring.', |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return true |
| 79 | + }, |
| 80 | +} |
0 commit comments