authController.php 10 KB

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