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 &= $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. // TODO #730: change the way to get the configuration
  110. try {
  111. $conf = new FreshRSS_Configuration($username);
  112. } catch(Minz_Exception $e) {
  113. // $username is not a valid user, nor the configuration file!
  114. Minz_Log::warning('Login failure: ' . $e->getMessage());
  115. Minz_Request::bad(_t('feedback.auth.login.invalid'),
  116. array('c' => 'auth', 'a' => 'login'));
  117. }
  118. $ok = FreshRSS_FormAuth::checkCredentials(
  119. $username, $conf->passwordHash, $nonce, $challenge
  120. );
  121. if ($ok) {
  122. // Set session parameter to give access to the user.
  123. Minz_Session::_param('currentUser', $username);
  124. Minz_Session::_param('passwordHash', $conf->passwordHash);
  125. FreshRSS_Auth::giveAccess();
  126. // Set cookie parameter if nedded.
  127. if (Minz_Request::param('keep_logged_in')) {
  128. FreshRSS_FormAuth::makeCookie($username, $conf->passwordHash);
  129. } else {
  130. FreshRSS_FormAuth::deleteCookie();
  131. }
  132. // All is good, go back to the index.
  133. Minz_Request::good(_t('feedback.auth.login.success'),
  134. array('c' => 'index', 'a' => 'index'));
  135. } else {
  136. Minz_Log::warning('Password mismatch for' .
  137. ' user=' . $username .
  138. ', nonce=' . $nonce .
  139. ', c=' . $challenge);
  140. Minz_Request::bad(_t('feedback.auth.login.invalid'),
  141. array('c' => 'auth', 'a' => 'login'));
  142. }
  143. } elseif (FreshRSS_Context::$system_conf->unsafe_autologin_enabled) {
  144. $username = Minz_Request::param('u', '');
  145. $password = Minz_Request::param('p', '');
  146. Minz_Request::_param('p');
  147. if (!$username) {
  148. return;
  149. }
  150. // TODO #730: change the way to get the configuration
  151. try {
  152. $conf = new FreshRSS_Configuration($username);
  153. } catch(Minz_Exception $e) {
  154. // $username is not a valid user, nor the configuration file!
  155. Minz_Log::warning('Login failure: ' . $e->getMessage());
  156. return;
  157. }
  158. if (!function_exists('password_verify')) {
  159. include_once(LIB_PATH . '/password_compat.php');
  160. }
  161. $s = $conf->passwordHash;
  162. $ok = password_verify($password, $s);
  163. unset($password);
  164. if ($ok) {
  165. Minz_Session::_param('currentUser', $username);
  166. Minz_Session::_param('passwordHash', $s);
  167. FreshRSS_Auth::giveAccess();
  168. Minz_Request::good(_t('feedback.auth.login.success'),
  169. array('c' => 'index', 'a' => 'index'));
  170. } else {
  171. Minz_Log::warning('Unsafe password mismatch for user ' . $username);
  172. Minz_Request::bad(_t('feedback.auth.login.invalid'),
  173. array('c' => 'auth', 'a' => 'login'));
  174. }
  175. }
  176. }
  177. /**
  178. * This action handles Persona login page.
  179. *
  180. * If this action is reached through a POST request, assertion from Persona
  181. * is verificated and user connected if all is ok.
  182. *
  183. * Parameter is:
  184. * - assertion (default: false)
  185. *
  186. * @todo: Persona system should be moved to a plugin
  187. */
  188. public function personaLoginAction() {
  189. $this->view->res = false;
  190. if (Minz_Request::isPost()) {
  191. $this->view->_useLayout(false);
  192. $assert = Minz_Request::param('assertion');
  193. $url = 'https://verifier.login.persona.org/verify';
  194. $params = 'assertion=' . $assert . '&audience=' .
  195. urlencode(Minz_Url::display(null, 'php', true));
  196. $ch = curl_init();
  197. $options = array(
  198. CURLOPT_URL => $url,
  199. CURLOPT_RETURNTRANSFER => TRUE,
  200. CURLOPT_POST => 2,
  201. CURLOPT_POSTFIELDS => $params
  202. );
  203. curl_setopt_array($ch, $options);
  204. $result = curl_exec($ch);
  205. curl_close($ch);
  206. $res = json_decode($result, true);
  207. $login_ok = false;
  208. $reason = '';
  209. if ($res['status'] === 'okay') {
  210. $email = filter_var($res['email'], FILTER_VALIDATE_EMAIL);
  211. if ($email != '') {
  212. $persona_file = DATA_PATH . '/persona/' . $email . '.txt';
  213. if (($current_user = @file_get_contents($persona_file)) !== false) {
  214. $current_user = trim($current_user);
  215. // TODO #730: change the way to get the configuration
  216. try {
  217. $conf = new FreshRSS_Configuration($current_user);
  218. $login_ok = strcasecmp($email, $conf->mail_login) === 0;
  219. } catch (Minz_Exception $e) {
  220. //Permission denied or conf file does not exist
  221. $reason = 'Invalid configuration for user ' .
  222. '[' . $current_user . '] ' . $e->getMessage();
  223. }
  224. }
  225. } else {
  226. $reason = 'Invalid email format [' . $res['email'] . ']';
  227. }
  228. } else {
  229. $reason = $res['reason'];
  230. }
  231. if ($login_ok) {
  232. Minz_Session::_param('currentUser', $current_user);
  233. Minz_Session::_param('mail', $email);
  234. FreshRSS_Auth::giveAccess();
  235. invalidateHttpCache();
  236. } else {
  237. Minz_Log::error($reason);
  238. $res = array();
  239. $res['status'] = 'failure';
  240. $res['reason'] = _t('feedback.auth.login.invalid');
  241. }
  242. header('Content-Type: application/json; charset=UTF-8');
  243. $this->view->res = $res;
  244. }
  245. }
  246. /**
  247. * This action removes all accesses of the current user.
  248. */
  249. public function logoutAction() {
  250. invalidateHttpCache();
  251. FreshRSS_Auth::removeAccess();
  252. Minz_Request::good(_t('feedback.auth.logout.success'),
  253. array('c' => 'index', 'a' => 'index'));
  254. }
  255. /**
  256. * This action resets the authentication system.
  257. *
  258. * After reseting, form auth is set by default.
  259. */
  260. public function resetAction() {
  261. Minz_View::prependTitle(_t('admin.auth.title_reset') . ' · ');
  262. Minz_View::appendScript(Minz_Url::display(
  263. '/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')
  264. ));
  265. $this->view->no_form = false;
  266. // Enable changement of auth only if Persona!
  267. if (FreshRSS_Context::$system_conf->auth_type != 'persona') {
  268. $this->view->message = array(
  269. 'status' => 'bad',
  270. 'title' => _t('gen.short.damn'),
  271. 'body' => _t('feedback.auth.not_persona')
  272. );
  273. $this->view->no_form = true;
  274. return;
  275. }
  276. // TODO #730
  277. $conf = new FreshRSS_Configuration(Minz_Configuration::defaultUser());
  278. // Admin user must have set its master password.
  279. if (!$conf->passwordHash) {
  280. $this->view->message = array(
  281. 'status' => 'bad',
  282. 'title' => _t('gen.short.damn'),
  283. 'body' => _t('feedback.auth.no_password_set')
  284. );
  285. $this->view->no_form = true;
  286. return;
  287. }
  288. invalidateHttpCache();
  289. if (Minz_Request::isPost()) {
  290. $nonce = Minz_Session::param('nonce');
  291. $username = Minz_Request::param('username', '');
  292. $challenge = Minz_Request::param('challenge', '');
  293. $ok = FreshRSS_FormAuth::checkCredentials(
  294. $username, $conf->passwordHash, $nonce, $challenge
  295. );
  296. if ($ok) {
  297. // TODO #730
  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. }