Auth.php 7.9 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 (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. $userConfiguration = Minz_Configuration::get('user');
  111. $default_user = $systemConfiguration->default_user;
  112. $ok = self::$login_ok;
  113. switch ($scope) {
  114. case 'general':
  115. break;
  116. case 'admin':
  117. $ok &= $default_user === Minz_Session::param('currentUser') || $userConfiguration->is_admin;
  118. break;
  119. default:
  120. $ok = false;
  121. }
  122. return $ok;
  123. }
  124. /**
  125. * Removes all accesses for the current user.
  126. */
  127. public static function removeAccess() {
  128. self::$login_ok = false;
  129. Minz_Session::_param('loginOk');
  130. Minz_Session::_param('csrf');
  131. Minz_Session::_param('REMOTE_USER');
  132. $system_conf = Minz_Configuration::get('system');
  133. $username = '';
  134. $token_param = Minz_Request::param('token', '');
  135. if ($token_param != '') {
  136. $username = trim(Minz_Request::param('user', ''));
  137. if ($username != '') {
  138. $conf = get_user_configuration($username);
  139. if ($conf == null) {
  140. $username = '';
  141. }
  142. }
  143. }
  144. if ($username == '') {
  145. $username = $system_conf->default_user;
  146. }
  147. Minz_Session::_param('currentUser', $username);
  148. switch ($system_conf->auth_type) {
  149. case 'form':
  150. Minz_Session::_param('passwordHash');
  151. FreshRSS_FormAuth::deleteCookie();
  152. break;
  153. case 'http_auth':
  154. case 'none':
  155. // Nothing to do...
  156. break;
  157. default:
  158. // TODO: extensions
  159. }
  160. }
  161. /**
  162. * Return if authentication is enabled on this instance of FRSS.
  163. */
  164. public static function accessNeedsLogin() {
  165. $conf = Minz_Configuration::get('system');
  166. $auth_type = $conf->auth_type;
  167. return $auth_type !== 'none';
  168. }
  169. /**
  170. * Return if authentication requires a PHP action.
  171. */
  172. public static function accessNeedsAction() {
  173. $conf = Minz_Configuration::get('system');
  174. $auth_type = $conf->auth_type;
  175. return $auth_type === 'form';
  176. }
  177. public static function csrfToken() {
  178. $csrf = Minz_Session::param('csrf');
  179. if ($csrf == '') {
  180. $salt = FreshRSS_Context::$system_conf->salt;
  181. $csrf = sha1($salt . uniqid(mt_rand(), true));
  182. Minz_Session::_param('csrf', $csrf);
  183. }
  184. return $csrf;
  185. }
  186. public static function isCsrfOk($token = null) {
  187. $csrf = Minz_Session::param('csrf');
  188. if ($token === null) {
  189. $token = Minz_Request::fetchPOST('_csrf');
  190. }
  191. return $token != '' && $token === $csrf;
  192. }
  193. }
  194. class FreshRSS_FormAuth {
  195. public static function checkCredentials($username, $hash, $nonce, $challenge) {
  196. if (!FreshRSS_user_Controller::checkUsername($username) ||
  197. !ctype_graph($challenge) ||
  198. !ctype_alnum($nonce)) {
  199. Minz_Log::debug('Invalid credential parameters:' .
  200. ' user=' . $username .
  201. ' challenge=' . $challenge .
  202. ' nonce=' . $nonce);
  203. return false;
  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. $conf = Minz_Configuration::get('system');
  215. $limits = $conf->limits;
  216. $cookie_duration = empty($limits['cookie_duration']) ? 2592000 : $limits['cookie_duration'];
  217. if ($mtime + $cookie_duration < time()) {
  218. // Token has expired (> cookie_duration) or does not exist.
  219. @unlink($token_file);
  220. return array();
  221. }
  222. $credentials = @file_get_contents($token_file);
  223. return $credentials === false ? array() : explode("\t", $credentials, 2);
  224. }
  225. public static function makeCookie($username, $password_hash) {
  226. $conf = Minz_Configuration::get('system');
  227. do {
  228. $token = sha1($conf->salt . $username . uniqid(mt_rand(), true));
  229. $token_file = DATA_PATH . '/tokens/' . $token . '.txt';
  230. } while (file_exists($token_file));
  231. if (@file_put_contents($token_file, $username . "\t" . $password_hash) === false) {
  232. return false;
  233. }
  234. $limits = $conf->limits;
  235. $cookie_duration = empty($limits['cookie_duration']) ? 2592000 : $limits['cookie_duration'];
  236. $expire = time() + $cookie_duration;
  237. Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
  238. return $token;
  239. }
  240. public static function deleteCookie() {
  241. $token = Minz_Session::getLongTermCookie('FreshRSS_login');
  242. if (ctype_alnum($token)) {
  243. Minz_Session::deleteLongTermCookie('FreshRSS_login');
  244. @unlink(DATA_PATH . '/tokens/' . $token . '.txt');
  245. }
  246. if (rand(0, 10) === 1) {
  247. self::purgeTokens();
  248. }
  249. }
  250. public static function purgeTokens() {
  251. $conf = Minz_Configuration::get('system');
  252. $limits = $conf->limits;
  253. $cookie_duration = empty($limits['cookie_duration']) ? 2592000 : $limits['cookie_duration'];
  254. $oldest = time() - $cookie_duration;
  255. foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
  256. $extension = $file_info->getExtension();
  257. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  258. @unlink($file_info->getPathname());
  259. }
  260. }
  261. }
  262. }