1 | <?php |
||
2 | |||
3 | namespace Multicoin\Api\Http\Middlewares; |
||
4 | |||
5 | use Closure; |
||
6 | use Multicoin\Api\Exceptions\WebhookFailed; |
||
7 | |||
8 | class VerifySignature |
||
9 | { |
||
10 | public function handle($request, Closure $next) |
||
11 | { |
||
12 | $signature = $request->header('X-Multicoin-Signature'); |
||
13 | |||
14 | if (! $signature) { |
||
15 | throw WebhookFailed::missingSignature(); |
||
16 | } |
||
17 | |||
18 | if (! $this->isValid($signature, $request)) { |
||
19 | throw WebhookFailed::invalidSignature($signature); |
||
20 | } |
||
21 | |||
22 | return $next($request); |
||
23 | } |
||
24 | |||
25 | protected function isValid(string $signature, $request): bool |
||
26 | { |
||
27 | $payload = $request->getContent(); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
28 | $secret = config('multicoin.api_key'); |
||
29 | if (empty($secret)) { |
||
30 | throw WebhookFailed::signingSecretNotSet(); |
||
31 | } |
||
32 | $timestamp = $request->header('timestamp'); |
||
33 | $token = $request->header('token'); |
||
34 | $computedSignature = hash_hmac('sha256', $token.$timestamp, $secret); |
||
35 | |||
36 | return hash_equals($signature, $computedSignature); |
||
37 | } |
||
38 | } |
||
39 |