4
0

Auth.php 7.5 KB

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