mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-01-04 16:26:24 +00:00
3fc38c15a3
* fix(cache): also cache 400 and 404 responses * refactor(token_auth)
33 lines
939 B
PHP
33 lines
939 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class TokenAuthenticationMiddleware implements Middleware
|
|
{
|
|
public function __invoke(Request $request, $next): Response
|
|
{
|
|
if (! Configuration::getConfig('authentication', 'token')) {
|
|
return $next($request);
|
|
}
|
|
|
|
$token = $request->get('token');
|
|
|
|
if (! $token) {
|
|
return new Response(render(__DIR__ . '/../templates/token.html.php', [
|
|
'message' => 'Missing token',
|
|
'token' => '',
|
|
]), 401);
|
|
}
|
|
|
|
if (! hash_equals(Configuration::getConfig('authentication', 'token'), $token)) {
|
|
return new Response(render(__DIR__ . '/../templates/token.html.php', [
|
|
'message' => 'Invalid token',
|
|
'token' => $token,
|
|
]), 401);
|
|
}
|
|
|
|
$request = $request->withAttribute('token', $token);
|
|
|
|
return $next($request);
|
|
}
|
|
}
|