4
0

FormAuth.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  21. * @return array{username: string, password: string}
  22. */
  23. public static function getAutofilledFields(bool $raw = false): array {
  24. $username = Minz_ExtensionManager::callHook(Minz_HookType::AutofillField, 'username');
  25. $password = Minz_ExtensionManager::callHook(Minz_HookType::AutofillField, 'password');
  26. $fields = [
  27. 'username' => is_string($username) ? $username : '',
  28. 'password' => is_string($password) ? $password : '',
  29. ];
  30. if (!$raw) {
  31. foreach ($fields as $k => $v) {
  32. $fields[$k] = htmlspecialchars($v, ENT_COMPAT, 'UTF-8');
  33. }
  34. }
  35. return $fields;
  36. }
  37. /** @return list<string> */
  38. public static function getCredentialsFromCookie(): array {
  39. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  40. if (!ctype_alnum($token)) {
  41. return [];
  42. }
  43. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  44. $mtime = @filemtime($token_file) ?: 0;
  45. $limits = FreshRSS_Context::systemConf()->limits;
  46. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  47. if ($mtime + $cookie_duration < time()) {
  48. // Token has expired (> cookie_duration) or does not exist.
  49. @unlink($token_file);
  50. return [];
  51. }
  52. $credentials = @file_get_contents($token_file);
  53. if ($credentials !== false && self::renewCookie($token) != false) {
  54. return explode("\t", $credentials, 2);
  55. }
  56. return [];
  57. }
  58. private static function renewCookie(string $token): string|false {
  59. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  60. if (touch($token_file)) {
  61. $limits = FreshRSS_Context::systemConf()->limits;
  62. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  63. $expire = time() + $cookie_duration;
  64. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  65. return $token;
  66. }
  67. return false;
  68. }
  69. public static function makeCookie(string $username, #[\SensitiveParameter] string $password_hash): string|false {
  70. do {
  71. $token = hash('sha256', FreshRSS_Context::systemConf()->salt . $username . random_bytes(32));
  72. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  73. } while (file_exists($token_file));
  74. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  75. return false;
  76. }
  77. return self::renewCookie($token);
  78. }
  79. public static function deleteCookie(): void {
  80. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  81. if (ctype_alnum($token)) {
  82. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  83. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  84. }
  85. if (rand(0, 10) === 1) {
  86. self::purgeTokens();
  87. }
  88. }
  89. public static function purgeTokens(): void {
  90. $limits = FreshRSS_Context::systemConf()->limits;
  91. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  92. $oldest = time() - $cookie_duration;
  93. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  94. $extension = $file_info->getExtension();
  95. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  96. @unlink($file_info->getPathname());
  97. }
  98. }
  99. }
  100. }