authController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 Persona) 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 'persona':
  76. Minz_Request::forward(array('c' => 'auth', 'a' => 'personaLogin'));
  77. break;
  78. case 'http_auth':
  79. case 'none':
  80. // It should not happened!
  81. Minz_Error::error(404);
  82. default:
  83. // TODO load plugin instead
  84. Minz_Error::error(404);
  85. }
  86. }
  87. /**
  88. * This action handles form login page.
  89. *
  90. * If this action is reached through a POST request, username and password
  91. * are compared to login the current user.
  92. *
  93. * Parameters are:
  94. * - nonce (default: false)
  95. * - username (default: '')
  96. * - challenge (default: '')
  97. * - keep_logged_in (default: false)
  98. *
  99. * @todo move unsafe autologin in an extension.
  100. */
  101. public function formLoginAction() {
  102. invalidateHttpCache();
  103. $file_mtime = @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js');
  104. Minz_View::appendScript(Minz_Url::display('/scripts/bcrypt.min.js?' . $file_mtime));
  105. if (Minz_Request::isPost()) {
  106. $nonce = Minz_Session::param('nonce');
  107. $username = Minz_Request::param('username', '');
  108. $challenge = Minz_Request::param('challenge', '');
  109. $conf = get_user_configuration($username);
  110. if (is_null($conf)) {
  111. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  112. }
  113. $ok = FreshRSS_FormAuth::checkCredentials(
  114. $username, $conf->passwordHash, $nonce, $challenge
  115. );
  116. if ($ok) {
  117. // Set session parameter to give access to the user.
  118. Minz_Session::_param('currentUser', $username);
  119. Minz_Session::_param('passwordHash', $conf->passwordHash);
  120. FreshRSS_Auth::giveAccess();
  121. // Set cookie parameter if nedded.
  122. if (Minz_Request::param('keep_logged_in')) {
  123. FreshRSS_FormAuth::makeCookie($username, $conf->passwordHash);
  124. } else {
  125. FreshRSS_FormAuth::deleteCookie();
  126. }
  127. // All is good, go back to the index.
  128. Minz_Request::good(_t('feedback.auth.login.success'),
  129. array('c' => 'index', 'a' => 'index'));
  130. } else {
  131. Minz_Log::warning('Password mismatch for' .
  132. ' user=' . $username .
  133. ', nonce=' . $nonce .
  134. ', c=' . $challenge);
  135. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  136. }
  137. } elseif (FreshRSS_Context::$system_conf->unsafe_autologin_enabled) {
  138. $username = Minz_Request::param('u', '');
  139. $password = Minz_Request::param('p', '');
  140. Minz_Request::_param('p');
  141. if (!$username) {
  142. return;
  143. }
  144. $conf = get_user_configuration($username);
  145. if (is_null($conf)) {
  146. return;
  147. }
  148. if (!function_exists('password_verify')) {
  149. include_once(LIB_PATH . '/password_compat.php');
  150. }
  151. $s = $conf->passwordHash;
  152. $ok = password_verify($password, $s);
  153. unset($password);
  154. if ($ok) {
  155. Minz_Session::_param('currentUser', $username);
  156. Minz_Session::_param('passwordHash', $s);
  157. FreshRSS_Auth::giveAccess();
  158. Minz_Request::good(_t('feedback.auth.login.success'),
  159. array('c' => 'index', 'a' => 'index'));
  160. } else {
  161. Minz_Log::warning('Unsafe password mismatch for user ' . $username);
  162. Minz_Error::error(403, array(_t('feedback.auth.login.invalid')), false);
  163. }
  164. }
  165. }
  166. /**
  167. * This action handles Persona login page.
  168. *
  169. * If this action is reached through a POST request, assertion from Persona
  170. * is verificated and user connected if all is ok.
  171. *
  172. * Parameter is:
  173. * - assertion (default: false)
  174. *
  175. * @todo: Persona system should be moved to a plugin
  176. */
  177. public function personaLoginAction() {
  178. $this->view->res = false;
  179. if (Minz_Request::isPost()) {
  180. $this->view->_useLayout(false);
  181. $assert = Minz_Request::param('assertion');
  182. $url = 'https://verifier.login.persona.org/verify';
  183. $params = 'assertion=' . $assert . '&audience=' .
  184. urlencode(Minz_Url::display(null, 'php', true));
  185. $ch = curl_init();
  186. $options = array(
  187. CURLOPT_URL => $url,
  188. CURLOPT_RETURNTRANSFER => TRUE,
  189. CURLOPT_POST => 2,
  190. CURLOPT_POSTFIELDS => $params
  191. );
  192. curl_setopt_array($ch, $options);
  193. $result = curl_exec($ch);
  194. curl_close($ch);
  195. $res = json_decode($result, true);
  196. $login_ok = false;
  197. $reason = '';
  198. if ($res['status'] === 'okay') {
  199. $email = filter_var($res['email'], FILTER_VALIDATE_EMAIL);
  200. if ($email != '') {
  201. $persona_file = DATA_PATH . '/persona/' . $email . '.txt';
  202. if (($current_user = @file_get_contents($persona_file)) !== false) {
  203. $current_user = trim($current_user);
  204. $conf = get_user_configuration($current_user);
  205. if (!is_null($conf)) {
  206. $login_ok = strcasecmp($email, $conf->mail_login) === 0;
  207. } else {
  208. $reason = 'Invalid configuration for user ' .
  209. '[' . $current_user . ']';
  210. }
  211. }
  212. } else {
  213. $reason = 'Invalid email format [' . $res['email'] . ']';
  214. }
  215. } else {
  216. $reason = $res['reason'];
  217. }
  218. if ($login_ok) {
  219. Minz_Session::_param('currentUser', $current_user);
  220. Minz_Session::_param('mail', $email);
  221. FreshRSS_Auth::giveAccess();
  222. invalidateHttpCache();
  223. } else {
  224. Minz_Log::warning($reason);
  225. $res = array();
  226. $res['status'] = 'failure';
  227. $res['reason'] = _t('feedback.auth.login.invalid');
  228. }
  229. header('Content-Type: application/json; charset=UTF-8');
  230. $this->view->res = $res;
  231. }
  232. }
  233. /**
  234. * This action removes all accesses of the current user.
  235. */
  236. public function logoutAction() {
  237. invalidateHttpCache();
  238. FreshRSS_Auth::removeAccess();
  239. Minz_Request::good(_t('feedback.auth.logout.success'),
  240. array('c' => 'index', 'a' => 'index'));
  241. }
  242. /**
  243. * This action resets the authentication system.
  244. *
  245. * After reseting, form auth is set by default.
  246. */
  247. public function resetAction() {
  248. Minz_View::prependTitle(_t('admin.auth.title_reset') . ' · ');
  249. Minz_View::appendScript(Minz_Url::display(
  250. '/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')
  251. ));
  252. $this->view->no_form = false;
  253. // Enable changement of auth only if Persona!
  254. if (FreshRSS_Context::$system_conf->auth_type != 'persona') {
  255. $this->view->message = array(
  256. 'status' => 'bad',
  257. 'title' => _t('gen.short.damn'),
  258. 'body' => _t('feedback.auth.not_persona')
  259. );
  260. $this->view->no_form = true;
  261. return;
  262. }
  263. $conf = get_user_configuration(FreshRSS_Context::$system_conf->default_user);
  264. if (is_null($conf)) {
  265. return;
  266. }
  267. // Admin user must have set its master password.
  268. if (!$conf->passwordHash) {
  269. $this->view->message = array(
  270. 'status' => 'bad',
  271. 'title' => _t('gen.short.damn'),
  272. 'body' => _t('feedback.auth.no_password_set')
  273. );
  274. $this->view->no_form = true;
  275. return;
  276. }
  277. invalidateHttpCache();
  278. if (Minz_Request::isPost()) {
  279. $nonce = Minz_Session::param('nonce');
  280. $username = Minz_Request::param('username', '');
  281. $challenge = Minz_Request::param('challenge', '');
  282. $ok = FreshRSS_FormAuth::checkCredentials(
  283. $username, $conf->passwordHash, $nonce, $challenge
  284. );
  285. if ($ok) {
  286. FreshRSS_Context::$system_conf->auth_type = 'form';
  287. $ok = FreshRSS_Context::$system_conf->save();
  288. if ($ok) {
  289. Minz_Request::good(_t('feedback.auth.form.set'));
  290. } else {
  291. Minz_Request::bad(_t('feedback.auth.form.not_set'),
  292. array('c' => 'auth', 'a' => 'reset'));
  293. }
  294. } else {
  295. Minz_Log::warning('Password mismatch for' .
  296. ' user=' . $username .
  297. ', nonce=' . $nonce .
  298. ', c=' . $challenge);
  299. Minz_Request::bad(_t('feedback.auth.login.invalid'),
  300. array('c' => 'auth', 'a' => 'reset'));
  301. }
  302. }
  303. }
  304. /**
  305. * This action gives possibility to a user to create an account.
  306. */
  307. public function registerAction() {
  308. if (max_registrations_reached()) {
  309. Minz_Error::error(403);
  310. }
  311. Minz_View::prependTitle(_t('gen.auth.registration.title') . ' · ');
  312. }
  313. }