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