4
0

FormAuth.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_FormAuth {
  4. public static function checkCredentials(string $username, #[\SensitiveParameter] string $hash,
  5. string $nonce, #[\SensitiveParameter] string $challenge): bool {
  6. if (!FreshRSS_user_Controller::checkUsername($username) ||
  7. !ctype_graph($hash) ||
  8. !ctype_graph($challenge) ||
  9. !ctype_alnum($nonce)) {
  10. Minz_Log::debug("Invalid credential parameters: user={$username}, challenge={$challenge}, nonce={$nonce}");
  11. return false;
  12. }
  13. // Expecting bcrypt format, see: https://en.wikipedia.org/wiki/Bcrypt#Description
  14. if (!preg_match('/^\$2[aby]\$(0[4-9]|10)\$[.\/0-9A-Za-z]{53}$/', $challenge)) {
  15. Minz_Log::debug("Invalid challenge format: user={$username}, challenge={$challenge}, nonce={$nonce}");
  16. return false;
  17. }
  18. return password_verify($hash . $nonce, $challenge);
  19. }
  20. /** @return list<string> */
  21. public static function getCredentialsFromCookie(): array {
  22. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  23. if (!ctype_alnum($token)) {
  24. return [];
  25. }
  26. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  27. $mtime = @filemtime($token_file) ?: 0;
  28. $limits = FreshRSS_Context::systemConf()->limits;
  29. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  30. if ($mtime + $cookie_duration < time()) {
  31. // Token has expired (> cookie_duration) or does not exist.
  32. @unlink($token_file);
  33. return [];
  34. }
  35. $credentials = @file_get_contents($token_file);
  36. if ($credentials !== false && self::renewCookie($token) != false) {
  37. return explode("\t", $credentials, 2);
  38. }
  39. return [];
  40. }
  41. private static function renewCookie(string $token): string|false {
  42. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  43. if (touch($token_file)) {
  44. $limits = FreshRSS_Context::systemConf()->limits;
  45. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  46. $expire = time() + $cookie_duration;
  47. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  48. return $token;
  49. }
  50. return false;
  51. }
  52. public static function makeCookie(string $username, #[\SensitiveParameter] string $password_hash): string|false {
  53. do {
  54. $token = hash('sha256', FreshRSS_Context::systemConf()->salt . $username . random_bytes(32));
  55. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  56. } while (file_exists($token_file));
  57. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  58. return false;
  59. }
  60. return self::renewCookie($token);
  61. }
  62. public static function deleteCookie(): void {
  63. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  64. if (ctype_alnum($token)) {
  65. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  66. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  67. }
  68. if (rand(0, 10) === 1) {
  69. self::purgeTokens();
  70. }
  71. }
  72. public static function purgeTokens(): void {
  73. $limits = FreshRSS_Context::systemConf()->limits;
  74. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  75. $oldest = time() - $cookie_duration;
  76. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  77. $extension = $file_info->getExtension();
  78. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  79. @unlink($file_info->getPathname());
  80. }
  81. }
  82. }
  83. }