4
0

FormAuth.php 3.3 KB

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