Auth.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 = 7_776_000;
  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'] !== FreshRSS_http_Util::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 && self::giveAccess()) {
  30. return self::$login_ok;
  31. }
  32. if (self::accessControl() && self::giveAccess()) {
  33. FreshRSS_UserDAO::touch();
  34. return self::$login_ok;
  35. }
  36. // Be sure all accesses are removed!
  37. self::removeAccess();
  38. return false;
  39. }
  40. /**
  41. * This method checks if user is allowed to connect.
  42. *
  43. * Required session parameters are also set in this method (such as
  44. * currentUser).
  45. *
  46. * @return bool true if user can be connected, false otherwise.
  47. */
  48. private static function accessControl(): bool {
  49. $auth_type = FreshRSS_Context::systemConf()->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. Minz_User::CURRENT_USER => $current_user,
  58. 'passwordHash' => trim($credentials[1]),
  59. 'csrf' => false,
  60. ]);
  61. }
  62. return $current_user != '';
  63. case 'http_auth':
  64. $current_user = FreshRSS_http_Util::httpAuthUser();
  65. if ($current_user == '') {
  66. return false;
  67. }
  68. $login_ok = FreshRSS_UserDAO::exists($current_user);
  69. if (!$login_ok && FreshRSS_Context::systemConf()->http_auth_auto_register) {
  70. $email = null;
  71. if (FreshRSS_Context::systemConf()->http_auth_auto_register_email_field !== '' &&
  72. is_string($_SERVER[FreshRSS_Context::systemConf()->http_auth_auto_register_email_field] ?? null)) {
  73. $email = $_SERVER[FreshRSS_Context::systemConf()->http_auth_auto_register_email_field];
  74. }
  75. $language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::systemConf()->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. Minz_User::CURRENT_USER => $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(): bool {
  99. FreshRSS_Context::initUser();
  100. if (!FreshRSS_Context::hasUserConf() || !FreshRSS_Context::userConf()->enabled) {
  101. self::$login_ok = false;
  102. return false;
  103. }
  104. switch (FreshRSS_Context::systemConf()->auth_type) {
  105. case 'form':
  106. self::$login_ok = Minz_Session::paramString('passwordHash') === FreshRSS_Context::userConf()->passwordHash;
  107. break;
  108. case 'http_auth':
  109. $current_user = Minz_User::name() ?? '';
  110. self::$login_ok = strcasecmp($current_user, FreshRSS_http_Util::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' => FreshRSS_http_Util::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 bool true if user has corresponding access, false else.
  130. */
  131. public static function hasAccess(string $scope = 'general'): bool {
  132. if (!FreshRSS_Context::hasUserConf()) {
  133. return false;
  134. }
  135. $currentUser = Minz_User::name();
  136. $isAdmin = FreshRSS_Context::userConf()->is_admin;
  137. $default_user = FreshRSS_Context::systemConf()->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 (bool)$ok;
  149. }
  150. /**
  151. * Removes all accesses for the current user.
  152. */
  153. public static function removeAccess(): void {
  154. self::$login_ok = false;
  155. Minz_Session::_params([
  156. 'loginOk' => false,
  157. 'lastReauth' => false,
  158. 'csrf' => false,
  159. 'REMOTE_USER' => false,
  160. ]);
  161. $username = Minz_Request::paramString('user');
  162. if (!Minz_Request::tokenIsOk()) {
  163. $username = FreshRSS_Context::systemConf()->default_user;
  164. }
  165. Minz_User::change($username);
  166. switch (FreshRSS_Context::systemConf()->auth_type) {
  167. case 'form':
  168. Minz_Session::_param('passwordHash');
  169. FreshRSS_FormAuth::deleteCookie();
  170. break;
  171. case 'http_auth':
  172. case 'none':
  173. // Nothing to do…
  174. break;
  175. default:
  176. // TODO: extensions
  177. }
  178. }
  179. /**
  180. * Return if authentication is enabled on this instance of FRSS.
  181. */
  182. public static function accessNeedsLogin(): bool {
  183. return FreshRSS_Context::systemConf()->auth_type !== 'none';
  184. }
  185. /**
  186. * Return if authentication requires a PHP action.
  187. */
  188. public static function accessNeedsAction(): bool {
  189. return FreshRSS_Context::systemConf()->auth_type === 'form';
  190. }
  191. public static function csrfToken(): string {
  192. $csrf = Minz_Session::paramString('csrf');
  193. if ($csrf == '') {
  194. $csrf = hash('sha256', FreshRSS_Context::systemConf()->salt . random_bytes(32));
  195. Minz_Session::_param('csrf', $csrf);
  196. }
  197. return $csrf;
  198. }
  199. public static function isCsrfOk(?string $token = null): bool {
  200. $csrf = Minz_Session::paramString('csrf');
  201. if ($token === null) {
  202. $token = $_POST['_csrf'] ?? '';
  203. }
  204. return $token != '' && $token === $csrf;
  205. }
  206. public static function needsReauth(): bool {
  207. $auth_type = FreshRSS_Context::systemConf()->auth_type;
  208. $reauth_required = FreshRSS_Context::systemConf()->reauth_required;
  209. $reauth_time = FreshRSS_Context::systemConf()->reauth_time;
  210. if (!$reauth_required) {
  211. return false;
  212. }
  213. $last_reauth = Minz_Session::paramInt('lastReauth');
  214. if ($auth_type !== 'none' && time() - $last_reauth > $reauth_time) {
  215. if ($auth_type === 'http_auth') {
  216. // TODO: not implemented - just let the user through
  217. return false;
  218. }
  219. return true;
  220. }
  221. return false;
  222. }
  223. /**
  224. * Return if user needs reauth and got redirected to login page.
  225. *
  226. * @param array{c?: string, a?: string, params?: array<string, mixed>}|null $redirect
  227. */
  228. public static function requestReauth(?array $redirect = null): bool {
  229. if (self::needsReauth()) {
  230. if (Minz_Request::paramBoolean('ajax')) {
  231. // Send 403 and exit instead of redirect with Minz_Error::error()
  232. header('HTTP/1.1 403 Forbidden');
  233. exit();
  234. }
  235. $redirect = Minz_Url::serialize($redirect ?? Minz_Request::currentRequest());
  236. Minz_Request::forward([
  237. 'c' => 'auth',
  238. 'a' => 'reauth',
  239. 'params' => [
  240. 'r' => $redirect,
  241. ],
  242. ], true);
  243. return true;
  244. }
  245. return false;
  246. }
  247. }