Auth.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. $conf = Minz_Configuration::get('system');
  18. $current_user = $conf->default_user;
  19. Minz_Session::_param('currentUser', $current_user);
  20. }
  21. if (self::$login_ok) {
  22. self::giveAccess();
  23. } elseif (self::accessControl()) {
  24. self::giveAccess();
  25. FreshRSS_UserDAO::touch($current_user);
  26. } else {
  27. // Be sure all accesses are removed!
  28. self::removeAccess();
  29. }
  30. }
  31. /**
  32. * This method checks if user is allowed to connect.
  33. *
  34. * Required session parameters are also set in this method (such as
  35. * currentUser).
  36. *
  37. * @return boolean true if user can be connected, false else.
  38. */
  39. private static function accessControl() {
  40. $conf = Minz_Configuration::get('system');
  41. $auth_type = $conf->auth_type;
  42. switch ($auth_type) {
  43. case 'form':
  44. $credentials = FreshRSS_FormAuth::getCredentialsFromCookie();
  45. $current_user = '';
  46. if (isset($credentials[1])) {
  47. $current_user = trim($credentials[0]);
  48. Minz_Session::_param('currentUser', $current_user);
  49. Minz_Session::_param('passwordHash', trim($credentials[1]));
  50. }
  51. return $current_user != '';
  52. case 'http_auth':
  53. $current_user = httpAuthUser();
  54. $login_ok = $current_user != '';
  55. if ($login_ok) {
  56. Minz_Session::_param('currentUser', $current_user);
  57. }
  58. return $login_ok;
  59. case 'persona':
  60. $email = filter_var(Minz_Session::param('mail'), FILTER_VALIDATE_EMAIL);
  61. $persona_file = DATA_PATH . '/persona/' . $email . '.txt';
  62. if (($current_user = @file_get_contents($persona_file)) !== false) {
  63. $current_user = trim($current_user);
  64. Minz_Session::_param('currentUser', $current_user);
  65. Minz_Session::_param('mail', $email);
  66. return true;
  67. }
  68. return false;
  69. case 'none':
  70. return true;
  71. default:
  72. // TODO load extension
  73. return false;
  74. }
  75. }
  76. /**
  77. * Gives access to the current user.
  78. */
  79. public static function giveAccess() {
  80. $current_user = Minz_Session::param('currentUser');
  81. $user_conf = get_user_configuration($current_user);
  82. $system_conf = Minz_Configuration::get('system');
  83. switch ($system_conf->auth_type) {
  84. case 'form':
  85. self::$login_ok = Minz_Session::param('passwordHash') === $user_conf->passwordHash;
  86. break;
  87. case 'http_auth':
  88. self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
  89. break;
  90. case 'persona':
  91. self::$login_ok = strcasecmp(Minz_Session::param('mail'), $user_conf->mail_login) === 0;
  92. break;
  93. case 'none':
  94. self::$login_ok = true;
  95. break;
  96. default:
  97. // TODO: extensions
  98. self::$login_ok = false;
  99. }
  100. Minz_Session::_param('loginOk', self::$login_ok);
  101. }
  102. /**
  103. * Returns if current user has access to the given scope.
  104. *
  105. * @param string $scope general (default) or admin
  106. * @return boolean true if user has corresponding access, false else.
  107. */
  108. public static function hasAccess($scope = 'general') {
  109. $conf = Minz_Configuration::get('system');
  110. $default_user = $conf->default_user;
  111. $ok = self::$login_ok;
  112. switch ($scope) {
  113. case 'general':
  114. break;
  115. case 'admin':
  116. $ok &= Minz_Session::param('currentUser') === $default_user;
  117. break;
  118. default:
  119. $ok = false;
  120. }
  121. return $ok;
  122. }
  123. /**
  124. * Removes all accesses for the current user.
  125. */
  126. public static function removeAccess() {
  127. Minz_Session::_param('loginOk');
  128. self::$login_ok = false;
  129. $conf = Minz_Configuration::get('system');
  130. Minz_Session::_param('currentUser', $conf->default_user);
  131. switch ($conf->auth_type) {
  132. case 'form':
  133. Minz_Session::_param('passwordHash');
  134. FreshRSS_FormAuth::deleteCookie();
  135. break;
  136. case 'persona':
  137. Minz_Session::_param('mail');
  138. break;
  139. case 'http_auth':
  140. case 'none':
  141. // Nothing to do...
  142. break;
  143. default:
  144. // TODO: extensions
  145. }
  146. }
  147. /**
  148. * Return if authentication is enabled on this instance of FRSS.
  149. */
  150. public static function accessNeedsLogin() {
  151. $conf = Minz_Configuration::get('system');
  152. $auth_type = $conf->auth_type;
  153. return $auth_type !== 'none';
  154. }
  155. /**
  156. * Return if authentication requires a PHP action.
  157. */
  158. public static function accessNeedsAction() {
  159. $conf = Minz_Configuration::get('system');
  160. $auth_type = $conf->auth_type;
  161. return $auth_type === 'form' || $auth_type === 'persona';
  162. }
  163. }
  164. class FreshRSS_FormAuth {
  165. public static function checkCredentials($username, $hash, $nonce, $challenge) {
  166. if (!ctype_alnum($username) ||
  167. !ctype_graph($challenge) ||
  168. !ctype_alnum($nonce)) {
  169. Minz_Log::debug('Invalid credential parameters:' .
  170. ' user=' . $username .
  171. ' challenge=' . $challenge .
  172. ' nonce=' . $nonce);
  173. return false;
  174. }
  175. if (!function_exists('password_verify')) {
  176. include_once(LIB_PATH . '/password_compat.php');
  177. }
  178. return password_verify($nonce . $hash, $challenge);
  179. }
  180. public static function getCredentialsFromCookie() {
  181. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  182. if (!ctype_alnum($token)) {
  183. return array();
  184. }
  185. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  186. $mtime = @filemtime($token_file);
  187. if ($mtime + 2629744 < time()) {
  188. // Token has expired (> 1 month) or does not exist.
  189. // TODO: 1 month -> use a configuration instead
  190. @unlink($token_file);
  191. return array();
  192. }
  193. $credentials = @file_get_contents($token_file);
  194. return $credentials === false ? array() : explode("\t", $credentials, 2);
  195. }
  196. public static function makeCookie($username, $password_hash) {
  197. do {
  198. $conf = Minz_Configuration::get('system');
  199. $token = sha1($conf->salt . $username . uniqid(mt_rand(), true));
  200. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  201. } while (file_exists($token_file));
  202. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  203. return false;
  204. }
  205. $expire = time() + 2629744; //1 month //TODO: Use a configuration instead
  206. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  207. return $token;
  208. }
  209. public static function deleteCookie() {
  210. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  211. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  212. if (ctype_alnum($token)) {
  213. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  214. }
  215. if (rand(0, 10) === 1) {
  216. self::purgeTokens();
  217. }
  218. }
  219. public static function purgeTokens() {
  220. $oldest = time() - 2629744; // 1 month // TODO: Use a configuration instead
  221. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  222. // $extension = $file_info->getExtension(); doesn't work in PHP < 5.3.7
  223. $extension = pathinfo($file_info->getFilename(), PATHINFO_EXTENSION);
  224. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  225. @unlink($file_info->getPathname());
  226. }
  227. }
  228. }
  229. }