Auth.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This class handles all authentication process.
  5. */
  6. class FreshRSS_Auth {
  7. /**
  8. * Determines if user is connected.
  9. */
  10. public const DEFAULT_COOKIE_DURATION = 7776000;
  11. private static bool $login_ok = false;
  12. /**
  13. * This method initializes authentication system.
  14. */
  15. public static function init(): bool {
  16. if (isset($_SESSION['REMOTE_USER']) && $_SESSION['REMOTE_USER'] !== httpAuthUser()) {
  17. //HTTP REMOTE_USER has changed
  18. self::removeAccess();
  19. }
  20. self::$login_ok = Minz_Session::paramBoolean('loginOk');
  21. $current_user = Minz_User::name();
  22. if ($current_user === null) {
  23. $current_user = FreshRSS_Context::systemConf()->default_user;
  24. Minz_Session::_params([
  25. Minz_User::CURRENT_USER => $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 bool true if user can be connected, false otherwise.
  46. */
  47. private static function accessControl(): bool {
  48. $auth_type = FreshRSS_Context::systemConf()->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. Minz_User::CURRENT_USER => $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::systemConf()->http_auth_auto_register) {
  69. $email = null;
  70. if (FreshRSS_Context::systemConf()->http_auth_auto_register_email_field !== '' &&
  71. isset($_SERVER[FreshRSS_Context::systemConf()->http_auth_auto_register_email_field])) {
  72. $email = (string)$_SERVER[FreshRSS_Context::systemConf()->http_auth_auto_register_email_field];
  73. }
  74. $language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::systemConf()->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. Minz_User::CURRENT_USER => $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(): bool {
  98. FreshRSS_Context::initUser();
  99. if (!FreshRSS_Context::hasUserConf()) {
  100. self::$login_ok = false;
  101. return false;
  102. }
  103. switch (FreshRSS_Context::systemConf()->auth_type) {
  104. case 'form':
  105. self::$login_ok = Minz_Session::paramString('passwordHash') === FreshRSS_Context::userConf()->passwordHash;
  106. break;
  107. case 'http_auth':
  108. $current_user = Minz_User::name() ?? '';
  109. self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
  110. break;
  111. case 'none':
  112. self::$login_ok = true;
  113. break;
  114. default:
  115. // TODO: extensions
  116. self::$login_ok = false;
  117. }
  118. Minz_Session::_params([
  119. 'loginOk' => self::$login_ok,
  120. 'REMOTE_USER' => httpAuthUser(),
  121. ]);
  122. return self::$login_ok;
  123. }
  124. /**
  125. * Returns if current user has access to the given scope.
  126. *
  127. * @param string $scope general (default) or admin
  128. * @return bool true if user has corresponding access, false else.
  129. */
  130. public static function hasAccess(string $scope = 'general'): bool {
  131. if (!FreshRSS_Context::hasUserConf()) {
  132. return false;
  133. }
  134. $currentUser = Minz_User::name();
  135. $isAdmin = FreshRSS_Context::userConf()->is_admin;
  136. $default_user = FreshRSS_Context::systemConf()->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 (bool)$ok;
  148. }
  149. /**
  150. * Removes all accesses for the current user.
  151. */
  152. public static function removeAccess(): void {
  153. self::$login_ok = false;
  154. Minz_Session::_params([
  155. 'loginOk' => false,
  156. 'csrf' => false,
  157. 'REMOTE_USER' => false,
  158. ]);
  159. $username = '';
  160. $token_param = Minz_Request::paramString('token');
  161. if ($token_param != '') {
  162. $username = Minz_Request::paramString('user');
  163. if ($username != '') {
  164. $conf = get_user_configuration($username);
  165. if ($conf == null) {
  166. $username = '';
  167. }
  168. }
  169. }
  170. if ($username == '') {
  171. $username = FreshRSS_Context::systemConf()->default_user;
  172. }
  173. Minz_User::change($username);
  174. switch (FreshRSS_Context::systemConf()->auth_type) {
  175. case 'form':
  176. Minz_Session::_param('passwordHash');
  177. FreshRSS_FormAuth::deleteCookie();
  178. break;
  179. case 'http_auth':
  180. case 'none':
  181. // Nothing to do…
  182. break;
  183. default:
  184. // TODO: extensions
  185. }
  186. }
  187. /**
  188. * Return if authentication is enabled on this instance of FRSS.
  189. */
  190. public static function accessNeedsLogin(): bool {
  191. return FreshRSS_Context::systemConf()->auth_type !== 'none';
  192. }
  193. /**
  194. * Return if authentication requires a PHP action.
  195. */
  196. public static function accessNeedsAction(): bool {
  197. return FreshRSS_Context::systemConf()->auth_type === 'form';
  198. }
  199. public static function csrfToken(): string {
  200. $csrf = Minz_Session::paramString('csrf');
  201. if ($csrf == '') {
  202. $salt = FreshRSS_Context::systemConf()->salt;
  203. $csrf = sha1($salt . uniqid('' . random_int(0, mt_getrandmax()), true));
  204. Minz_Session::_param('csrf', $csrf);
  205. }
  206. return $csrf;
  207. }
  208. public static function isCsrfOk(?string $token = null): bool {
  209. $csrf = Minz_Session::paramString('csrf');
  210. if ($token === null) {
  211. $token = $_POST['_csrf'] ?? '';
  212. }
  213. return $token != '' && $token === $csrf;
  214. }
  215. }