Auth.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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() && self::giveAccess()) {
  28. FreshRSS_UserDAO::touch();
  29. } else {
  30. // Be sure all accesses are removed!
  31. self::removeAccess();
  32. }
  33. return self::$login_ok;
  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 != '' && FreshRSS_UserDAO::exists($current_user);
  59. if ($login_ok) {
  60. Minz_Session::_param('currentUser', $current_user);
  61. }
  62. return $login_ok;
  63. case 'none':
  64. return true;
  65. default:
  66. // TODO load extension
  67. return false;
  68. }
  69. }
  70. /**
  71. * Gives access to the current user.
  72. */
  73. public static function giveAccess() {
  74. $current_user = Minz_Session::param('currentUser');
  75. $user_conf = get_user_configuration($current_user);
  76. if ($user_conf == null) {
  77. self::$login_ok = false;
  78. return false;
  79. }
  80. $system_conf = Minz_Configuration::get('system');
  81. switch ($system_conf->auth_type) {
  82. case 'form':
  83. self::$login_ok = Minz_Session::param('passwordHash') === $user_conf->passwordHash;
  84. break;
  85. case 'http_auth':
  86. self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
  87. break;
  88. case 'none':
  89. self::$login_ok = true;
  90. break;
  91. default:
  92. // TODO: extensions
  93. self::$login_ok = false;
  94. }
  95. Minz_Session::_param('loginOk', self::$login_ok);
  96. Minz_Session::_param('REMOTE_USER', httpAuthUser());
  97. return self::$login_ok;
  98. }
  99. /**
  100. * Returns if current user has access to the given scope.
  101. *
  102. * @param string $scope general (default) or admin
  103. * @return boolean true if user has corresponding access, false else.
  104. */
  105. public static function hasAccess($scope = 'general') {
  106. $conf = Minz_Configuration::get('system');
  107. $default_user = $conf->default_user;
  108. $ok = self::$login_ok;
  109. switch ($scope) {
  110. case 'general':
  111. break;
  112. case 'admin':
  113. $ok &= Minz_Session::param('currentUser') === $default_user;
  114. break;
  115. default:
  116. $ok = false;
  117. }
  118. return $ok;
  119. }
  120. /**
  121. * Removes all accesses for the current user.
  122. */
  123. public static function removeAccess() {
  124. self::$login_ok = false;
  125. Minz_Session::_param('loginOk');
  126. Minz_Session::_param('csrf');
  127. Minz_Session::_param('REMOTE_USER');
  128. $system_conf = Minz_Configuration::get('system');
  129. $username = '';
  130. $token_param = Minz_Request::param('token', '');
  131. if ($token_param != '') {
  132. $username = trim(Minz_Request::param('user', ''));
  133. if ($username != '') {
  134. $conf = get_user_configuration($username);
  135. if ($conf == null) {
  136. $username = '';
  137. }
  138. }
  139. }
  140. if ($username == '') {
  141. $username = $system_conf->default_user;
  142. }
  143. Minz_Session::_param('currentUser', $username);
  144. switch ($system_conf->auth_type) {
  145. case 'form':
  146. Minz_Session::_param('passwordHash');
  147. FreshRSS_FormAuth::deleteCookie();
  148. break;
  149. case 'http_auth':
  150. case 'none':
  151. // Nothing to do...
  152. break;
  153. default:
  154. // TODO: extensions
  155. }
  156. }
  157. /**
  158. * Return if authentication is enabled on this instance of FRSS.
  159. */
  160. public static function accessNeedsLogin() {
  161. $conf = Minz_Configuration::get('system');
  162. $auth_type = $conf->auth_type;
  163. return $auth_type !== 'none';
  164. }
  165. /**
  166. * Return if authentication requires a PHP action.
  167. */
  168. public static function accessNeedsAction() {
  169. $conf = Minz_Configuration::get('system');
  170. $auth_type = $conf->auth_type;
  171. return $auth_type === 'form';
  172. }
  173. public static function csrfToken() {
  174. $csrf = Minz_Session::param('csrf');
  175. if ($csrf == '') {
  176. $salt = FreshRSS_Context::$system_conf->salt;
  177. $csrf = sha1($salt . uniqid(mt_rand(), true));
  178. Minz_Session::_param('csrf', $csrf);
  179. }
  180. return $csrf;
  181. }
  182. public static function isCsrfOk($token = null) {
  183. $csrf = Minz_Session::param('csrf');
  184. if ($csrf == '') {
  185. return true; //Not logged in yet
  186. }
  187. if ($token === null) {
  188. $token = Minz_Request::fetchPOST('_csrf');
  189. }
  190. return $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. if (!function_exists('password_verify')) {
  205. include_once(LIB_PATH . '/password_compat.php');
  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(); doesn't work in PHP < 5.3.7
  259. $extension = pathinfo($file_info->getFilename(), PATHINFO_EXTENSION);
  260. if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
  261. @unlink($file_info->getPathname());
  262. }
  263. }
  264. }
  265. }