Test Failed
Push — master ( 1068e7...19b6ab )
by Thomas
02:42
created

MailgunWebhook::verify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 0
cts 7
cp 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Tompec\EmailLog\Middlewares;
4
5
use Closure;
6
use Illuminate\Http\Response;
7
8
class MailgunWebhook
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @return mixed
16
     */
17
    public function handle($request, Closure $next)
18
    {
19
        if (! $this->verify($request)) {
20
            abort(Response::HTTP_FORBIDDEN);
21
        }
22
23
        return $next($request);
24
    }
25
26
    public function verify($request)
27
    {
28
        $token = $request->input('signature.token');
29
        $timestamp = $request->input('signature.timestamp');
30
        $signature = $request->input('signature.signature');
31
32
        // check if the timestamp is fresh
33
        if (abs(time() - $timestamp) > 15) {
34
            return false;
35
        }
36
37
        // returns true if signature is valid
38
        return hash_hmac('sha256', $timestamp.$token, config('services.mailgun.secret')) === $signature;
39
    }
40
}
41