Passed
Push — master ( 8c743d...154964 )
by Eudald
10:11
created

VerifySignature::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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
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) : config('gocardless.webhooks.webhook_endpoint_secret');
Loading history...
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
}