1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: eudaldarranztresserra |
||
5 | * Date: 2019-03-01 |
||
6 | * Time: 13:06 |
||
7 | */ |
||
8 | |||
9 | namespace Nestednet\Gocardless\Middlewares; |
||
10 | |||
11 | use Closure; |
||
12 | use Exception; |
||
13 | use GoCardlessPro\Webhook; |
||
14 | use Nestednet\Gocardless\Exceptions\WebhookFailed; |
||
15 | |||
16 | class VerifySignature |
||
17 | { |
||
18 | public function handle($request, Closure $next) |
||
19 | { |
||
20 | $signature = $request->header('Webhook-Signature'); |
||
21 | |||
22 | if (!$signature) { |
||
23 | throw WebhookFailed::missingSignature(); |
||
24 | } |
||
25 | |||
26 | if (!$this->isValid($signature, $request->getContent(), $request->route('configKey'))) { |
||
27 | throw WebhookFailed::invalidSignature($signature); |
||
28 | } |
||
29 | |||
30 | return $next($request); |
||
31 | } |
||
32 | |||
33 | protected function isValid(string $signature, string $payload, string $configKey = null) : bool |
||
34 | { |
||
35 | $secret = ($configKey) ? |
||
36 | config('gocardless.webhooks.webhook_endpoint_secret_' . $configKey) : config('gocardless.webhooks.webhook_endpoint_secret'); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
37 | |||
38 | if (empty($secret)) { |
||
39 | throw WebhookFailed::noSecretKeyProvided(); |
||
40 | } |
||
41 | |||
42 | try { |
||
43 | Webhook::parse($payload, $signature, $secret); |
||
44 | } catch (Exception $e) { |
||
45 | return false; |
||
46 | } |
||
47 | |||
48 | return true; |
||
49 | } |
||
50 | |||
51 | } |