Skip to content

Commit dd26a1b

Browse files
authored
Merge pull request #13 from aleahy/SignaturePatch
Signature patch
2 parents 606357d + ec8c239 commit dd26a1b

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/Exceptions/WebhookFailed.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class WebhookFailed extends Exception
99
{
10+
public static function invalidSignature(): self
11+
{
12+
return new static('The signature is invalid.');
13+
}
14+
1015
public static function signingSecretNotSet(): self
1116
{
1217
return new static('The webhook signing secret is not set. Make sure that the `signing_secret` config key is set to the correct value.');

src/Webhook.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
namespace BinaryCats\MailgunWebhooks;
44

5+
use BinaryCats\MailgunWebhooks\Exceptions\WebhookFailed;
6+
57
class Webhook
68
{
79
/**
810
* Validate and raise an appropriate event.
911
*
1012
* @param $payload
11-
* @param array $signature
12-
* @param string $secret
13+
* @param array $signature
14+
* @param string $secret
1315
* @return BinaryCats\MailgunWebhooks\Event
16+
* @throws WebhookFailed
1417
*/
1518
public static function constructEvent(array $payload, array $signature, string $secret): Event
1619
{
1720
// verify we are good, else throw an expection
18-
WebhookSignature::make($signature, $secret)->verify();
21+
if (!WebhookSignature::make($signature, $secret)->verify()) {
22+
throw WebhookFailed::invalidSignature();
23+
}
24+
1925
// Make an event
2026
return Event::constructFrom($payload);
2127
}

tests/IntegrationTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,38 @@ public function a_request_with_a_config_key_will_use_the_correct_signing_secret(
159159
],
160160
];
161161

162-
Arr::set($payload, 'signature', $this->determineMailgunSignature($payload));
162+
Arr::set($payload, 'signature', $this->determineMailgunSignature($payload, 'somekey'));
163163

164164
$this
165165
->postJson('mailgun-webhooks/somekey', $payload)
166166
->assertSuccessful();
167167
}
168+
169+
170+
/** @test */
171+
public function an_invalid_signature_value_generates_a_500_error()
172+
{
173+
$payload = [
174+
'event-data' => [
175+
'event' => 'my.type',
176+
'key' => 'value',
177+
],
178+
];
179+
180+
Arr::set($payload, 'signature', [
181+
'timestamp' => time(),
182+
'token' => 'some token',
183+
'signature' => 'invalid_signature'
184+
]);
185+
186+
$this
187+
->postJson('mailgun-webhooks', $payload)
188+
->assertStatus(500);
189+
190+
$this->assertCount(0, WebhookCall::get());
191+
192+
Event::assertNotDispatched('mailgun-webhooks::my.type');
193+
194+
$this->assertNull(cache('dummyjob'));
195+
}
168196
}

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected function determineMailgunSignature(array $payload, string $configKey =
8888
return [
8989
'timestamp' => $timestamp,
9090
'token' => $token,
91-
'signature' => hash_hmac('sha256', "{$timestamp}.{$token}", $secret),
91+
'signature' => hash_hmac('sha256', "{$timestamp}{$token}", $secret),
9292
];
9393
}
9494
}

0 commit comments

Comments
 (0)