2022-08-06 23:46:28 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class AuthenticationMiddleware
|
|
|
|
{
|
2023-03-06 22:43:44 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (Configuration::getConfig('authentication', 'password') === '') {
|
|
|
|
throw new \Exception('The authentication password cannot be the empty string');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
public function __invoke(): void
|
|
|
|
{
|
|
|
|
$user = $_SERVER['PHP_AUTH_USER'] ?? null;
|
|
|
|
$password = $_SERVER['PHP_AUTH_PW'] ?? null;
|
|
|
|
|
|
|
|
if ($user === null || $password === null) {
|
2022-10-16 18:55:43 +03:00
|
|
|
print $this->renderAuthenticationDialog();
|
2022-08-06 23:46:28 +03:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
Configuration::getConfig('authentication', 'username') === $user
|
|
|
|
&& Configuration::getConfig('authentication', 'password') === $password
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-16 18:55:43 +03:00
|
|
|
print $this->renderAuthenticationDialog();
|
2022-08-06 23:46:28 +03:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2022-10-16 18:55:43 +03:00
|
|
|
private function renderAuthenticationDialog(): string
|
2022-08-06 23:46:28 +03:00
|
|
|
{
|
|
|
|
http_response_code(401);
|
|
|
|
header('WWW-Authenticate: Basic realm="RSS-Bridge"');
|
2023-09-23 19:54:14 +03:00
|
|
|
return render(__DIR__ . '/../templates/error.html.php', [
|
|
|
|
'message' => 'Please authenticate in order to access this instance!',
|
|
|
|
]);
|
2022-08-06 23:46:28 +03:00
|
|
|
}
|
|
|
|
}
|