authController.php 8.1 KB

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