Auth.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 has access to the given scope.
  93. *
  94. * @param string $scope general (default) or admin
  95. * @return boolean true if user has corresponding access, false else.
  96. */
  97. public static function hasAccess($scope = 'general') {
  98. $ok = self::$login_ok;
  99. switch ($scope) {
  100. case 'general':
  101. break;
  102. case 'admin':
  103. $ok &= Minz_Session::param('currentUser') === Minz_Configuration::defaultUser();
  104. break;
  105. default:
  106. $ok = false;
  107. }
  108. return $ok;
  109. }
  110. /**
  111. * Removes all accesses for the current user.
  112. */
  113. public static function removeAccess() {
  114. Minz_Session::_param('loginOk');
  115. self::$login_ok = false;
  116. Minz_Session::_param('currentUser', Minz_Configuration::defaultUser());
  117. switch (Minz_Configuration::authType()) {
  118. case 'form':
  119. Minz_Session::_param('passwordHash');
  120. FreshRSS_FormAuth::deleteCookie();
  121. break;
  122. case 'http_auth':
  123. case 'none':
  124. // Nothing to do...
  125. break;
  126. default:
  127. // TODO: extensions
  128. }
  129. }
  130. }
  131. class FreshRSS_FormAuth {
  132. public static function checkCredentials($username, $hash, $nonce, $challenge) {
  133. if (!ctype_alnum($username) ||
  134. !ctype_graph($challenge) ||
  135. !ctype_alnum($nonce)) {
  136. Minz_Log::debug('Invalid credential parameters:' .
  137. ' user=' . $username .
  138. ' challenge=' . $challenge .
  139. ' nonce=' . $nonce);
  140. return false;
  141. }
  142. if (!function_exists('password_verify')) {
  143. include_once(LIB_PATH . '/password_compat.php');
  144. }
  145. return password_verify($nonce . $hash, $challenge);
  146. }
  147. public static function getCredentialsFromCookie() {
  148. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  149. if (!ctype_alnum($token)) {
  150. return array();
  151. }
  152. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  153. $mtime = @filemtime($token_file);
  154. if ($mtime + 2629744 < time()) {
  155. // Token has expired (> 1 month) or does not exist.
  156. // TODO: 1 month -> use a configuration instead
  157. @unlink($token_file);
  158. return array();
  159. }
  160. $credentials = @file_get_contents($token_file);
  161. return $credentials === false ? array() : explode("\t", $credentials, 2);
  162. }
  163. public static function makeCookie($username, $password_hash) {
  164. do {
  165. $token = sha1(Minz_Configuration::salt() . $username . uniqid(mt_rand(), true));
  166. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  167. } while (file_exists($token_file));
  168. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  169. return false;
  170. }
  171. $expire = time() + 2629744; //1 month //TODO: Use a configuration instead
  172. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  173. return $token;
  174. }
  175. public static function deleteCookie() {
  176. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  177. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  178. if (ctype_alnum($token)) {
  179. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  180. }
  181. if (rand(0, 10) === 1) {
  182. self::purgeTokens();
  183. }
  184. }
  185. public static function purgeTokens() {
  186. $oldest = time() - 2629744; // 1 month // TODO: Use a configuration instead
  187. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  188. // $extension = $file_info->getExtension(); doesn't work in PHP < 5.3.7
  189. $extension = pathinfo($file_info->getFilename(), PATHINFO_EXTENSION);
  190. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  191. @unlink($file_info->getPathname());
  192. }
  193. }
  194. }
  195. }