FormAuth.php 3.0 KB

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