authController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. $current_token = FreshRSS_Context::$user_conf->token;
  27. $token = Minz_Request::param('token', $current_token);
  28. FreshRSS_Context::$user_conf->token = $token;
  29. $ok &= FreshRSS_Context::$user_conf->save();
  30. $anon = Minz_Request::param('anon_access', false);
  31. $anon = ((bool)$anon) && ($anon !== 'no');
  32. $anon_refresh = Minz_Request::param('anon_refresh', false);
  33. $anon_refresh = ((bool)$anon_refresh) && ($anon_refresh !== 'no');
  34. $auth_type = Minz_Request::param('auth_type', 'none');
  35. $unsafe_autologin = Minz_Request::param('unsafe_autologin', false);
  36. $api_enabled = Minz_Request::param('api_enabled', false);
  37. if ($anon != FreshRSS_Context::$system_conf->allow_anonymous ||
  38. $auth_type != FreshRSS_Context::$system_conf->auth_type ||
  39. $anon_refresh != FreshRSS_Context::$system_conf->allow_anonymous_refresh ||
  40. $unsafe_autologin != FreshRSS_Context::$system_conf->unsafe_autologin_enabled ||
  41. $api_enabled != FreshRSS_Context::$system_conf->api_enabled) {
  42. // TODO: test values from form
  43. FreshRSS_Context::$system_conf->auth_type = $auth_type;
  44. FreshRSS_Context::$system_conf->allow_anonymous = $anon;
  45. FreshRSS_Context::$system_conf->allow_anonymous_refresh = $anon_refresh;
  46. FreshRSS_Context::$system_conf->unsafe_autologin_enabled = $unsafe_autologin;
  47. FreshRSS_Context::$system_conf->api_enabled = $api_enabled;
  48. $ok &= FreshRSS_Context::$system_conf->save();
  49. }
  50. invalidateHttpCache();
  51. if ($ok) {
  52. Minz_Request::good(_t('feedback.conf.updated'),
  53. array('c' => 'auth', 'a' => 'index'));
  54. } else {
  55. Minz_Request::bad(_t('feedback.conf.error'),
  56. array('c' => 'auth', 'a' => 'index'));
  57. }
  58. }
  59. }
  60. /**
  61. * This action handles the login page.
  62. *
  63. * It forwards to the correct login page (form) or main page if
  64. * the user is already connected.
  65. */
  66. public function loginAction() {
  67. if (FreshRSS_Auth::hasAccess()) {
  68. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  69. }
  70. $auth_type = FreshRSS_Context::$system_conf->auth_type;
  71. switch ($auth_type) {
  72. case 'form':
  73. Minz_Request::forward(array('c' => 'auth', 'a' => 'formLogin'));
  74. break;
  75. case 'http_auth':
  76. case 'none':
  77. // It should not happened!
  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. */
  98. public function formLoginAction() {
  99. invalidateHttpCache();
  100. $file_mtime = @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js');
  101. Minz_View::appendScript(Minz_Url::display('/scripts/bcrypt.min.js?' . $file_mtime));
  102. if (Minz_Request::isPost()) {
  103. $nonce = Minz_Session::param('nonce');
  104. $username = Minz_Request::param('username', '');
  105. $challenge = Minz_Request::param('challenge', '');
  106. $conf = get_user_configuration($username);
  107. if (is_null($conf)) {
  108. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  109. return;
  110. }
  111. $ok = FreshRSS_FormAuth::checkCredentials(
  112. $username, $conf->passwordHash, $nonce, $challenge
  113. );
  114. if ($ok) {
  115. // Set session parameter to give access to the user.
  116. Minz_Session::_param('currentUser', $username);
  117. Minz_Session::_param('passwordHash', $conf->passwordHash);
  118. FreshRSS_Auth::giveAccess();
  119. // Set cookie parameter if nedded.
  120. if (Minz_Request::param('keep_logged_in')) {
  121. FreshRSS_FormAuth::makeCookie($username, $conf->passwordHash);
  122. } else {
  123. FreshRSS_FormAuth::deleteCookie();
  124. }
  125. // All is good, go back to the index.
  126. Minz_Request::good(_t('feedback.auth.login.success'),
  127. array('c' => 'index', 'a' => 'index'));
  128. } else {
  129. Minz_Log::warning('Password mismatch for' .
  130. ' user=' . $username .
  131. ', nonce=' . $nonce .
  132. ', c=' . $challenge);
  133. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  134. }
  135. } elseif (FreshRSS_Context::$system_conf->unsafe_autologin_enabled) {
  136. $username = Minz_Request::param('u', '');
  137. $password = Minz_Request::param('p', '');
  138. Minz_Request::_param('p');
  139. if (!$username) {
  140. return;
  141. }
  142. $conf = get_user_configuration($username);
  143. if (is_null($conf)) {
  144. return;
  145. }
  146. if (!function_exists('password_verify')) {
  147. include_once(LIB_PATH . '/password_compat.php');
  148. }
  149. $s = $conf->passwordHash;
  150. $ok = password_verify($password, $s);
  151. unset($password);
  152. if ($ok) {
  153. Minz_Session::_param('currentUser', $username);
  154. Minz_Session::_param('passwordHash', $s);
  155. FreshRSS_Auth::giveAccess();
  156. Minz_Request::good(_t('feedback.auth.login.success'),
  157. array('c' => 'index', 'a' => 'index'));
  158. } else {
  159. Minz_Log::warning('Unsafe password mismatch for user ' . $username);
  160. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  161. }
  162. }
  163. }
  164. /**
  165. * This action removes all accesses of the current user.
  166. */
  167. public function logoutAction() {
  168. invalidateHttpCache();
  169. FreshRSS_Auth::removeAccess();
  170. Minz_Request::good(_t('feedback.auth.logout.success'),
  171. array('c' => 'index', 'a' => 'index'));
  172. }
  173. /**
  174. * This action gives possibility to a user to create an account.
  175. */
  176. public function registerAction() {
  177. if (max_registrations_reached()) {
  178. Minz_Error::error(403);
  179. }
  180. Minz_View::prependTitle(_t('gen.auth.registration.title') . ' · ');
  181. }
  182. }