Auth.php 8.0 KB

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