4
0

authController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This controller handles action about authentication.
  5. */
  6. class FreshRSS_auth_Controller extends FreshRSS_ActionController {
  7. /**
  8. * This action handles authentication management page.
  9. *
  10. * Parameters are:
  11. * - token (default: current token)
  12. * - anon_access (default: false)
  13. * - anon_refresh (default: false)
  14. * - auth_type (default: none)
  15. * - unsafe_autologin (default: false)
  16. * - api_enabled (default: false)
  17. */
  18. public function indexAction(): void {
  19. if (!FreshRSS_Auth::hasAccess('admin')) {
  20. Minz_Error::error(403);
  21. }
  22. FreshRSS_View::prependTitle(_t('admin.auth.title') . ' · ');
  23. if (Minz_Request::isPost()) {
  24. $ok = true;
  25. $anon = Minz_Request::paramBoolean('anon_access');
  26. $anon_refresh = Minz_Request::paramBoolean('anon_refresh');
  27. $auth_type = Minz_Request::paramString('auth_type') ?: 'form';
  28. $unsafe_autologin = Minz_Request::paramBoolean('unsafe_autologin');
  29. $api_enabled = Minz_Request::paramBoolean('api_enabled');
  30. if ($anon !== FreshRSS_Context::systemConf()->allow_anonymous ||
  31. $auth_type !== FreshRSS_Context::systemConf()->auth_type ||
  32. $anon_refresh !== FreshRSS_Context::systemConf()->allow_anonymous_refresh ||
  33. $unsafe_autologin !== FreshRSS_Context::systemConf()->unsafe_autologin_enabled ||
  34. $api_enabled !== FreshRSS_Context::systemConf()->api_enabled) {
  35. if (in_array($auth_type, ['form', 'http_auth', 'none'], true)) {
  36. FreshRSS_Context::systemConf()->auth_type = $auth_type;
  37. } else {
  38. FreshRSS_Context::systemConf()->auth_type = 'form';
  39. }
  40. FreshRSS_Context::systemConf()->allow_anonymous = $anon;
  41. FreshRSS_Context::systemConf()->allow_anonymous_refresh = $anon_refresh;
  42. FreshRSS_Context::systemConf()->unsafe_autologin_enabled = $unsafe_autologin;
  43. FreshRSS_Context::systemConf()->api_enabled = $api_enabled;
  44. $ok &= FreshRSS_Context::systemConf()->save();
  45. }
  46. invalidateHttpCache();
  47. if ($ok) {
  48. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'auth', 'a' => 'index' ]);
  49. } else {
  50. Minz_Request::bad(_t('feedback.conf.error'), [ 'c' => 'auth', 'a' => 'index' ]);
  51. }
  52. }
  53. }
  54. /**
  55. * This action handles the login page.
  56. *
  57. * It forwards to the correct login page (form) or main page if
  58. * the user is already connected.
  59. */
  60. public function loginAction(): void {
  61. if (FreshRSS_Auth::hasAccess() && Minz_Request::paramString('u') === '') {
  62. Minz_Request::forward(['c' => 'index', 'a' => 'index'], true);
  63. }
  64. $auth_type = FreshRSS_Context::systemConf()->auth_type;
  65. FreshRSS_Context::initUser(Minz_User::INTERNAL_USER, false);
  66. match ($auth_type) {
  67. 'form' => Minz_Request::forward(['c' => 'auth', 'a' => 'formLogin']),
  68. 'http_auth' => Minz_Error::error(403, [
  69. 'error' => [
  70. _t('feedback.access.denied'),
  71. ' [HTTP Remote-User=' . htmlspecialchars(httpAuthUser(false), ENT_NOQUOTES, 'UTF-8') .
  72. ' ; Remote IP address=' . connectionRemoteAddress() . ']'
  73. ]
  74. ], false),
  75. 'none' => Minz_Error::error(404), // It should not happen!
  76. default => Minz_Error::error(404), // TODO load plugin instead
  77. };
  78. }
  79. /**
  80. * This action handles form login page.
  81. *
  82. * If this action is reached through a POST request, username and password
  83. * are compared to login the current user.
  84. *
  85. * Parameters are:
  86. * - nonce (default: false)
  87. * - username (default: '')
  88. * - challenge (default: '')
  89. * - keep_logged_in (default: false)
  90. *
  91. * @todo move unsafe autologin in an extension.
  92. * @throws Exception
  93. */
  94. public function formLoginAction(): void {
  95. invalidateHttpCache();
  96. FreshRSS_View::prependTitle(_t('gen.auth.login') . ' · ');
  97. FreshRSS_View::appendScript(Minz_Url::display('/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')));
  98. $limits = FreshRSS_Context::systemConf()->limits;
  99. $this->view->cookie_days = (int)round($limits['cookie_duration'] / 86400, 1);
  100. $isPOST = Minz_Request::isPost() && !Minz_Session::paramBoolean('POST_to_GET');
  101. Minz_Session::_param('POST_to_GET');
  102. if ($isPOST) {
  103. $nonce = Minz_Session::paramString('nonce');
  104. $username = Minz_Request::paramString('username');
  105. $challenge = Minz_Request::paramString('challenge');
  106. if ($nonce === '') {
  107. Minz_Log::warning("Invalid session during login for user={$username}, nonce={$nonce}");
  108. header('HTTP/1.1 403 Forbidden');
  109. Minz_Session::_param('POST_to_GET', true); //Prevent infinite internal redirect
  110. Minz_Request::setBadNotification(_t('install.session.nok'));
  111. Minz_Request::forward(['c' => 'auth', 'a' => 'login'], false);
  112. return;
  113. }
  114. usleep(random_int(100, 10000)); //Primitive mitigation of timing attacks, in μs
  115. FreshRSS_Context::initUser($username);
  116. if (!FreshRSS_Context::hasUserConf()) {
  117. // Initialise the default user to be able to display the error page
  118. FreshRSS_Context::initUser(FreshRSS_Context::systemConf()->default_user);
  119. Minz_Error::error(403, _t('feedback.auth.login.invalid'), false);
  120. return;
  121. }
  122. if (!FreshRSS_Context::userConf()->enabled || FreshRSS_Context::userConf()->passwordHash == '') {
  123. usleep(random_int(100, 5000)); //Primitive mitigation of timing attacks, in μs
  124. Minz_Error::error(403, _t('feedback.auth.login.invalid'), false);
  125. return;
  126. }
  127. $ok = FreshRSS_FormAuth::checkCredentials(
  128. $username, FreshRSS_Context::userConf()->passwordHash, $nonce, $challenge
  129. );
  130. if ($ok) {
  131. // Set session parameter to give access to the user.
  132. Minz_Session::_params([
  133. Minz_User::CURRENT_USER => $username,
  134. 'passwordHash' => FreshRSS_Context::userConf()->passwordHash,
  135. 'csrf' => false,
  136. ]);
  137. FreshRSS_Auth::giveAccess();
  138. // Set cookie parameter if needed.
  139. if (Minz_Request::paramBoolean('keep_logged_in')) {
  140. FreshRSS_FormAuth::makeCookie($username, FreshRSS_Context::userConf()->passwordHash);
  141. } else {
  142. FreshRSS_FormAuth::deleteCookie();
  143. }
  144. Minz_Translate::init(FreshRSS_Context::userConf()->language);
  145. // All is good, go back to the original request or the index.
  146. $url = Minz_Url::unserialize(Minz_Request::paramString('original_request'));
  147. if (empty($url)) {
  148. $url = [ 'c' => 'index', 'a' => 'index' ];
  149. }
  150. Minz_Request::good(_t('feedback.auth.login.success'), $url);
  151. } else {
  152. Minz_Log::warning("Password mismatch for user={$username}, nonce={$nonce}, c={$challenge}");
  153. header('HTTP/1.1 403 Forbidden');
  154. Minz_Session::_param('POST_to_GET', true); //Prevent infinite internal redirect
  155. Minz_Request::setBadNotification(_t('feedback.auth.login.invalid'));
  156. Minz_Request::forward(['c' => 'auth', 'a' => 'login'], false);
  157. }
  158. } elseif (FreshRSS_Context::systemConf()->unsafe_autologin_enabled) {
  159. $username = Minz_Request::paramString('u', plaintext: true);
  160. $password = Minz_Request::paramString('p', plaintext: true);
  161. Minz_Request::_param('p');
  162. if ($username === '') {
  163. return;
  164. }
  165. FreshRSS_FormAuth::deleteCookie();
  166. FreshRSS_Context::initUser($username);
  167. if (!FreshRSS_Context::hasUserConf()) {
  168. return;
  169. }
  170. $s = FreshRSS_Context::userConf()->passwordHash;
  171. $ok = password_verify($password, $s);
  172. unset($password);
  173. if ($ok) {
  174. Minz_Session::_params([
  175. Minz_User::CURRENT_USER => $username,
  176. 'passwordHash' => $s,
  177. 'csrf' => false,
  178. ]);
  179. FreshRSS_Auth::giveAccess();
  180. Minz_Translate::init(FreshRSS_Context::userConf()->language);
  181. Minz_Request::good(_t('feedback.auth.login.success'), ['c' => 'index', 'a' => 'index']);
  182. } else {
  183. Minz_Log::warning('Unsafe password mismatch for user ' . $username);
  184. Minz_Request::bad(
  185. _t('feedback.auth.login.invalid'),
  186. ['c' => 'auth', 'a' => 'login']
  187. );
  188. }
  189. }
  190. }
  191. /**
  192. * This action removes all accesses of the current user.
  193. */
  194. public function logoutAction(): void {
  195. invalidateHttpCache();
  196. FreshRSS_Auth::removeAccess();
  197. Minz_Request::good(_t('feedback.auth.logout.success'), [ 'c' => 'index', 'a' => 'index' ]);
  198. }
  199. /**
  200. * This action gives possibility to a user to create an account.
  201. *
  202. * The user is redirected to the home when logged in.
  203. *
  204. * A 403 is sent if max number of registrations is reached.
  205. */
  206. public function registerAction(): void {
  207. if (FreshRSS_Auth::hasAccess()) {
  208. Minz_Request::forward(['c' => 'index', 'a' => 'index'], true);
  209. }
  210. if (max_registrations_reached()) {
  211. Minz_Error::error(403);
  212. }
  213. $this->view->show_tos_checkbox = file_exists(TOS_FILENAME);
  214. $this->view->show_email_field = FreshRSS_Context::systemConf()->force_email_validation;
  215. $this->view->preferred_language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::systemConf()->language);
  216. FreshRSS_View::prependTitle(_t('gen.auth.registration.title') . ' · ');
  217. }
  218. public static function getLogoutUrl(): string {
  219. if (($_SERVER['AUTH_TYPE'] ?? '') === 'openid-connect') {
  220. $url_string = urlencode(Minz_Request::guessBaseUrl());
  221. return './oidc/?logout=' . $url_string . '/';
  222. # The trailing slash is necessary so that we don’t redirect to http://.
  223. # https://bz.apache.org/bugzilla/show_bug.cgi?id=61355#c13
  224. } else {
  225. return _url('auth', 'logout');
  226. }
  227. }
  228. }