authController.php 8.2 KB

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