Auth.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. $current_user = FreshRSS_Context::$system_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. $auth_type = FreshRSS_Context::$system_conf->auth_type;
  48. switch ($auth_type) {
  49. case 'form':
  50. $credentials = FreshRSS_FormAuth::getCredentialsFromCookie();
  51. $current_user = '';
  52. if (isset($credentials[1])) {
  53. $current_user = trim($credentials[0]);
  54. Minz_Session::_params([
  55. 'currentUser' => $current_user,
  56. 'passwordHash' => trim($credentials[1]),
  57. 'csrf' => false,
  58. ]);
  59. }
  60. return $current_user != '';
  61. case 'http_auth':
  62. $current_user = httpAuthUser();
  63. if ($current_user == '') {
  64. return false;
  65. }
  66. $login_ok = FreshRSS_UserDAO::exists($current_user);
  67. if (!$login_ok && FreshRSS_Context::$system_conf->http_auth_auto_register) {
  68. $email = null;
  69. if (FreshRSS_Context::$system_conf->http_auth_auto_register_email_field !== '' &&
  70. isset($_SERVER[FreshRSS_Context::$system_conf->http_auth_auto_register_email_field])) {
  71. $email = $_SERVER[FreshRSS_Context::$system_conf->http_auth_auto_register_email_field];
  72. }
  73. $language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::$system_conf->language);
  74. Minz_Translate::init($language);
  75. $login_ok = FreshRSS_user_Controller::createUser($current_user, $email, '', [
  76. 'language' => $language,
  77. ]);
  78. }
  79. if ($login_ok) {
  80. Minz_Session::_params([
  81. 'currentUser' => $current_user,
  82. 'csrf' => false,
  83. ]);
  84. }
  85. return $login_ok;
  86. case 'none':
  87. return true;
  88. default:
  89. // TODO load extension
  90. return false;
  91. }
  92. }
  93. /**
  94. * Gives access to the current user.
  95. */
  96. public static function giveAccess() {
  97. FreshRSS_Context::initUser();
  98. if (FreshRSS_Context::$user_conf == null) {
  99. self::$login_ok = false;
  100. return false;
  101. }
  102. switch (FreshRSS_Context::$system_conf->auth_type) {
  103. case 'form':
  104. self::$login_ok = Minz_Session::param('passwordHash') === FreshRSS_Context::$user_conf->passwordHash;
  105. break;
  106. case 'http_auth':
  107. $current_user = Minz_Session::param('currentUser');
  108. self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
  109. break;
  110. case 'none':
  111. self::$login_ok = true;
  112. break;
  113. default:
  114. // TODO: extensions
  115. self::$login_ok = false;
  116. }
  117. Minz_Session::_params([
  118. 'loginOk' => self::$login_ok,
  119. 'REMOTE_USER' => httpAuthUser(),
  120. ]);
  121. return self::$login_ok;
  122. }
  123. /**
  124. * Returns if current user has access to the given scope.
  125. *
  126. * @param string $scope general (default) or admin
  127. * @return boolean true if user has corresponding access, false else.
  128. */
  129. public static function hasAccess($scope = 'general') {
  130. if (FreshRSS_Context::$user_conf == null) {
  131. return false;
  132. }
  133. $currentUser = Minz_Session::param('currentUser');
  134. $isAdmin = FreshRSS_Context::$user_conf->is_admin;
  135. $default_user = FreshRSS_Context::$system_conf->default_user;
  136. $ok = self::$login_ok;
  137. switch ($scope) {
  138. case 'general':
  139. break;
  140. case 'admin':
  141. $ok &= $default_user === $currentUser || $isAdmin;
  142. break;
  143. default:
  144. $ok = false;
  145. }
  146. return $ok;
  147. }
  148. /**
  149. * Removes all accesses for the current user.
  150. */
  151. public static function removeAccess() {
  152. self::$login_ok = false;
  153. Minz_Session::_params([
  154. 'loginOk' => false,
  155. 'csrf' => false,
  156. 'REMOTE_USER' => false,
  157. ]);
  158. $username = '';
  159. $token_param = Minz_Request::param('token', '');
  160. if ($token_param != '') {
  161. $username = trim(Minz_Request::param('user', ''));
  162. if ($username != '') {
  163. $conf = get_user_configuration($username);
  164. if ($conf == null) {
  165. $username = '';
  166. }
  167. }
  168. }
  169. if ($username == '') {
  170. $username = FreshRSS_Context::$system_conf->default_user;
  171. }
  172. Minz_Session::_param('currentUser', $username);
  173. switch (FreshRSS_Context::$system_conf->auth_type) {
  174. case 'form':
  175. Minz_Session::_param('passwordHash');
  176. FreshRSS_FormAuth::deleteCookie();
  177. break;
  178. case 'http_auth':
  179. case 'none':
  180. // Nothing to do…
  181. break;
  182. default:
  183. // TODO: extensions
  184. }
  185. }
  186. /**
  187. * Return if authentication is enabled on this instance of FRSS.
  188. */
  189. public static function accessNeedsLogin() {
  190. return FreshRSS_Context::$system_conf->auth_type !== 'none';
  191. }
  192. /**
  193. * Return if authentication requires a PHP action.
  194. */
  195. public static function accessNeedsAction() {
  196. return FreshRSS_Context::$system_conf->auth_type === 'form';
  197. }
  198. public static function csrfToken() {
  199. $csrf = Minz_Session::param('csrf');
  200. if ($csrf == '') {
  201. $salt = FreshRSS_Context::$system_conf->salt;
  202. $csrf = sha1($salt . uniqid('' . mt_rand(), true));
  203. Minz_Session::_param('csrf', $csrf);
  204. }
  205. return $csrf;
  206. }
  207. public static function isCsrfOk($token = null) {
  208. $csrf = Minz_Session::param('csrf');
  209. if ($token === null) {
  210. $token = $_POST['_csrf'] ?? '';
  211. }
  212. return $token != '' && $token === $csrf;
  213. }
  214. }