|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OpenAI\Webhooks; |
| 4 | + |
| 5 | +use DateTimeInterface; |
| 6 | +use OpenAI\Exceptions\WebhookVerificationException; |
| 7 | +use Psr\Http\Message\RequestInterface; |
| 8 | +use RuntimeException; |
| 9 | +use UnexpectedValueException; |
| 10 | + |
| 11 | +readonly class WebhookSignatureVerifier |
| 12 | +{ |
| 13 | + private string $secret; |
| 14 | + |
| 15 | + /** |
| 16 | + * @throws UnexpectedValueException |
| 17 | + */ |
| 18 | + public function __construct( |
| 19 | + string $secret, |
| 20 | + private int $tolerance = 300, |
| 21 | + string $secretPrefix = 'whsec_', |
| 22 | + ) { |
| 23 | + if (str_starts_with($secret, $secretPrefix)) { |
| 24 | + $secret = substr($secret, strlen($secretPrefix)); |
| 25 | + } |
| 26 | + |
| 27 | + $this->secret = base64_decode($secret, true) |
| 28 | + ?: throw new UnexpectedValueException('Invalid secret format'); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @throws WebhookVerificationException|RuntimeException |
| 33 | + */ |
| 34 | + public function verify(RequestInterface $request): void |
| 35 | + { |
| 36 | + $body = $request->getBody(); |
| 37 | + $payload = $body->getContents(); |
| 38 | + $body->rewind(); |
| 39 | + |
| 40 | + $this->verifySignature($payload, [ |
| 41 | + 'webhook-id' => trim($request->getHeaderLine('webhook-id')) ?: null, |
| 42 | + 'webhook-timestamp' => trim($request->getHeaderLine('webhook-timestamp')) ?: null, |
| 43 | + 'webhook-signature' => trim($request->getHeaderLine('webhook-signature')) ?: null, |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param array{webhook-id: ?non-falsy-string, webhook-timestamp: ?non-falsy-string, webhook-signature: ?non-falsy-string} $headers |
| 49 | + * |
| 50 | + * @throws WebhookVerificationException |
| 51 | + */ |
| 52 | + final protected function verifySignature(string $payload, array $headers): void |
| 53 | + { |
| 54 | + if (! isset($headers['webhook-id'], $headers['webhook-timestamp'], $headers['webhook-signature'])) { |
| 55 | + throw WebhookVerificationException::missingRequiredHeader(); |
| 56 | + } |
| 57 | + |
| 58 | + [ |
| 59 | + 'webhook-id' => $messageId, |
| 60 | + 'webhook-timestamp' => $messageTimestamp, |
| 61 | + 'webhook-signature' => $messageSignature, |
| 62 | + ] = $headers; |
| 63 | + $timestamp = $this->verifyTimestamp($messageTimestamp); |
| 64 | + $signature = $this->sign($messageId, $timestamp, $payload); |
| 65 | + [, $expectedSignature] = explode(',', $signature, 2); |
| 66 | + $passedSignatures = explode(' ', $messageSignature); |
| 67 | + |
| 68 | + foreach ($passedSignatures as $versionedSignature) { |
| 69 | + [$version, $passedSignature] = explode(',', $versionedSignature, 2); |
| 70 | + |
| 71 | + if (strcmp($version, 'v1') !== 0) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + if (hash_equals($expectedSignature, $passedSignature)) { |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + throw WebhookVerificationException::noMatchingSignature(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @throws WebhookVerificationException |
| 85 | + * |
| 86 | + * @internal |
| 87 | + */ |
| 88 | + final public function sign(string $messageId, DateTimeInterface|int $timestamp, string $payload): string |
| 89 | + { |
| 90 | + $timestamp = match (true) { |
| 91 | + $timestamp instanceof DateTimeInterface => $timestamp->getTimestamp(), |
| 92 | + is_int($timestamp) && $timestamp > 0 => $timestamp, |
| 93 | + default => throw WebhookVerificationException::invalidTimestamp(), |
| 94 | + }; |
| 95 | + |
| 96 | + $hash = hash_hmac( |
| 97 | + 'sha256', |
| 98 | + implode('.', [$messageId, $timestamp, $payload]), |
| 99 | + $this->secret, |
| 100 | + ); |
| 101 | + $signature = base64_encode(pack('H*', $hash)); |
| 102 | + |
| 103 | + return 'v1,'.$signature; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @throws WebhookVerificationException |
| 108 | + */ |
| 109 | + protected function verifyTimestamp(string $timestampHeader): int |
| 110 | + { |
| 111 | + $now = time(); |
| 112 | + $timestamp = (int) $timestampHeader; |
| 113 | + |
| 114 | + if ($timestamp < ($now - $this->tolerance)) { |
| 115 | + throw WebhookVerificationException::timestampMismatch(); |
| 116 | + } |
| 117 | + |
| 118 | + if ($timestamp > ($now + $this->tolerance)) { |
| 119 | + throw WebhookVerificationException::timestampMismatch(); |
| 120 | + } |
| 121 | + |
| 122 | + return $timestamp; |
| 123 | + } |
| 124 | +} |
0 commit comments