FormAuth.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. return password_verify($nonce . $hash, $challenge);
  13. }
  14. /** @return array<string> */
  15. public static function getCredentialsFromCookie(): array {
  16. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  17. if (!ctype_alnum($token)) {
  18. return [];
  19. }
  20. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  21. $mtime = @filemtime($token_file) ?: 0;
  22. $limits = FreshRSS_Context::systemConf()->limits;
  23. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  24. if ($mtime + $cookie_duration < time()) {
  25. // Token has expired (> cookie_duration) or does not exist.
  26. @unlink($token_file);
  27. return [];
  28. }
  29. $credentials = @file_get_contents($token_file);
  30. if ($credentials !== false && self::renewCookie($token) != false) {
  31. return explode("\t", $credentials, 2);
  32. }
  33. return [];
  34. }
  35. private static function renewCookie(string $token): string|false {
  36. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  37. if (touch($token_file)) {
  38. $limits = FreshRSS_Context::systemConf()->limits;
  39. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  40. $expire = time() + $cookie_duration;
  41. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  42. return $token;
  43. }
  44. return false;
  45. }
  46. public static function makeCookie(string $username, string $password_hash): string|false {
  47. do {
  48. $token = sha1(FreshRSS_Context::systemConf()->salt . $username . uniqid('' . mt_rand(), true));
  49. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  50. } while (file_exists($token_file));
  51. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  52. return false;
  53. }
  54. return self::renewCookie($token);
  55. }
  56. public static function deleteCookie(): void {
  57. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  58. if (ctype_alnum($token)) {
  59. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  60. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  61. }
  62. if (rand(0, 10) === 1) {
  63. self::purgeTokens();
  64. }
  65. }
  66. public static function purgeTokens(): void {
  67. $limits = FreshRSS_Context::systemConf()->limits;
  68. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  69. $oldest = time() - $cookie_duration;
  70. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  71. $extension = $file_info->getExtension();
  72. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  73. @unlink($file_info->getPathname());
  74. }
  75. }
  76. }
  77. }