FormAuth.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. class FreshRSS_FormAuth {
  3. public static function checkCredentials($username, $hash, $nonce, $challenge) {
  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:' .
  9. ' user=' . $username .
  10. ' challenge=' . $challenge .
  11. ' nonce=' . $nonce);
  12. return false;
  13. }
  14. return password_verify($nonce . $hash, $challenge);
  15. }
  16. public static function getCredentialsFromCookie() {
  17. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  18. if (!ctype_alnum($token)) {
  19. return array();
  20. }
  21. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  22. $mtime = @filemtime($token_file);
  23. $limits = FreshRSS_Context::$system_conf->limits;
  24. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  25. if ($mtime + $cookie_duration < time()) {
  26. // Token has expired (> cookie_duration) or does not exist.
  27. @unlink($token_file);
  28. return array();
  29. }
  30. $credentials = @file_get_contents($token_file);
  31. if ($credentials !== false && self::renewCookie($token)) {
  32. return explode("\t", $credentials, 2);
  33. }
  34. return [];
  35. }
  36. private static function renewCookie($token) {
  37. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  38. if (touch($token_file)) {
  39. $limits = FreshRSS_Context::$system_conf->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. public static function makeCookie($username, $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() {
  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() {
  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. }