FormAuth.php 3.0 KB

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