authController.php 11 KB

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