Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signature patch #13

Merged
merged 2 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Exceptions/WebhookFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class WebhookFailed extends Exception
{
public static function invalidSignature(): self
{
return new static('The signature is invalid.');
}

public static function signingSecretNotSet(): self
{
return new static('The webhook signing secret is not set. Make sure that the `signing_secret` config key is set to the correct value.');
Expand Down
12 changes: 9 additions & 3 deletions src/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

namespace BinaryCats\MailgunWebhooks;

use BinaryCats\MailgunWebhooks\Exceptions\WebhookFailed;

class Webhook
{
/**
* Validate and raise an appropriate event.
*
* @param $payload
* @param array $signature
* @param string $secret
* @param array $signature
* @param string $secret
* @return BinaryCats\MailgunWebhooks\Event
* @throws WebhookFailed
*/
public static function constructEvent(array $payload, array $signature, string $secret): Event
{
// verify we are good, else throw an expection
WebhookSignature::make($signature, $secret)->verify();
if (!WebhookSignature::make($signature, $secret)->verify()) {
throw WebhookFailed::invalidSignature();
}

// Make an event
return Event::constructFrom($payload);
}
Expand Down
30 changes: 29 additions & 1 deletion tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,38 @@ public function a_request_with_a_config_key_will_use_the_correct_signing_secret(
],
];

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

$this
->postJson('mailgun-webhooks/somekey', $payload)
->assertSuccessful();
}


/** @test */
public function an_invalid_signature_value_generates_a_500_error()
{
$payload = [
'event-data' => [
'event' => 'my.type',
'key' => 'value',
],
];

Arr::set($payload, 'signature', [
'timestamp' => time(),
'token' => 'some token',
'signature' => 'invalid_signature'
]);

$this
->postJson('mailgun-webhooks', $payload)
->assertStatus(500);

$this->assertCount(0, WebhookCall::get());

Event::assertNotDispatched('mailgun-webhooks::my.type');

$this->assertNull(cache('dummyjob'));
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function determineMailgunSignature(array $payload, string $configKey =
return [
'timestamp' => $timestamp,
'token' => $token,
'signature' => hash_hmac('sha256', "{$timestamp}.{$token}", $secret),
'signature' => hash_hmac('sha256', "{$timestamp}{$token}", $secret),
];
}
}