4
0

authController.php 11 KB

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