|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Convoy; |
| 4 | + |
| 5 | +use Convoy\Exceptions\WebhookVerificationException; |
| 6 | + |
| 7 | +class Webhook |
| 8 | +{ |
| 9 | + public const DEFAULT_TOLERANCE = 300; |
| 10 | + public const DEFAULT_ENCODING = 'hex'; |
| 11 | + public const DEFAULT_HASH = 'sha256'; |
| 12 | + |
| 13 | + public string $secret; |
| 14 | + |
| 15 | + public int $tolerance; |
| 16 | + |
| 17 | + public string $hash; |
| 18 | + |
| 19 | + public string $encoding; |
| 20 | + |
| 21 | + public function __construct($secret, $hash = self::DEFAULT_HASH, $encoding = self::DEFAULT_ENCODING, $tolerance = self::DEFAULT_TOLERANCE) |
| 22 | + { |
| 23 | + $this->secret = $secret; |
| 24 | + |
| 25 | + $this->hash = $hash; |
| 26 | + |
| 27 | + $this->encoding = $encoding; |
| 28 | + |
| 29 | + $this->tolerance = $tolerance; |
| 30 | + } |
| 31 | + |
| 32 | + public function verify(string $payload, string $header): bool |
| 33 | + { |
| 34 | + $parts = explode(',', $header); |
| 35 | + |
| 36 | + return count($parts) > 1 ? $this->verifyAdvancedSignature($payload, $header) : $this->verifySimpleSignature($payload, $header); |
| 37 | + } |
| 38 | + |
| 39 | + private function verifyAdvancedSignature(string $payload, string $header): bool |
| 40 | + { |
| 41 | + $timestamp = $this->getTimestamp($header); |
| 42 | + |
| 43 | + $signatures = $this->getSignatures($header); |
| 44 | + |
| 45 | + if ($timestamp === -1) { |
| 46 | + throw new WebhookVerificationException('Webhook has invalid header'); |
| 47 | + } |
| 48 | + |
| 49 | + $tolerance = $this->tolerance; |
| 50 | + |
| 51 | + if (($tolerance > 0) && (abs(time()) - $timestamp) > $tolerance) { |
| 52 | + throw new WebhookVerificationException('Timestamp has expired'); |
| 53 | + } |
| 54 | + |
| 55 | + $signedPayload = "{$timestamp},{$payload}"; |
| 56 | + $expectedSignature = $this->computeSignature($signedPayload); |
| 57 | + $signatureFound = false; |
| 58 | + |
| 59 | + foreach ($signatures as $signature) { |
| 60 | + if ($this->secureCompare($expectedSignature, $signature)) { |
| 61 | + $signatureFound = true; |
| 62 | + |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (! $signatureFound) { |
| 68 | + throw new WebhookVerificationException('Webhook has no valid signature'); |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + private function getTimestamp(string $header): int |
| 75 | + { |
| 76 | + $items = explode(',', $header); |
| 77 | + |
| 78 | + foreach ($items as $item) { |
| 79 | + $itemParts = explode('=', $item, 2); |
| 80 | + if ('t' === $itemParts[0]) { |
| 81 | + if (! is_numeric($itemParts[1])) { |
| 82 | + return -1; |
| 83 | + } |
| 84 | + |
| 85 | + return (int) ($itemParts[1]); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return -1; |
| 90 | + } |
| 91 | + |
| 92 | + private function getSignatures(string $header): array |
| 93 | + { |
| 94 | + $items = explode(',', $header); |
| 95 | + |
| 96 | + $signatures = []; |
| 97 | + |
| 98 | + foreach ($items as $item) { |
| 99 | + $itemParts = explode('=', $item, 2); |
| 100 | + if (str_contains(trim($itemParts[0]), "v")) { |
| 101 | + $signatures[] = $itemParts[1]; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return $signatures; |
| 106 | + } |
| 107 | + |
| 108 | + private function verifySimpleSignature(string $payload, string $header): bool |
| 109 | + { |
| 110 | + $signature = $this->computeSignature($payload, $header); |
| 111 | + |
| 112 | + return $this->secureCompare($this->computeSignature($payload), $header); |
| 113 | + } |
| 114 | + |
| 115 | + private function computeSignature(string $payload) |
| 116 | + { |
| 117 | + switch ($this->encoding) { |
| 118 | + case 'hex': |
| 119 | + return hash_hmac($this->hash, $payload, $this->secret); |
| 120 | + case 'base64': |
| 121 | + return base64_encode(hash_hmac($this->hash, $payload, $this->secret, true)); |
| 122 | + default: |
| 123 | + throw new WebhookVerificationException('Invalid Encoding'); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private function secureCompare(string $a, string $b): bool |
| 128 | + { |
| 129 | + return hash_equals($a, $b); |
| 130 | + } |
| 131 | +} |
0 commit comments