Auth.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * This class handles all authentication process.
  4. */
  5. class FreshRSS_Auth {
  6. /**
  7. * Determines if user is connected.
  8. */
  9. private static $login_ok = false;
  10. /**
  11. * This method initializes authentication system.
  12. */
  13. public static function init() {
  14. if (Minz_Session::param('REMOTE_USER', '') !== httpAuthUser()) {
  15. //HTTP REMOTE_USER has changed
  16. self::removeAccess();
  17. }
  18. self::$login_ok = Minz_Session::param('loginOk', false);
  19. $current_user = Minz_Session::param('currentUser', '');
  20. if ($current_user === '') {
  21. $conf = Minz_Configuration::get('system');
  22. $current_user = $conf->default_user;
  23. Minz_Session::_param('currentUser', $current_user);
  24. }
  25. if (self::$login_ok) {
  26. self::giveAccess();
  27. } elseif (self::accessControl()) {
  28. self::giveAccess();
  29. FreshRSS_UserDAO::touch();
  30. } else {
  31. // Be sure all accesses are removed!
  32. self::removeAccess();
  33. }
  34. }
  35. /**
  36. * This method checks if user is allowed to connect.
  37. *
  38. * Required session parameters are also set in this method (such as
  39. * currentUser).
  40. *
  41. * @return boolean true if user can be connected, false else.
  42. */
  43. private static function accessControl() {
  44. $conf = Minz_Configuration::get('system');
  45. $auth_type = $conf->auth_type;
  46. switch ($auth_type) {
  47. case 'form':
  48. $credentials = FreshRSS_FormAuth::getCredentialsFromCookie();
  49. $current_user = '';
  50. if (isset($credentials[1])) {
  51. $current_user = trim($credentials[0]);
  52. Minz_Session::_param('currentUser', $current_user);
  53. Minz_Session::_param('passwordHash', trim($credentials[1]));
  54. }
  55. return $current_user != '';
  56. case 'http_auth':
  57. $current_user = httpAuthUser();
  58. $login_ok = $current_user != '';
  59. if ($login_ok) {
  60. Minz_Session::_param('currentUser', $current_user);
  61. Minz_Session::_param('REMOTE_USER', $current_user);
  62. }
  63. return $login_ok;
  64. case 'none':
  65. return true;
  66. default:
  67. // TODO load extension
  68. return false;
  69. }
  70. }
  71. /**
  72. * Gives access to the current user.
  73. */
  74. public static function giveAccess() {
  75. $current_user = Minz_Session::param('currentUser');
  76. $user_conf = get_user_configuration($current_user);
  77. if ($user_conf == null) {
  78. self::$login_ok = false;
  79. return;
  80. }
  81. $system_conf = Minz_Configuration::get('system');
  82. switch ($system_conf->auth_type) {
  83. case 'form':
  84. self::$login_ok = Minz_Session::param('passwordHash') === $user_conf->passwordHash;
  85. break;
  86. case 'http_auth':
  87. self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
  88. break;
  89. case 'none':
  90. self::$login_ok = true;
  91. break;
  92. default:
  93. // TODO: extensions
  94. self::$login_ok = false;
  95. }
  96. Minz_Session::_param('loginOk', self::$login_ok);
  97. }
  98. /**
  99. * Returns if current user has access to the given scope.
  100. *
  101. * @param string $scope general (default) or admin
  102. * @return boolean true if user has corresponding access, false else.
  103. */
  104. public static function hasAccess($scope = 'general') {
  105. $conf = Minz_Configuration::get('system');
  106. $default_user = $conf->default_user;
  107. $ok = self::$login_ok;
  108. switch ($scope) {
  109. case 'general':
  110. break;
  111. case 'admin':
  112. $ok &= Minz_Session::param('currentUser') === $default_user;
  113. break;
  114. default:
  115. $ok = false;
  116. }
  117. return $ok;
  118. }
  119. /**
  120. * Removes all accesses for the current user.
  121. */
  122. public static function removeAccess() {
  123. self::$login_ok = false;
  124. Minz_Session::_param('loginOk');
  125. Minz_Session::_param('csrf');
  126. $system_conf = Minz_Configuration::get('system');
  127. $username = '';
  128. $token_param = Minz_Request::param('token', '');
  129. if ($token_param != '') {
  130. $username = trim(Minz_Request::param('user', ''));
  131. if ($username != '') {
  132. $conf = get_user_configuration($username);
  133. if ($conf == null) {
  134. $username = '';
  135. }
  136. }
  137. }
  138. if ($username == '') {
  139. $username = $system_conf->default_user;
  140. }
  141. Minz_Session::_param('currentUser', $username);
  142. switch ($system_conf->auth_type) {
  143. case 'form':
  144. Minz_Session::_param('passwordHash');
  145. FreshRSS_FormAuth::deleteCookie();
  146. break;
  147. case 'http_auth':
  148. case 'none':
  149. // Nothing to do...
  150. break;
  151. default:
  152. // TODO: extensions
  153. }
  154. }
  155. /**
  156. * Return if authentication is enabled on this instance of FRSS.
  157. */
  158. public static function accessNeedsLogin() {
  159. $conf = Minz_Configuration::get('system');
  160. $auth_type = $conf->auth_type;
  161. return $auth_type !== 'none';
  162. }
  163. /**
  164. * Return if authentication requires a PHP action.
  165. */
  166. public static function accessNeedsAction() {
  167. $conf = Minz_Configuration::get('system');
  168. $auth_type = $conf->auth_type;
  169. return $auth_type === 'form';
  170. }
  171. public static function csrfToken() {
  172. $csrf = Minz_Session::param('csrf');
  173. if ($csrf == '') {
  174. $salt = FreshRSS_Context::$system_conf->salt;
  175. $csrf = sha1($salt . uniqid(mt_rand(), true));
  176. Minz_Session::_param('csrf', $csrf);
  177. }
  178. return $csrf;
  179. }
  180. public static function isCsrfOk($token = null) {
  181. $csrf = Minz_Session::param('csrf');
  182. if ($csrf == '') {
  183. return true; //Not logged in yet
  184. }
  185. if ($token === null) {
  186. $token = Minz_Request::fetchPOST('_csrf');
  187. }
  188. return $token === $csrf;
  189. }
  190. }
  191. class FreshRSS_FormAuth {
  192. public static function checkCredentials($username, $hash, $nonce, $challenge) {
  193. if (!FreshRSS_user_Controller::checkUsername($username) ||
  194. !ctype_graph($challenge) ||
  195. !ctype_alnum($nonce)) {
  196. Minz_Log::debug('Invalid credential parameters:' .
  197. ' user=' . $username .
  198. ' challenge=' . $challenge .
  199. ' nonce=' . $nonce);
  200. return false;
  201. }
  202. if (!function_exists('password_verify')) {
  203. include_once(LIB_PATH . '/password_compat.php');
  204. }
  205. return password_verify($nonce . $hash, $challenge);
  206. }
  207. public static function getCredentialsFromCookie() {
  208. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  209. if (!ctype_alnum($token)) {
  210. return array();
  211. }
  212. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  213. $mtime = @filemtime($token_file);
  214. if ($mtime + 2629744 < time()) {
  215. // Token has expired (> 1 month) or does not exist.
  216. // TODO: 1 month -> use a configuration instead
  217. @unlink($token_file);
  218. return array();
  219. }
  220. $credentials = @file_get_contents($token_file);
  221. return $credentials === false ? array() : explode("\t", $credentials, 2);
  222. }
  223. public static function makeCookie($username, $password_hash) {
  224. $conf = Minz_Configuration::get('system');
  225. do {
  226. $token = sha1($conf->salt . $username . uniqid(mt_rand(), true));
  227. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  228. } while (file_exists($token_file));
  229. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  230. return false;
  231. }
  232. $limits = $conf->limits;
  233. $cookie_duration = empty($limits['cookie_duration']) ? 2629744 : $limits['cookie_duration'];
  234. $expire = time() + $cookie_duration;
  235. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  236. return $token;
  237. }
  238. public static function deleteCookie() {
  239. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  240. if (ctype_alnum($token)) {
  241. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  242. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  243. }
  244. if (rand(0, 10) === 1) {
  245. self::purgeTokens();
  246. }
  247. }
  248. public static function purgeTokens() {
  249. $conf = Minz_Configuration::get('system');
  250. $limits = $conf->limits;
  251. $cookie_duration = empty($limits['cookie_duration']) ? 2629744 : $limits['cookie_duration'];
  252. $oldest = time() - $cookie_duration;
  253. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  254. // $extension = $file_info->getExtension(); doesn't work in PHP < 5.3.7
  255. $extension = pathinfo($file_info->getFilename(), PATHINFO_EXTENSION);
  256. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  257. @unlink($file_info->getPathname());
  258. }
  259. }
  260. }
  261. }