Auth.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * This class handles all authentication process.
  4. */
  5. class FreshRSS_Auth {
  6. /**
  7. * Determines if user is connected.
  8. */
  9. const DEFAULT_COOKIE_DURATION = 7776000;
  10. private static $login_ok = false;
  11. /**
  12. * This method initializes authentication system.
  13. */
  14. public static function init() {
  15. if (isset($_SESSION['REMOTE_USER']) && $_SESSION['REMOTE_USER'] !== httpAuthUser()) {
  16. //HTTP REMOTE_USER has changed
  17. self::removeAccess();
  18. }
  19. self::$login_ok = Minz_Session::param('loginOk', false);
  20. $current_user = Minz_Session::param('currentUser', '');
  21. if ($current_user === '') {
  22. $conf = Minz_Configuration::get('system');
  23. $current_user = $conf->default_user;
  24. Minz_Session::_params([
  25. 'currentUser' => $current_user,
  26. 'csrf' => false,
  27. ]);
  28. }
  29. if (self::$login_ok) {
  30. self::giveAccess();
  31. } elseif (self::accessControl() && self::giveAccess()) {
  32. FreshRSS_UserDAO::touch();
  33. } else {
  34. // Be sure all accesses are removed!
  35. self::removeAccess();
  36. }
  37. return self::$login_ok;
  38. }
  39. /**
  40. * This method checks if user is allowed to connect.
  41. *
  42. * Required session parameters are also set in this method (such as
  43. * currentUser).
  44. *
  45. * @return boolean true if user can be connected, false else.
  46. */
  47. private static function accessControl() {
  48. FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
  49. $auth_type = FreshRSS_Context::$system_conf->auth_type;
  50. switch ($auth_type) {
  51. case 'form':
  52. $credentials = FreshRSS_FormAuth::getCredentialsFromCookie();
  53. $current_user = '';
  54. if (isset($credentials[1])) {
  55. $current_user = trim($credentials[0]);
  56. Minz_Session::_params([
  57. 'currentUser' => $current_user,
  58. 'passwordHash' => trim($credentials[1]),
  59. 'csrf' => false,
  60. ]);
  61. }
  62. return $current_user != '';
  63. case 'http_auth':
  64. $current_user = httpAuthUser();
  65. if ($current_user == '') {
  66. return false;
  67. }
  68. $login_ok = FreshRSS_UserDAO::exists($current_user);
  69. if (!$login_ok && FreshRSS_Context::$system_conf->http_auth_auto_register) {
  70. $email = null;
  71. if (FreshRSS_Context::$system_conf->http_auth_auto_register_email_field !== '' &&
  72. isset($_SERVER[FreshRSS_Context::$system_conf->http_auth_auto_register_email_field])) {
  73. $email = $_SERVER[FreshRSS_Context::$system_conf->http_auth_auto_register_email_field];
  74. }
  75. $language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::$system_conf->language);
  76. Minz_Translate::init($language);
  77. $login_ok = FreshRSS_user_Controller::createUser($current_user, $email, '', [
  78. 'language' => $language,
  79. ]);
  80. }
  81. if ($login_ok) {
  82. Minz_Session::_params([
  83. 'currentUser' => $current_user,
  84. 'csrf' => false,
  85. ]);
  86. }
  87. return $login_ok;
  88. case 'none':
  89. return true;
  90. default:
  91. // TODO load extension
  92. return false;
  93. }
  94. }
  95. /**
  96. * Gives access to the current user.
  97. */
  98. public static function giveAccess() {
  99. $current_user = Minz_Session::param('currentUser');
  100. $user_conf = get_user_configuration($current_user);
  101. if ($user_conf == null) {
  102. self::$login_ok = false;
  103. return false;
  104. }
  105. $system_conf = Minz_Configuration::get('system');
  106. switch ($system_conf->auth_type) {
  107. case 'form':
  108. self::$login_ok = Minz_Session::param('passwordHash') === $user_conf->passwordHash;
  109. break;
  110. case 'http_auth':
  111. self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
  112. break;
  113. case 'none':
  114. self::$login_ok = true;
  115. break;
  116. default:
  117. // TODO: extensions
  118. self::$login_ok = false;
  119. }
  120. Minz_Session::_params([
  121. 'loginOk' => self::$login_ok,
  122. 'REMOTE_USER' => httpAuthUser(),
  123. ]);
  124. return self::$login_ok;
  125. }
  126. /**
  127. * Returns if current user has access to the given scope.
  128. *
  129. * @param string $scope general (default) or admin
  130. * @return boolean true if user has corresponding access, false else.
  131. */
  132. public static function hasAccess($scope = 'general') {
  133. $systemConfiguration = Minz_Configuration::get('system');
  134. $currentUser = Minz_Session::param('currentUser');
  135. $userConfiguration = get_user_configuration($currentUser);
  136. $isAdmin = $userConfiguration && $userConfiguration->is_admin;
  137. $default_user = $systemConfiguration->default_user;
  138. $ok = self::$login_ok;
  139. switch ($scope) {
  140. case 'general':
  141. break;
  142. case 'admin':
  143. $ok &= $default_user === $currentUser || $isAdmin;
  144. break;
  145. default:
  146. $ok = false;
  147. }
  148. return $ok;
  149. }
  150. /**
  151. * Removes all accesses for the current user.
  152. */
  153. public static function removeAccess() {
  154. self::$login_ok = false;
  155. Minz_Session::_params([
  156. 'loginOk' => false,
  157. 'csrf' => false,
  158. 'REMOTE_USER' => false,
  159. ]);
  160. $system_conf = Minz_Configuration::get('system');
  161. $username = '';
  162. $token_param = Minz_Request::param('token', '');
  163. if ($token_param != '') {
  164. $username = trim(Minz_Request::param('user', ''));
  165. if ($username != '') {
  166. $conf = get_user_configuration($username);
  167. if ($conf == null) {
  168. $username = '';
  169. }
  170. }
  171. }
  172. if ($username == '') {
  173. $username = $system_conf->default_user;
  174. }
  175. Minz_Session::_param('currentUser', $username);
  176. switch ($system_conf->auth_type) {
  177. case 'form':
  178. Minz_Session::_param('passwordHash');
  179. FreshRSS_FormAuth::deleteCookie();
  180. break;
  181. case 'http_auth':
  182. case 'none':
  183. // Nothing to do...
  184. break;
  185. default:
  186. // TODO: extensions
  187. }
  188. }
  189. /**
  190. * Return if authentication is enabled on this instance of FRSS.
  191. */
  192. public static function accessNeedsLogin() {
  193. $conf = Minz_Configuration::get('system');
  194. $auth_type = $conf->auth_type;
  195. return $auth_type !== 'none';
  196. }
  197. /**
  198. * Return if authentication requires a PHP action.
  199. */
  200. public static function accessNeedsAction() {
  201. $conf = Minz_Configuration::get('system');
  202. $auth_type = $conf->auth_type;
  203. return $auth_type === 'form';
  204. }
  205. public static function csrfToken() {
  206. $csrf = Minz_Session::param('csrf');
  207. if ($csrf == '') {
  208. $salt = FreshRSS_Context::$system_conf->salt;
  209. $csrf = sha1($salt . uniqid(mt_rand(), true));
  210. Minz_Session::_param('csrf', $csrf);
  211. }
  212. return $csrf;
  213. }
  214. public static function isCsrfOk($token = null) {
  215. $csrf = Minz_Session::param('csrf');
  216. if ($token === null) {
  217. $token = Minz_Request::fetchPOST('_csrf');
  218. }
  219. return $token != '' && $token === $csrf;
  220. }
  221. }
  222. class FreshRSS_FormAuth {
  223. public static function checkCredentials($username, $hash, $nonce, $challenge) {
  224. if (!FreshRSS_user_Controller::checkUsername($username) ||
  225. !ctype_graph($hash) ||
  226. !ctype_graph($challenge) ||
  227. !ctype_alnum($nonce)) {
  228. Minz_Log::debug('Invalid credential parameters:' .
  229. ' user=' . $username .
  230. ' challenge=' . $challenge .
  231. ' nonce=' . $nonce);
  232. return false;
  233. }
  234. return password_verify($nonce . $hash, $challenge);
  235. }
  236. public static function getCredentialsFromCookie() {
  237. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  238. if (!ctype_alnum($token)) {
  239. return array();
  240. }
  241. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  242. $mtime = @filemtime($token_file);
  243. $conf = Minz_Configuration::get('system');
  244. $limits = $conf->limits;
  245. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  246. if ($mtime + $cookie_duration < time()) {
  247. // Token has expired (> cookie_duration) or does not exist.
  248. @unlink($token_file);
  249. return array();
  250. }
  251. $credentials = @file_get_contents($token_file);
  252. if ($credentials !== false && self::renewCookie($token)) {
  253. return explode("\t", $credentials, 2);
  254. }
  255. return [];
  256. }
  257. private static function renewCookie($token) {
  258. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  259. if (touch($token_file)) {
  260. $conf = Minz_Configuration::get('system');
  261. $limits = $conf->limits;
  262. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  263. $expire = time() + $cookie_duration;
  264. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  265. return $token;
  266. }
  267. return false;
  268. }
  269. public static function makeCookie($username, $password_hash) {
  270. $conf = Minz_Configuration::get('system');
  271. do {
  272. $token = sha1($conf->salt . $username . uniqid(mt_rand(), true));
  273. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  274. } while (file_exists($token_file));
  275. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  276. return false;
  277. }
  278. return self::renewCookie($token);
  279. }
  280. public static function deleteCookie() {
  281. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  282. if (ctype_alnum($token)) {
  283. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  284. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  285. }
  286. if (rand(0, 10) === 1) {
  287. self::purgeTokens();
  288. }
  289. }
  290. public static function purgeTokens() {
  291. $conf = Minz_Configuration::get('system');
  292. $limits = $conf->limits;
  293. $cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
  294. $oldest = time() - $cookie_duration;
  295. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  296. $extension = $file_info->getExtension();
  297. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  298. @unlink($file_info->getPathname());
  299. }
  300. }
  301. }
  302. }