authController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. switch ($auth_type) {
  67. case 'form':
  68. Minz_Request::forward(['c' => 'auth', 'a' => 'formLogin']);
  69. break;
  70. case 'http_auth':
  71. Minz_Error::error(403, [
  72. 'error' => [
  73. _t('feedback.access.denied'),
  74. ' [HTTP Remote-User=' . htmlspecialchars(httpAuthUser(false), ENT_NOQUOTES, 'UTF-8') .
  75. ' ; Remote IP address=' . connectionRemoteAddress() . ']'
  76. ]
  77. ], false);
  78. break;
  79. case 'none':
  80. // It should not happen!
  81. Minz_Error::error(404);
  82. default:
  83. // TODO load plugin instead
  84. Minz_Error::error(404);
  85. }
  86. }
  87. /**
  88. * This action handles form login page.
  89. *
  90. * If this action is reached through a POST request, username and password
  91. * are compared to login the current user.
  92. *
  93. * Parameters are:
  94. * - nonce (default: false)
  95. * - username (default: '')
  96. * - challenge (default: '')
  97. * - keep_logged_in (default: false)
  98. *
  99. * @todo move unsafe autologin in an extension.
  100. * @throws Exception
  101. */
  102. public function formLoginAction(): void {
  103. invalidateHttpCache();
  104. FreshRSS_View::prependTitle(_t('gen.auth.login') . ' · ');
  105. FreshRSS_View::appendScript(Minz_Url::display('/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')));
  106. $limits = FreshRSS_Context::systemConf()->limits;
  107. $this->view->cookie_days = (int)round($limits['cookie_duration'] / 86400, 1);
  108. $isPOST = Minz_Request::isPost() && !Minz_Session::paramBoolean('POST_to_GET');
  109. Minz_Session::_param('POST_to_GET');
  110. if ($isPOST) {
  111. $nonce = Minz_Session::paramString('nonce');
  112. $username = Minz_Request::paramString('username');
  113. $challenge = Minz_Request::paramString('challenge');
  114. if ($nonce === '') {
  115. Minz_Log::warning("Invalid session during login for user={$username}, nonce={$nonce}");
  116. header('HTTP/1.1 403 Forbidden');
  117. Minz_Session::_param('POST_to_GET', true); //Prevent infinite internal redirect
  118. Minz_Request::setBadNotification(_t('install.session.nok'));
  119. Minz_Request::forward(['c' => 'auth', 'a' => 'login'], false);
  120. return;
  121. }
  122. usleep(random_int(100, 10000)); //Primitive mitigation of timing attacks, in μs
  123. FreshRSS_Context::initUser($username);
  124. if (!FreshRSS_Context::hasUserConf()) {
  125. // Initialise the default user to be able to display the error page
  126. FreshRSS_Context::initUser(FreshRSS_Context::systemConf()->default_user);
  127. Minz_Error::error(403, _t('feedback.auth.login.invalid'), false);
  128. return;
  129. }
  130. if (!FreshRSS_Context::userConf()->enabled || FreshRSS_Context::userConf()->passwordHash == '') {
  131. usleep(random_int(100, 5000)); //Primitive mitigation of timing attacks, in μs
  132. Minz_Error::error(403, _t('feedback.auth.login.invalid'), false);
  133. return;
  134. }
  135. $ok = FreshRSS_FormAuth::checkCredentials(
  136. $username, FreshRSS_Context::userConf()->passwordHash, $nonce, $challenge
  137. );
  138. if ($ok) {
  139. // Set session parameter to give access to the user.
  140. Minz_Session::_params([
  141. Minz_User::CURRENT_USER => $username,
  142. 'passwordHash' => FreshRSS_Context::userConf()->passwordHash,
  143. 'csrf' => false,
  144. ]);
  145. FreshRSS_Auth::giveAccess();
  146. // Set cookie parameter if needed.
  147. if (Minz_Request::paramBoolean('keep_logged_in')) {
  148. FreshRSS_FormAuth::makeCookie($username, FreshRSS_Context::userConf()->passwordHash);
  149. } else {
  150. FreshRSS_FormAuth::deleteCookie();
  151. }
  152. Minz_Translate::init(FreshRSS_Context::userConf()->language);
  153. // All is good, go back to the original request or the index.
  154. $url = Minz_Url::unserialize(Minz_Request::paramString('original_request'));
  155. if (empty($url)) {
  156. $url = [ 'c' => 'index', 'a' => 'index' ];
  157. }
  158. Minz_Request::good(_t('feedback.auth.login.success'), $url);
  159. } else {
  160. Minz_Log::warning("Password mismatch for user={$username}, nonce={$nonce}, c={$challenge}");
  161. header('HTTP/1.1 403 Forbidden');
  162. Minz_Session::_param('POST_to_GET', true); //Prevent infinite internal redirect
  163. Minz_Request::setBadNotification(_t('feedback.auth.login.invalid'));
  164. Minz_Request::forward(['c' => 'auth', 'a' => 'login'], false);
  165. }
  166. } elseif (FreshRSS_Context::systemConf()->unsafe_autologin_enabled) {
  167. $username = Minz_Request::paramString('u');
  168. $password = Minz_Request::paramString('p');
  169. Minz_Request::_param('p');
  170. if (!$username) {
  171. return;
  172. }
  173. FreshRSS_FormAuth::deleteCookie();
  174. FreshRSS_Context::initUser($username);
  175. if (!FreshRSS_Context::hasUserConf()) {
  176. return;
  177. }
  178. $s = FreshRSS_Context::userConf()->passwordHash;
  179. $ok = password_verify($password, $s);
  180. unset($password);
  181. if ($ok) {
  182. Minz_Session::_params([
  183. Minz_User::CURRENT_USER => $username,
  184. 'passwordHash' => $s,
  185. 'csrf' => false,
  186. ]);
  187. FreshRSS_Auth::giveAccess();
  188. Minz_Translate::init(FreshRSS_Context::userConf()->language);
  189. Minz_Request::good(_t('feedback.auth.login.success'), ['c' => 'index', 'a' => 'index']);
  190. } else {
  191. Minz_Log::warning('Unsafe password mismatch for user ' . $username);
  192. Minz_Request::bad(
  193. _t('feedback.auth.login.invalid'),
  194. ['c' => 'auth', 'a' => 'login']
  195. );
  196. }
  197. }
  198. }
  199. /**
  200. * This action removes all accesses of the current user.
  201. */
  202. public function logoutAction(): void {
  203. invalidateHttpCache();
  204. FreshRSS_Auth::removeAccess();
  205. Minz_Request::good(_t('feedback.auth.logout.success'), [ 'c' => 'index', 'a' => 'index' ]);
  206. }
  207. /**
  208. * This action gives possibility to a user to create an account.
  209. *
  210. * The user is redirected to the home when logged in.
  211. *
  212. * A 403 is sent if max number of registrations is reached.
  213. */
  214. public function registerAction(): void {
  215. if (FreshRSS_Auth::hasAccess()) {
  216. Minz_Request::forward(['c' => 'index', 'a' => 'index'], true);
  217. }
  218. if (max_registrations_reached()) {
  219. Minz_Error::error(403);
  220. }
  221. $this->view->show_tos_checkbox = file_exists(TOS_FILENAME);
  222. $this->view->show_email_field = FreshRSS_Context::systemConf()->force_email_validation;
  223. $this->view->preferred_language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::systemConf()->language);
  224. FreshRSS_View::prependTitle(_t('gen.auth.registration.title') . ' · ');
  225. }
  226. public static function getLogoutUrl(): string {
  227. if (($_SERVER['AUTH_TYPE'] ?? '') === 'openid-connect') {
  228. $url_string = urlencode(Minz_Request::guessBaseUrl());
  229. return './oidc/?logout=' . $url_string . '/';
  230. # The trailing slash is necessary so that we don’t redirect to http://.
  231. # https://bz.apache.org/bugzilla/show_bug.cgi?id=61355#c13
  232. } else {
  233. return _url('auth', 'logout') ?: '';
  234. }
  235. }
  236. }