|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MailCarrier\Webhooks\Strategies; |
| 4 | + |
| 5 | +use Carbon\CarbonImmutable; |
| 6 | +use Illuminate\Support\Facades\Config; |
| 7 | +use MailCarrier\Webhooks\Dto\IncomingWebhook; |
| 8 | +use MailCarrier\Webhooks\Dto\WebhookData; |
| 9 | +use MailCarrier\Webhooks\Strategies\Contracts\Strategy; |
| 10 | + |
| 11 | +class MailgunStrategy implements Strategy |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Whether to log validation failures. |
| 15 | + */ |
| 16 | + public function isVerbose(): bool |
| 17 | + { |
| 18 | + return Config::get('mailcarrier.webhooks.providers.mailgun.verbose', false); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Whether to throw an exception on validation failure instead of continuing. |
| 23 | + */ |
| 24 | + public function isFatal(): bool |
| 25 | + { |
| 26 | + return Config::get('mailcarrier.webhooks.providers.mailgun.fatal', false); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Get the Mailgun webhook secret from config. |
| 31 | + */ |
| 32 | + private function getSecret(): string |
| 33 | + { |
| 34 | + $secret = Config::get('mailcarrier.webhooks.providers.mailgun.secret'); |
| 35 | + |
| 36 | + if (empty($secret)) { |
| 37 | + throw new \RuntimeException('Mailgun webhook secret is not configured. Please set MAILGUN_WEBHOOK_SECRET in your .env file.'); |
| 38 | + } |
| 39 | + |
| 40 | + return $secret; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Validate the webhook signature using Mailgun's algorithm. |
| 45 | + * |
| 46 | + * @see https://documentation.mailgun.com/docs/mailgun/user-manual/tracking-messages/#securing-webhooks |
| 47 | + */ |
| 48 | + public function validate(IncomingWebhook $webhook): bool |
| 49 | + { |
| 50 | + $signature = $webhook->getBodyValue('signature'); |
| 51 | + |
| 52 | + if (!isset($signature['timestamp'], $signature['token'], $signature['signature'])) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + // Concatenate timestamp and token |
| 57 | + $data = $signature['timestamp'] . $signature['token']; |
| 58 | + |
| 59 | + // Calculate HMAC using SHA256 with secret from config |
| 60 | + $calculatedSignature = hash_hmac('sha256', $data, $this->getSecret()); |
| 61 | + |
| 62 | + // Compare signatures |
| 63 | + return hash_equals($calculatedSignature, $signature['signature']); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Extract structured data from Mailgun's webhook payload. |
| 68 | + * |
| 69 | + * @param array $payload The raw webhook payload from Mailgun |
| 70 | + */ |
| 71 | + public function extract(array $payload): WebhookData |
| 72 | + { |
| 73 | + if (!isset($payload['event-data'])) { |
| 74 | + throw new \InvalidArgumentException('Invalid Mailgun webhook payload: missing event-data'); |
| 75 | + } |
| 76 | + |
| 77 | + $eventData = $payload['event-data']; |
| 78 | + |
| 79 | + if (!isset($eventData['id'], $eventData['event'], $eventData['timestamp'])) { |
| 80 | + throw new \InvalidArgumentException('Invalid Mailgun webhook payload: missing required fields'); |
| 81 | + } |
| 82 | + |
| 83 | + return new WebhookData( |
| 84 | + messageId: $eventData['id'], |
| 85 | + eventName: $eventData['event'], |
| 86 | + date: CarbonImmutable::createFromTimestamp($eventData['timestamp']) |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments