Auth.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * This class handles all authentication process.
  4. */
  5. class FreshRSS_Auth {
  6. /**
  7. * Determines if user is connected.
  8. */
  9. const DEFAULT_COOKIE_DURATION = 7776000;
  10. private static $login_ok = false;
  11. /**
  12. * This method initializes authentication system.
  13. */
  14. public static function init() {
  15. if (isset($_SESSION['REMOTE_USER']) && $_SESSION['REMOTE_USER'] !== httpAuthUser()) {
  16. //HTTP REMOTE_USER has changed
  17. self::removeAccess();
  18. }
  19. self::$login_ok = Minz_Session::param('loginOk', false);
  20. $current_user = Minz_Session::param('currentUser', '');
  21. if ($current_user === '') {
  22. $conf = Minz_Configuration::get('system');
  23. $current_user = $conf->default_user;
  24. Minz_Session::_params([
  25. 'currentUser' => $current_user,
  26. 'csrf' => false,
  27. ]);
  28. }
  29. if (self::$login_ok) {
  30. self::giveAccess();
  31. } elseif (self::accessControl() && self::giveAccess()) {
  32. FreshRSS_UserDAO::touch();
  33. } else {
  34. // Be sure all accesses are removed!
  35. self::removeAccess();
  36. }
  37. return self::$login_ok;
  38. }
  39. /**
  40. * This method checks if user is allowed to connect.
  41. *
  42. * Required session parameters are also set in this method (such as
  43. * currentUser).
  44. *
  45. * @return boolean true if user can be connected, false else.
  46. */
  47. private static function accessControl() {
  48. FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
  49. $auth_type = FreshRSS_Context::$system_conf->auth_type;
  50. switch ($auth_type) {
  51. case 'form':
  52. $credentials = FreshRSS_FormAuth::getCredentialsFromCookie();
  53. $current_user = '';
  54. if (isset($credentials[1])) {
  55. $current_user = trim($credentials[0]);
  56. Minz_Session::_params([
  57. 'currentUser' => $current_user,
  58. 'passwordHash' => trim($credentials[1]),
  59. 'csrf' => false,
  60. ]);
  61. }
  62. return $current_user != '';
  63. case 'http_auth':
  64. $current_user = httpAuthUser();
  65. if ($current_user == '') {
  66. return false;
  67. }
  68. $login_ok = FreshRSS_UserDAO::exists($current_user);
  69. if (!$login_ok && FreshRSS_Context::$system_conf->http_auth_auto_register) {
  70. $email = null;
  71. if (FreshRSS_Context::$system_conf->http_auth_auto_register_email_field !== '' &&
  72. isset($_SERVER[FreshRSS_Context::$system_conf->http_auth_auto_register_email_field])) {
  73. $email = $_SERVER[FreshRSS_Context::$system_conf->http_auth_auto_register_email_field];
  74. }
  75. $language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::$system_conf->language);
  76. Minz_Translate::init($language);
  77. $login_ok = FreshRSS_user_Controller::createUser($current_user, $email, '', [
  78. 'language' => $language,
  79. ]);
  80. }
  81. if ($login_ok) {
  82. Minz_Session::_params([
  83. 'currentUser' => $current_user,
  84. 'csrf' => false,
  85. ]);
  86. }
  87. return $login_ok;
  88. case 'none':
  89. return true;
  90. default:
  91. // TODO load extension
  92. return false;
  93. }
  94. }
  95. /**
  96. * Gives access to the current user.
  97. */
  98. public static function giveAccess() {
  99. $current_user = Minz_Session::param('currentUser');
  100. $user_conf = get_user_configuration($current_user);
  101. if ($user_conf == null) {
  102. self::$login_ok = false;
  103. return false;
  104. }
  105. $system_conf = Minz_Configuration::get('system');
  106. switch ($system_conf->auth_type) {
  107. case 'form':
  108. self::$login_ok = Minz_Session::param('passwordHash') === $user_conf->passwordHash;
  109. break;
  110. case 'http_auth':
  111. self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
  112. break;
  113. case 'none':
  114. self::$login_ok = true;
  115. break;
  116. default:
  117. // TODO: extensions
  118. self::$login_ok = false;
  119. }
  120. Minz_Session::_params([
  121. 'loginOk' => self::$login_ok,
  122. 'REMOTE_USER' => httpAuthUser(),
  123. ]);
  124. return self::$login_ok;
  125. }
  126. /**
  127. * Returns if current user has access to the given scope.
  128. *
  129. * @param string $scope general (default) or admin
  130. * @return boolean true if user has corresponding access, false else.
  131. */
  132. public static function hasAccess($scope = 'general') {
  133. $systemConfiguration = Minz_Configuration::get('system');
  134. $currentUser = Minz_Session::param('currentUser');
  135. $userConfiguration = get_user_configuration($currentUser);
  136. $isAdmin = $userConfiguration && $userConfiguration->is_admin;
  137. $default_user = $systemConfiguration->default_user;
  138. $ok = self::$login_ok;
  139. switch ($scope) {
  140. case 'general':
  141. break;
  142. case 'admin':
  143. $ok &= $default_user === $currentUser || $isAdmin;
  144. break;
  145. default:
  146. $ok = false;
  147. }
  148. return $ok;
  149. }
  150. /**
  151. * Removes all accesses for the current user.
  152. */
  153. public static function removeAccess() {
  154. self::$login_ok = false;
  155. Minz_Session::_params([
  156. 'loginOk' => false,
  157. 'csrf' => false,
  158. 'REMOTE_USER' => false,
  159. ]);
  160. $system_conf = Minz_Configuration::get('system');
  161. $username = '';
  162. $token_param = Minz_Request::param('token', '');
  163. if ($token_param != '') {
  164. $username = trim(Minz_Request::param('user', ''));
  165. if ($username != '') {
  166. $conf = get_user_configuration($username);
  167. if ($conf == null) {
  168. $username = '';
  169. }
  170. }
  171. }
  172. if ($username == '') {
  173. $username = $system_conf->default_user;
  174. }
  175. Minz_Session::_param('currentUser', $username);
  176. switch ($system_conf->auth_type) {
  177. case 'form':
  178. Minz_Session::_param('passwordHash');
  179. FreshRSS_FormAuth::deleteCookie();
  180. break;
  181. case 'http_auth':
  182. case 'none':
  183. // Nothing to do...
  184. break;
  185. default:
  186. // TODO: extensions
  187. }
  188. }
  189. /**
  190. * Return if authentication is enabled on this instance of FRSS.
  191. */
  192. public static function accessNeedsLogin() {
  193. $conf = Minz_Configuration::get('system');
  194. $auth_type = $conf->auth_type;
  195. return $auth_type !== 'none';
  196. }
  197. /**
  198. * Return if authentication requires a PHP action.
  199. */
  200. public static function accessNeedsAction() {
  201. $conf = Minz_Configuration::get('system');
  202. $auth_type = $conf->auth_type;
  203. return $auth_type === 'form';
  204. }
  205. public static function csrfToken() {
  206. $csrf = Minz_Session::param('csrf');
  207. if ($csrf == '') {
  208. $salt = FreshRSS_Context::$system_conf->salt;
  209. $csrf = sha1($salt . uniqid(mt_rand(), true));
  210. Minz_Session::_param('csrf', $csrf);
  211. }
  212. return $csrf;
  213. }
  214. public static function isCsrfOk($token = null) {
  215. $csrf = Minz_Session::param('csrf');
  216. if ($token === null) {
  217. $token = Minz_Request::fetchPOST('_csrf');
  218. }
  219. return $token != '' && $token === $csrf;
  220. }
  221. }