-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidateMailgun.php
57 lines (48 loc) · 1.32 KB
/
ValidateMailgun.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class ValidateMailgun
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!$this->hasRequiredFields($request)) {
abort(403, 'Missing important headers');
}
if (!$this->validateMailgunToken($request)) {
abort(403, 'Mailgun token does not match signing key');
}
return $next($request);
}
/**
* Validate that the mailgun signature matches the timestamp/token/key hash
*
* @param $request
* @return bool
*/
public function validateMailgunToken($request)
{
$timestamp = $request->input('timestamp');
$token = $request->input('token');
$key = config('mail.webhook.signing_key');
$hash = hash_hmac("sha256", $timestamp . $token, $key, false);
return $hash === $request->input('signature');
}
/**
* Validate that the incoming request has the appropriate mailgun headers
*
* @param $request
* @return bool
*/
public function hasRequiredFields($request)
{
return $request->filled(['token','timestamp', 'signature']);
}
}