Skip to content

Commit 5beee5f

Browse files
authored
Merge pull request #40 from Muetze42/development
feat: add EnsureGitHubTokenIsValid middleware
2 parents a64bf7f + b499096 commit 5beee5f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace NormanHuth\HelpersLaravel\App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
class EnsureGitHubTokenIsValid
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param \Illuminate\Http\Request $request
15+
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
16+
*
17+
* @return \Symfony\Component\HttpFoundation\Response
18+
*/
19+
public function handle(Request $request, Closure $next): Response
20+
{
21+
$token = config('services.github.webhook_token');
22+
23+
if (
24+
!$token ||
25+
hash_equals(
26+
'sha256=' . hash_hmac('sha256', $request->getContent(), $token),
27+
$request->header('x-hub-signature-256')
28+
)
29+
) {
30+
return $next($request);
31+
}
32+
33+
abort(404, 'Could not verify GitHub Webhook secret.');
34+
}
35+
}

0 commit comments

Comments
 (0)