authController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * This controller handles action about authentication.
  4. */
  5. class FreshRSS_auth_Controller extends Minz_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() {
  20. if (!FreshRSS_Auth::hasAccess('admin')) {
  21. Minz_Error::error(403);
  22. }
  23. Minz_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'),
  49. array('c' => 'auth', 'a' => 'index'));
  50. } else {
  51. Minz_Request::bad(_t('feedback.conf.error'),
  52. array('c' => 'auth', 'a' => 'index'));
  53. }
  54. }
  55. }
  56. /**
  57. * This action handles the login page.
  58. *
  59. * It forwards to the correct login page (form) or main page if
  60. * the user is already connected.
  61. */
  62. public function loginAction() {
  63. if (FreshRSS_Auth::hasAccess()) {
  64. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  65. }
  66. $auth_type = FreshRSS_Context::$system_conf->auth_type;
  67. switch ($auth_type) {
  68. case 'form':
  69. Minz_Request::forward(array('c' => 'auth', 'a' => 'formLogin'));
  70. break;
  71. case 'http_auth':
  72. case 'none':
  73. // It should not happened!
  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. */
  94. public function formLoginAction() {
  95. invalidateHttpCache();
  96. $file_mtime = @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js');
  97. Minz_View::appendScript(Minz_Url::display('/scripts/bcrypt.min.js?' . $file_mtime));
  98. $conf = Minz_Configuration::get('system');
  99. $limits = $conf->limits;
  100. $this->view->cookie_days = round($limits['cookie_duration'] / 86400, 1);
  101. if (Minz_Request::isPost()) {
  102. $nonce = Minz_Session::param('nonce');
  103. $username = Minz_Request::param('username', '');
  104. $challenge = Minz_Request::param('challenge', '');
  105. $conf = get_user_configuration($username);
  106. if ($conf == null) {
  107. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  108. return;
  109. }
  110. $ok = FreshRSS_FormAuth::checkCredentials(
  111. $username, $conf->passwordHash, $nonce, $challenge
  112. );
  113. if ($ok) {
  114. // Set session parameter to give access to the user.
  115. Minz_Session::_param('currentUser', $username);
  116. Minz_Session::_param('passwordHash', $conf->passwordHash);
  117. FreshRSS_Auth::giveAccess();
  118. // Set cookie parameter if nedded.
  119. if (Minz_Request::param('keep_logged_in')) {
  120. FreshRSS_FormAuth::makeCookie($username, $conf->passwordHash);
  121. } else {
  122. FreshRSS_FormAuth::deleteCookie();
  123. }
  124. // All is good, go back to the index.
  125. Minz_Request::good(_t('feedback.auth.login.success'),
  126. array('c' => 'index', 'a' => 'index'));
  127. } else {
  128. Minz_Log::warning('Password mismatch for' .
  129. ' user=' . $username .
  130. ', nonce=' . $nonce .
  131. ', c=' . $challenge);
  132. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  133. }
  134. } elseif (FreshRSS_Context::$system_conf->unsafe_autologin_enabled) {
  135. $username = Minz_Request::param('u', '');
  136. $password = Minz_Request::param('p', '');
  137. Minz_Request::_param('p');
  138. if (!$username) {
  139. return;
  140. }
  141. $conf = get_user_configuration($username);
  142. if ($conf == null) {
  143. return;
  144. }
  145. if (!function_exists('password_verify')) {
  146. include_once(LIB_PATH . '/password_compat.php');
  147. }
  148. $s = $conf->passwordHash;
  149. $ok = password_verify($password, $s);
  150. unset($password);
  151. if ($ok) {
  152. Minz_Session::_param('currentUser', $username);
  153. Minz_Session::_param('passwordHash', $s);
  154. FreshRSS_Auth::giveAccess();
  155. Minz_Request::good(_t('feedback.auth.login.success'),
  156. array('c' => 'index', 'a' => 'index'));
  157. } else {
  158. Minz_Log::warning('Unsafe password mismatch for user ' . $username);
  159. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  160. }
  161. }
  162. }
  163. /**
  164. * This action removes all accesses of the current user.
  165. */
  166. public function logoutAction() {
  167. invalidateHttpCache();
  168. FreshRSS_Auth::removeAccess();
  169. Minz_Request::good(_t('feedback.auth.logout.success'),
  170. array('c' => 'index', 'a' => 'index'));
  171. }
  172. /**
  173. * This action gives possibility to a user to create an account.
  174. */
  175. public function registerAction() {
  176. if (max_registrations_reached()) {
  177. Minz_Error::error(403);
  178. }
  179. Minz_View::prependTitle(_t('gen.auth.registration.title') . ' · ');
  180. }
  181. }