Passed
Branch feature/add-webhook-services (535460)
by Eudald
02:00
created

VerifySignature   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 3
A isValid() 0 17 4
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) :
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
            /** @scrutinizer ignore-call */ 
37
            config('gocardless.webhooks.webhook_endpoint_secret_'.$configKey) :
Loading history...
37
            config('gocardless.webhooks.webhook_endpoint_secret');
38
39
        if (empty($secret)) {
40
            throw WebhookFailed::noSecretKeyProvided();
41
        }
42
43
        try {
44
            Webhook::parse($payload, $signature, $secret);
45
        } catch (Exception $e) {
46
            return false;
47
        }
48
49
        return true;
50
    }
51
52
}