diff options
author | 2024-08-30 00:07:58 +0200 | |
---|---|---|
committer | 2024-08-30 00:07:58 +0200 | |
commit | 39952c2d95cf4806063abbc2c7508cf9ab4f93e5 (patch) | |
tree | c3528c58eee65819754335c3ba2cd59e08fdf0a0 /middlewares/BasicAuthMiddleware.php | |
parent | e7ae06dcf08f0c977a231bb1ce9cb0b6657b4cfd (diff) | |
download | rss-bridge-39952c2d95cf4806063abbc2c7508cf9ab4f93e5.tar.gz rss-bridge-39952c2d95cf4806063abbc2c7508cf9ab4f93e5.tar.zst rss-bridge-39952c2d95cf4806063abbc2c7508cf9ab4f93e5.zip |
refactor: implement middleware chain (#4240)
* refactor: implement middleware chain
* refactor
Diffstat (limited to 'middlewares/BasicAuthMiddleware.php')
-rw-r--r-- | middlewares/BasicAuthMiddleware.php | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/middlewares/BasicAuthMiddleware.php b/middlewares/BasicAuthMiddleware.php new file mode 100644 index 00000000..6b0803e2 --- /dev/null +++ b/middlewares/BasicAuthMiddleware.php @@ -0,0 +1,38 @@ +<?php + +declare(strict_types=1); + +/** + * HTTP Basic auth check + */ +class BasicAuthMiddleware implements Middleware +{ + public function __invoke(Request $request, $next): Response + { + if (!Configuration::getConfig('authentication', 'enable')) { + return $next($request); + } + + if (Configuration::getConfig('authentication', 'password') === '') { + return new Response('The authentication password cannot be the empty string', 500); + } + $user = $request->server('PHP_AUTH_USER'); + $password = $request->server('PHP_AUTH_PW'); + if ($user === null || $password === null) { + $html = render(__DIR__ . '/../templates/error.html.php', [ + 'message' => 'Please authenticate in order to access this instance!', + ]); + return new Response($html, 401, ['WWW-Authenticate' => 'Basic realm="RSS-Bridge"']); + } + if ( + (Configuration::getConfig('authentication', 'username') !== $user) + || (!hash_equals(Configuration::getConfig('authentication', 'password'), $password)) + ) { + $html = render(__DIR__ . '/../templates/error.html.php', [ + 'message' => 'Please authenticate in order to access this instance!', + ]); + return new Response($html, 401, ['WWW-Authenticate' => 'Basic realm="RSS-Bridge"']); + } + return $next($request); + } +} |