4
0

FormAuth.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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)) {
  31. return explode("\t", $credentials, 2);
  32. }
  33. return [];
  34. }
  35. /** @return string|false */
  36. private static function renewCookie(string $token) {
  37. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  38. if (touch($token_file)) {
  39. $limits = FreshRSS_Context::systemConf()->limits;
  40. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  41. $expire = time() + $cookie_duration;
  42. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  43. return $token;
  44. }
  45. return false;
  46. }
  47. /** @return string|false */
  48. public static function makeCookie(string $username, string $password_hash) {
  49. do {
  50. $token = sha1(FreshRSS_Context::systemConf()->salt . $username . uniqid('' . mt_rand(), true));
  51. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  52. } while (file_exists($token_file));
  53. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  54. return false;
  55. }
  56. return self::renewCookie($token);
  57. }
  58. public static function deleteCookie(): void {
  59. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  60. if (ctype_alnum($token)) {
  61. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  62. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  63. }
  64. if (rand(0, 10) === 1) {
  65. self::purgeTokens();
  66. }
  67. }
  68. public static function purgeTokens(): void {
  69. $limits = FreshRSS_Context::systemConf()->limits;
  70. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  71. $oldest = time() - $cookie_duration;
  72. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  73. $extension = $file_info->getExtension();
  74. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  75. @unlink($file_info->getPathname());
  76. }
  77. }
  78. }
  79. }