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