|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the phpCacheAdmin. |
| 4 | + * Copyright (c) Róbert Kelčák (https://kelcak.com/) |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace RobiNN\Pca; |
| 10 | + |
| 11 | +class Auth { |
| 12 | + /** |
| 13 | + * Renders the login page and exits when the user is not authenticated. |
| 14 | + */ |
| 15 | + public static function check(): void { |
| 16 | + $users = self::users(); |
| 17 | + |
| 18 | + if ($users === []) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // Allow a cronjob to collect metrics without a login session. |
| 23 | + if (self::validToken()) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + self::login($users); |
| 28 | + } |
| 29 | + |
| 30 | + public static function isEnabled(): bool { |
| 31 | + return self::users() !== []; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Configured users as `username => password`, defined via the `authusers` config option. |
| 36 | + * |
| 37 | + * @return array<array-key, scalar> |
| 38 | + */ |
| 39 | + private static function users(): array { |
| 40 | + return array_filter((array) Config::get('authusers', []), static fn (mixed $password): bool => is_scalar($password)); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param array<array-key, scalar> $users |
| 45 | + */ |
| 46 | + private static function login(array $users): void { |
| 47 | + if (session_status() === PHP_SESSION_NONE) { |
| 48 | + session_start(); |
| 49 | + } |
| 50 | + |
| 51 | + if (isset($_GET['logout'])) { |
| 52 | + unset($_SESSION['pca_auth_user']); |
| 53 | + Http::redirect(); |
| 54 | + } |
| 55 | + |
| 56 | + $logged_user = $_SESSION['pca_auth_user'] ?? null; |
| 57 | + |
| 58 | + if (is_string($logged_user) && isset($users[$logged_user])) { |
| 59 | + return; // Already logged in. |
| 60 | + } |
| 61 | + |
| 62 | + $error = null; |
| 63 | + |
| 64 | + if (isset($_POST['pca_login'])) { |
| 65 | + $username = (string) Http::post('username', ''); |
| 66 | + |
| 67 | + if ( |
| 68 | + Csrf::validateToken(Http::post('csrf_token', '')) && |
| 69 | + self::validate($users, $username, (string) Http::post('password', '')) |
| 70 | + ) { |
| 71 | + session_regenerate_id(true); |
| 72 | + $_SESSION['pca_auth_user'] = $username; |
| 73 | + Http::redirect(); |
| 74 | + } |
| 75 | + |
| 76 | + $error = 'Incorrect username or password.'; |
| 77 | + } |
| 78 | + |
| 79 | + echo (new Template())->render('login', ['error' => $error]); |
| 80 | + exit; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param array<array-key, scalar> $users |
| 85 | + */ |
| 86 | + public static function validate(array $users, ?string $user, ?string $password): bool { |
| 87 | + if ($user === null || $password === null || !isset($users[$user])) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + return hash_equals((string) $users[$user], $password); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * A token lets the metrics cronjob bypass the login while auth is enabled. |
| 96 | + * |
| 97 | + * It only grants access to the metrics collection endpoint (?ajax&metrics), nothing else. |
| 98 | + */ |
| 99 | + private static function validToken(): bool { |
| 100 | + $token = (string) Config::get('authtoken', ''); |
| 101 | + |
| 102 | + if ($token === '' || !isset($_GET['ajax'], $_GET['metrics'])) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + return hash_equals($token, (string) ($_GET['token'] ?? '')); |
| 107 | + } |
| 108 | +} |
0 commit comments