Auth.php 7.8 KB

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