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