authController.php 8.2 KB

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