4
0

Auth.php 8.8 KB

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