authController.php 11 KB

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