Auth.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * This class handles all authentication process.
  4. */
  5. class FreshRSS_Auth {
  6. /**
  7. * Determines if user is connected.
  8. */
  9. private static $login_ok = false;
  10. /**
  11. * This method initializes authentication system.
  12. */
  13. public static function init() {
  14. self::$login_ok = Minz_Session::param('loginOk', false);
  15. $current_user = Minz_Session::param('currentUser', '');
  16. if ($current_user === '') {
  17. $current_user = Minz_Configuration::defaultUser();
  18. Minz_Session::_param('currentUser', $current_user);
  19. }
  20. $access_ok = self::accessControl($current_user);
  21. if ($access_ok) {
  22. self::giveAccess();
  23. } else {
  24. // Be sure all accesses are removed!
  25. self::removeAccess();
  26. }
  27. }
  28. /**
  29. * This method checks if user is allowed to connect.
  30. *
  31. * Required session parameters are also set in this method (such as
  32. * currentUser).
  33. *
  34. * @param string $username username of the user to check access.
  35. * @return boolean true if user can be connected, false else.
  36. */
  37. public static function accessControl($username) {
  38. if (self::$login_ok) {
  39. return true;
  40. }
  41. switch (Minz_Configuration::authType()) {
  42. case 'form':
  43. $credentials = FreshRSS_FormAuth::getCredentialsFromCookie();
  44. $current_user = '';
  45. if (isset($credentials[1])) {
  46. $current_user = trim($credentials[0]);
  47. Minz_Session::_param('currentUser', $current_user);
  48. Minz_Session::_param('passwordHash', trim($credentials[1]));
  49. }
  50. return $current_user != '';
  51. case 'http_auth':
  52. $current_user = httpAuthUser();
  53. $login_ok = $current_user != '';
  54. if ($login_ok) {
  55. Minz_Session::_param('currentUser', $current_user);
  56. }
  57. return $login_ok;
  58. case 'none':
  59. return true;
  60. default:
  61. // TODO load extension
  62. return false;
  63. }
  64. }
  65. /**
  66. * Gives access to the current user.
  67. */
  68. public static function giveAccess() {
  69. $current_user = Minz_Session::param('currentUser');
  70. try {
  71. $conf = new FreshRSS_Configuration($current_user);
  72. } catch(Minz_Exception $e) {
  73. die($e->getMessage());
  74. }
  75. switch (Minz_Configuration::authType()) {
  76. case 'form':
  77. self::$login_ok = Minz_Session::param('passwordHash') === $conf->passwordHash;
  78. break;
  79. case 'http_auth':
  80. self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
  81. break;
  82. case 'none':
  83. self::$login_ok = true;
  84. break;
  85. default:
  86. // TODO: extensions
  87. self::$login_ok = false;
  88. }
  89. Minz_Session::_param('loginOk', self::$login_ok);
  90. }
  91. /**
  92. * Returns if current user is connected.
  93. *
  94. * @return boolean true if user is connected, false else.
  95. */
  96. public static function hasAccess() {
  97. return self::$login_ok;
  98. }
  99. /**
  100. * Removes all accesses for the current user.
  101. */
  102. public static function removeAccess() {
  103. Minz_Session::_param('loginOk');
  104. self::$login_ok = false;
  105. Minz_Session::_param('currentUser', Minz_Configuration::defaultUser());
  106. switch (Minz_Configuration::authType()) {
  107. case 'form':
  108. Minz_Session::_param('passwordHash');
  109. FreshRSS_FormAuth::deleteCookie();
  110. break;
  111. case 'http_auth':
  112. case 'none':
  113. // Nothing to do...
  114. break;
  115. default:
  116. // TODO: extensions
  117. }
  118. }
  119. }
  120. class FreshRSS_FormAuth {
  121. public static function checkCredentials($username, $hash, $nonce, $challenge) {
  122. if (!ctype_alnum($username) ||
  123. !ctype_graph($challenge) ||
  124. !ctype_alnum($nonce)) {
  125. Minz_Log::debug('Invalid credential parameters:' .
  126. ' user=' . $username .
  127. ' challenge=' . $challenge .
  128. ' nonce=' . $nonce);
  129. return false;
  130. }
  131. if (!function_exists('password_verify')) {
  132. include_once(LIB_PATH . '/password_compat.php');
  133. }
  134. return password_verify($nonce . $hash, $challenge);
  135. }
  136. public static function getCredentialsFromCookie() {
  137. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  138. if (!ctype_alnum($token)) {
  139. return array();
  140. }
  141. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  142. $mtime = @filemtime($token_file);
  143. if ($mtime + 2629744 < time()) {
  144. // Token has expired (> 1 month) or does not exist.
  145. // TODO: 1 month -> use a configuration instead
  146. @unlink($token_file);
  147. return array();
  148. }
  149. $credentials = @file_get_contents($token_file);
  150. return $credentials === false ? array() : explode("\t", $credentials, 2);
  151. }
  152. public static function makeCookie($username, $password_hash) {
  153. do {
  154. $token = sha1(Minz_Configuration::salt() . $username . uniqid(mt_rand(), true));
  155. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  156. } while (file_exists($token_file));
  157. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  158. return false;
  159. }
  160. $expire = time() + 2629744; //1 month //TODO: Use a configuration instead
  161. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  162. return $token;
  163. }
  164. public static function deleteCookie() {
  165. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  166. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  167. if (ctype_alnum($token)) {
  168. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  169. }
  170. if (rand(0, 10) === 1) {
  171. self::purgeTokens();
  172. }
  173. }
  174. public static function purgeTokens() {
  175. $oldest = time() - 2629744; // 1 month // TODO: Use a configuration instead
  176. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  177. // $extension = $file_info->getExtension(); doesn't work in PHP < 5.3.7
  178. $extension = pathinfo($file_info->getFilename(), PATHINFO_EXTENSION);
  179. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  180. @unlink($file_info->getPathname());
  181. }
  182. }
  183. }
  184. }