authController.php 8.6 KB

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