VerifyWebhookSignature   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 4
1
<?php
2
3
namespace Digikraaft\PaystackWebhooks\Http\Middleware;
4
5
use Closure;
6
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
7
8
class VerifyWebhookSignature
9
{
10
    /**
11
     * Handle the incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @return \Illuminate\Http\Response
16
     *
17
     * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
18
     */
19
    public function handle($request, Closure $next)
20
    {
21
        // validate that callback is coming from Paystack
22
        if ((! $request->isMethod('post')) || ! $request->header('HTTP_X_PAYSTACK_SIGNATURE', null)) {
23
            throw new AccessDeniedHttpException("Invalid Request");
24
        }
25
26
        $input = $request->getContent();
27
        $paystack_key = config('paystackwebhooks.secret', env('PAYSTACK_SECRET'));
28
        if ($request->header('HTTP_X_PAYSTACK_SIGNATURE') !== hash_hmac('sha512', $input, $paystack_key)) {
29
            throw new AccessDeniedHttpException("Access Denied");
30
        }
31
32
        return $next($request);
33
    }
34
}
35