DefaultSignatureValidator::isValid()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Spatie\WebhookClient\SignatureValidator;
4
5
use Illuminate\Http\Request;
6
use Spatie\WebhookClient\Exceptions\WebhookFailed;
7
use Spatie\WebhookClient\WebhookConfig;
8
9
class DefaultSignatureValidator implements SignatureValidator
10
{
11
    public function isValid(Request $request, WebhookConfig $config): bool
12
    {
13
        $signature = $request->header($config->signatureHeaderName);
14
15
        if (! $signature) {
16
            return false;
17
        }
18
19
        $signingSecret = $config->signingSecret;
20
21
        if (empty($signingSecret)) {
22
            throw WebhookFailed::signingSecretNotSet();
23
        }
24
25
        $computedSignature = hash_hmac('sha256', $request->getContent(), $signingSecret);
26
27
        return hash_equals($signature, $computedSignature);
28
    }
29
}
30