mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-19 18:25:37 +02:00
refactor: implement middleware chain (#4240)
* refactor: implement middleware chain * refactor
This commit is contained in:
parent
e7ae06dcf0
commit
39952c2d95
7 changed files with 128 additions and 61 deletions
29
middlewares/TokenAuthenticationMiddleware.php
Normal file
29
middlewares/TokenAuthenticationMiddleware.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
class TokenAuthenticationMiddleware implements Middleware
|
||||
{
|
||||
public function __invoke(Request $request, $next): Response
|
||||
{
|
||||
if (! Configuration::getConfig('authentication', 'token')) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Always add token to request attribute
|
||||
$request = $request->withAttribute('token', $request->get('token'));
|
||||
|
||||
if (! $request->attribute('token')) {
|
||||
return new Response(render(__DIR__ . '/../templates/token.html.php', [
|
||||
'message' => 'Missing token',
|
||||
]), 401);
|
||||
}
|
||||
if (! hash_equals(Configuration::getConfig('authentication', 'token'), $request->attribute('token'))) {
|
||||
return new Response(render(__DIR__ . '/../templates/token.html.php', [
|
||||
'message' => 'Invalid token',
|
||||
]), 401);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue