FormAuth.php 2.8 KB

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