authController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. if (Minz_Request::isPost()) {
  25. $ok = true;
  26. $current_token = $this->view->conf->token;
  27. $token = Minz_Request::param('token', $current_token);
  28. $this->view->conf->_token($token);
  29. $ok &= $this->view->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('configuration_updated',
  52. array('c' => 'auth', 'a' => 'index'));
  53. } else {
  54. Minz_Request::bad('error_occurred',
  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. public function formLoginAction() {
  99. invalidateHttpCache();
  100. $file_mtime = @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js');
  101. Minz_View::appendScript(Minz_Url::display('/scripts/bcrypt.min.js?' . $file_mtime));
  102. if (Minz_Request::isPost()) {
  103. $nonce = Minz_Session::param('nonce');
  104. $username = Minz_Request::param('username', '');
  105. $challenge = Minz_Request::param('challenge', '');
  106. try {
  107. $conf = new FreshRSS_Configuration($username);
  108. } catch(Minz_Exception $e) {
  109. // $username is not a valid user, nor the configuration file!
  110. Minz_Log::warning('Login failure: ' . $e->getMessage());
  111. Minz_Request::bad(_t('invalid_login'),
  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('login'),
  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('invalid_login'),
  137. array('c' => 'auth', 'a' => 'login'));
  138. }
  139. }
  140. }
  141. /**
  142. * This action handles Persona login page.
  143. *
  144. * If this action is reached through a POST request, assertion from Persona
  145. * is verificated and user connected if all is ok.
  146. *
  147. * Parameter is:
  148. * - assertion (default: false)
  149. *
  150. * @todo: Persona system should be moved to a plugin
  151. */
  152. public function personaLoginAction() {
  153. $this->view->res = false;
  154. if (Minz_Request::isPost()) {
  155. $this->view->_useLayout(false);
  156. $assert = Minz_Request::param('assertion');
  157. $url = 'https://verifier.login.persona.org/verify';
  158. $params = 'assertion=' . $assert . '&audience=' .
  159. urlencode(Minz_Url::display(null, 'php', true));
  160. $ch = curl_init();
  161. $options = array(
  162. CURLOPT_URL => $url,
  163. CURLOPT_RETURNTRANSFER => TRUE,
  164. CURLOPT_POST => 2,
  165. CURLOPT_POSTFIELDS => $params
  166. );
  167. curl_setopt_array($ch, $options);
  168. $result = curl_exec($ch);
  169. curl_close($ch);
  170. $res = json_decode($result, true);
  171. $login_ok = false;
  172. $reason = '';
  173. if ($res['status'] === 'okay') {
  174. $email = filter_var($res['email'], FILTER_VALIDATE_EMAIL);
  175. if ($email != '') {
  176. $persona_file = DATA_PATH . '/persona/' . $email . '.txt';
  177. if (($current_user = @file_get_contents($persona_file)) !== false) {
  178. $current_user = trim($current_user);
  179. try {
  180. $conf = new FreshRSS_Configuration($current_user);
  181. $login_ok = strcasecmp($email, $conf->mail_login) === 0;
  182. } catch (Minz_Exception $e) {
  183. //Permission denied or conf file does not exist
  184. $reason = 'Invalid configuration for user ' .
  185. '[' . $current_user . '] ' . $e->getMessage();
  186. }
  187. }
  188. } else {
  189. $reason = 'Invalid email format [' . $res['email'] . ']';
  190. }
  191. } else {
  192. $reason = $res['reason'];
  193. }
  194. if ($login_ok) {
  195. Minz_Session::_param('currentUser', $current_user);
  196. Minz_Session::_param('mail', $email);
  197. FreshRSS_Auth::giveAccess();
  198. invalidateHttpCache();
  199. } else {
  200. Minz_Log::error($reason);
  201. $res = array();
  202. $res['status'] = 'failure';
  203. $res['reason'] = _t('invalid_login');
  204. }
  205. header('Content-Type: application/json; charset=UTF-8');
  206. $this->view->res = $res;
  207. }
  208. }
  209. /**
  210. * This action removes all accesses of the current user.
  211. */
  212. public function logoutAction() {
  213. invalidateHttpCache();
  214. FreshRSS_Auth::removeAccess();
  215. Minz_Request::good(_t('disconnected'),
  216. array('c' => 'index', 'a' => 'index'));
  217. }
  218. /**
  219. * This action resets the authentication system.
  220. *
  221. * After reseting, form auth is set by default.
  222. */
  223. public function resetAction() {
  224. Minz_View::prependTitle(_t('auth_reset') . ' · ');
  225. Minz_View::appendScript(Minz_Url::display(
  226. '/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')
  227. ));
  228. $this->view->no_form = false;
  229. // Enable changement of auth only if Persona!
  230. if (Minz_Configuration::authType() != 'persona') {
  231. $this->view->message = array(
  232. 'status' => 'bad',
  233. 'title' => _t('damn'),
  234. 'body' => _t('auth_not_persona')
  235. );
  236. $this->view->no_form = true;
  237. return;
  238. }
  239. $conf = new FreshRSS_Configuration(Minz_Configuration::defaultUser());
  240. // Admin user must have set its master password.
  241. if (!$conf->passwordHash) {
  242. $this->view->message = array(
  243. 'status' => 'bad',
  244. 'title' => _t('damn'),
  245. 'body' => _t('auth_no_password_set')
  246. );
  247. $this->view->no_form = true;
  248. return;
  249. }
  250. invalidateHttpCache();
  251. if (Minz_Request::isPost()) {
  252. $nonce = Minz_Session::param('nonce');
  253. $username = Minz_Request::param('username', '');
  254. $challenge = Minz_Request::param('challenge', '');
  255. $ok = FreshRSS_FormAuth::checkCredentials(
  256. $username, $conf->passwordHash, $nonce, $challenge
  257. );
  258. if ($ok) {
  259. Minz_Configuration::_authType('form');
  260. $ok = Minz_Configuration::writeFile();
  261. if ($ok) {
  262. Minz_Request::good(_t('auth_form_set'));
  263. } else {
  264. Minz_Request::bad(_t('auth_form_not_set'),
  265. array('c' => 'auth', 'a' => 'reset'));
  266. }
  267. } else {
  268. Minz_Log::warning('Password mismatch for' .
  269. ' user=' . $username .
  270. ', nonce=' . $nonce .
  271. ', c=' . $challenge);
  272. Minz_Request::bad(_t('invalid_login'),
  273. array('c' => 'auth', 'a' => 'reset'));
  274. }
  275. }
  276. }
  277. }