Skip to content

Commit

Permalink
v3 (#95)
Browse files Browse the repository at this point in the history
* wip

* improve exceptions

* Fix styling

* wip

* wip

* wip

Co-authored-by: freekmurze <[email protected]>
  • Loading branch information
freekmurze and freekmurze authored Jul 25, 2021
1 parent 9629d7f commit 8e412c2
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 39 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

All notable changes to `laravel-webhook-client` will be documented in this file

## 3.0.0 - 2021-07-23
## 3.0.0 - 2021-07-XX

- store headers
- require PHP 8
- require Laravel 8
- internals cleanup


## 2.7.5 - 2021-01-08

- Fix php constraint
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Next, the newly created `WebhookCall` model will be passed to a queued job that
```php
namespace App\Jobs;

use \Spatie\WebhookClient\ProcessWebhookJob as SpatieProcessWebhookJob;
use Spatie\WebhookClient\Jobs\ProcessWebhookJob as SpatieProcessWebhookJob;

class ProcessWebhookJob extends SpatieProcessWebhookJob
{
Expand Down
5 changes: 4 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## From v2 to v3

- add a migration to add a `headers` column to the `webhook_calls` table.
- add a migration to add `url` and `headers` columns to the `webhook_calls` table.

```php
$table->string('url');`
$table->json('headers')->nullable();`
```

- add a key `store_headers` to each entry in `configs` of the `webhook-client` config file. See the default config file for an example.

- the `Spatie\WebhookClient\Events\InvalidSignature` event has been renamed to `Spatie\WebhookClient\Events\InvalidWebhookSignatureEvent`
1 change: 1 addition & 0 deletions database/migrations/create_webhook_calls_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return new class extends Migration
$table->bigIncrements('id');

$table->string('name');
$table->string('url');
$table->json('headers')->nullable();
$table->json('payload')->nullable();
$table->text('exception')->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Http\Request;

class InvalidSignatureEvent
class InvalidWebhookSignatureEvent
{
public function __construct(
public Request $request
Expand Down
7 changes: 6 additions & 1 deletion src/Exceptions/InvalidConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Spatie\WebhookClient\Exceptions;

use Exception;
use Spatie\WebhookClient\ProcessWebhookJob;
use Spatie\WebhookClient\Jobs\ProcessWebhookJob;
use Spatie\WebhookClient\SignatureValidator\SignatureValidator;
use Spatie\WebhookClient\WebhookProfile\WebhookProfile;
use Spatie\WebhookClient\WebhookResponse\RespondsToWebhook;
Expand Down Expand Up @@ -42,4 +42,9 @@ public static function invalidProcessWebhookJob(string $processWebhookJob): self

return new static("`{$processWebhookJob}` is not a valid process webhook job class. A valid class should implement `{$abstractProcessWebhookJob}`.");
}

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.');
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/InvalidWebhookSignature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\WebhookClient\Exceptions;

use Exception;

class InvalidWebhookSignature extends Exception
{
public static function make(): self
{
return new static('The signature is invalid.');
}
}
18 changes: 0 additions & 18 deletions src/Exceptions/WebhookFailed.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace Spatie\WebhookClient;
namespace Spatie\WebhookClient\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\WebhookClient\WebhookConfig;
use Spatie\WebhookClient\WebhookProcessor;

class WebhookController
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Spatie\WebhookClient;
namespace Spatie\WebhookClient\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down
1 change: 1 addition & 0 deletions src/Models/WebhookCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function storeWebhook(WebhookConfig $config, Request $request): We

return self::create([
'name' => $config->name,
'url' => $request->fullUrl(),
'headers' => $headers,
'payload' => $request->input(),
]);
Expand Down
4 changes: 2 additions & 2 deletions src/SignatureValidator/DefaultSignatureValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Spatie\WebhookClient\SignatureValidator;

use Illuminate\Http\Request;
use Spatie\WebhookClient\Exceptions\WebhookFailed;
use Spatie\WebhookClient\Exceptions\InvalidConfig;
use Spatie\WebhookClient\WebhookConfig;

class DefaultSignatureValidator implements SignatureValidator
Expand All @@ -19,7 +19,7 @@ public function isValid(Request $request, WebhookConfig $config): bool
$signingSecret = $config->signingSecret;

if (empty($signingSecret)) {
throw WebhookFailed::signingSecretNotSet();
throw InvalidConfig::signingSecretNotSet();
}

$computedSignature = hash_hmac('sha256', $request->getContent(), $signingSecret);
Expand Down
1 change: 1 addition & 0 deletions src/WebhookClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\WebhookClient\Exceptions\InvalidConfig;
use Spatie\WebhookClient\Http\Controllers\WebhookController;

class WebhookClientServiceProvider extends PackageServiceProvider
{
Expand Down
1 change: 1 addition & 0 deletions src/WebhookConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\WebhookClient;

use Spatie\WebhookClient\Exceptions\InvalidConfig;
use Spatie\WebhookClient\Jobs\ProcessWebhookJob;
use Spatie\WebhookClient\SignatureValidator\SignatureValidator;
use Spatie\WebhookClient\WebhookProfile\WebhookProfile;
use Spatie\WebhookClient\WebhookResponse\DefaultRespondsTo;
Expand Down
15 changes: 8 additions & 7 deletions src/WebhookProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use Exception;
use Illuminate\Http\Request;
use Spatie\WebhookClient\Events\InvalidSignatureEvent;
use Spatie\WebhookClient\Exceptions\WebhookFailed;
use Spatie\WebhookClient\Events\InvalidWebhookSignatureEvent;
use Spatie\WebhookClient\Exceptions\InvalidWebhookSignature;
use Spatie\WebhookClient\Models\WebhookCall;
use Symfony\Component\HttpFoundation\Response;

class WebhookProcessor
{
Expand All @@ -16,7 +17,7 @@ public function __construct(
) {
}

public function process()
public function process(): Response
{
$this->ensureValidSignature();

Expand All @@ -31,12 +32,12 @@ public function process()
return $this->createResponse();
}

protected function ensureValidSignature()
protected function ensureValidSignature(): self
{
if (! $this->config->signatureValidator->isValid($this->request, $this->config)) {
event(new InvalidSignatureEvent($this->request));
event(new InvalidWebhookSignatureEvent($this->request));

throw WebhookFailed::invalidSignature();
throw InvalidWebhookSignature::make();
}

return $this;
Expand All @@ -62,7 +63,7 @@ protected function processWebhook(WebhookCall $webhookCall): void
}
}

protected function createResponse()
protected function createResponse(): Response
{
return $this->config->webhookResponse->respondToValidWebhook($this->request, $this->config);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestClasses/ProcessWebhookJobTestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Spatie\WebhookClient\Tests\TestClasses;

use Spatie\WebhookClient\ProcessWebhookJob;
use Spatie\WebhookClient\Jobs\ProcessWebhookJob;

class ProcessWebhookJobTestClass extends ProcessWebhookJob
{
Expand Down
1 change: 1 addition & 0 deletions tests/TestClasses/WebhookModelWithoutPayloadSaved.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static function storeWebhook(WebhookConfig $config, Request $request): We
{
return WebhookCall::create([
'name' => $config->name,
'url' => 'https://example.com',
'payload' => [],
]);
}
Expand Down
8 changes: 5 additions & 3 deletions tests/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Route;
use Spatie\WebhookClient\Events\InvalidSignatureEvent;
use Spatie\WebhookClient\Events\InvalidWebhookSignatureEvent;
use Spatie\WebhookClient\Models\WebhookCall;
use Spatie\WebhookClient\Tests\TestClasses\CustomRespondsToWebhook;
use Spatie\WebhookClient\Tests\TestClasses\EverythingIsValidSignatureValidator;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function a_request_with_an_invalid_payload_will_not_get_processed()

$this->assertCount(0, WebhookCall::get());
Queue::assertNothingPushed();
Event::assertDispatched(InvalidSignatureEvent::class);
Event::assertDispatched(InvalidWebhookSignatureEvent::class);
}

/** @test */
Expand Down Expand Up @@ -108,7 +108,7 @@ public function it_can_work_with_an_alternative_profile()
->assertSuccessful();

Queue::assertNothingPushed();
Event::assertNotDispatched(InvalidSignatureEvent::class);
Event::assertNotDispatched(InvalidWebhookSignatureEvent::class);
$this->assertCount(0, WebhookCall::get());
}

Expand All @@ -132,6 +132,8 @@ public function it_can_work_with_an_alternative_config()
/** @test */
public function it_can_work_with_an_alternative_model()
{
$this->withoutExceptionHandling();

config()->set('webhook-client.configs.0.webhook_model', WebhookModelWithoutPayloadSaved::class);
$this->refreshWebhookConfigRepository();

Expand Down

0 comments on commit 8e412c2

Please sign in to comment.