4
0

authController.php 8.2 KB

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