Auth.php 8.7 KB

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