Auth.php 7.8 KB

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