2 namespace Shaarli\Security
;
5 use Shaarli\Config\ConfigManager
;
8 * User login management
12 /** @var array A reference to the $_GLOBALS array */
13 protected $globals = [];
15 /** @var ConfigManager Configuration Manager instance **/
16 protected $configManager = null;
18 /** @var SessionManager Session Manager instance **/
19 protected $sessionManager = null;
21 /** @var BanManager Ban Manager instance **/
22 protected $banManager;
24 /** @var bool Whether the user is logged in **/
25 protected $isLoggedIn = false;
27 /** @var bool Whether the Shaarli instance is open to public edition **/
28 protected $openShaarli = false;
30 /** @var string User sign-in token depending on remote IP and credentials */
31 protected $staySignedInToken = '';
32 /** @var CookieManager */
33 protected $cookieManager;
38 * @param ConfigManager $configManager Configuration Manager instance
39 * @param SessionManager $sessionManager SessionManager instance
40 * @param CookieManager $cookieManager CookieManager instance
42 public function __construct($configManager, $sessionManager, $cookieManager)
44 $this->configManager
= $configManager;
45 $this->sessionManager
= $sessionManager;
46 $this->cookieManager
= $cookieManager;
47 $this->banManager
= new BanManager(
48 $this->configManager
->get('security.trusted_proxies', []),
49 $this->configManager
->get('security.ban_after'),
50 $this->configManager
->get('security.ban_duration'),
51 $this->configManager
->get('resource.ban_file', 'data/ipbans.php'),
52 $this->configManager
->get('resource.log')
55 if ($this->configManager
->get('security.open_shaarli') === true) {
56 $this->openShaarli
= true;
61 * Generate a token depending on deployment salt, user password and client IP
63 * @param string $clientIpAddress The remote client IP address
65 public function generateStaySignedInToken($clientIpAddress)
67 if ($this->configManager
->get('security.session_protection_disabled') === true) {
68 $clientIpAddress = '';
70 $this->staySignedInToken
= sha1(
71 $this->configManager
->get('credentials.hash')
73 . $this->configManager
->get('credentials.salt')
78 * Return the user's client stay-signed-in token
80 * @return string User's client stay-signed-in token
82 public function getStaySignedInToken()
84 return $this->staySignedInToken
;
88 * Check user session state and validity (expiration)
90 * @param string $clientIpId Client IP address identifier
92 public function checkLoginState($clientIpId)
94 if (! $this->configManager
->exists('credentials.login')) {
95 // Shaarli is not configured yet
96 $this->isLoggedIn
= false;
100 if ($this->staySignedInToken
=== $this->cookieManager
->getCookieParameter(CookieManager
::STAY_SIGNED_IN
)) {
101 // The user client has a valid stay-signed-in cookie
102 // Session information is updated with the current client information
103 $this->sessionManager
->storeLoginInfo($clientIpId);
104 } elseif ($this->sessionManager
->hasSessionExpired()
105 || $this->sessionManager
->hasClientIpChanged($clientIpId)
107 $this->sessionManager
->logout();
108 $this->isLoggedIn
= false;
112 $this->isLoggedIn
= true;
113 $this->sessionManager
->extendSession();
117 * Return whether the user is currently logged in
119 * @return true when the user is logged in, false otherwise
121 public function isLoggedIn()
123 if ($this->openShaarli
) {
126 return $this->isLoggedIn
;
130 * Check user credentials are valid
132 * @param string $remoteIp Remote client IP address
133 * @param string $clientIpId Client IP address identifier
134 * @param string $login Username
135 * @param string $password Password
137 * @return bool true if the provided credentials are valid, false otherwise
139 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
141 // Check login matches config
142 if ($login !== $this->configManager
->get('credentials.login')) {
148 $useLdapLogin = !empty($this->configManager
->get('ldap.host'));
149 if ((false === $useLdapLogin && $this->checkCredentialsFromLocalConfig($login, $password))
150 || (true === $useLdapLogin && $this->checkCredentialsFromLdap($login, $password))
152 $this->sessionManager
->storeLoginInfo($clientIpId);
154 $this->configManager
->get('resource.log'),
161 catch(Exception
$exception) {
163 $this->configManager
->get('resource.log'),
165 'Exception while checking credentials: ' . $exception
170 $this->configManager
->get('resource.log'),
172 'Login failed for user ' . $login
179 * Check user credentials from local config
181 * @param string $login Username
182 * @param string $password Password
184 * @return bool true if the provided credentials are valid, false otherwise
186 public function checkCredentialsFromLocalConfig($login, $password) {
187 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
189 return $login == $this->configManager
->get('credentials.login')
190 && $hash == $this->configManager
->get('credentials.hash');
194 * Check user credentials are valid through LDAP bind
196 * @param string $remoteIp Remote client IP address
197 * @param string $clientIpId Client IP address identifier
198 * @param string $login Username
199 * @param string $password Password
201 * @return bool true if the provided credentials are valid, false otherwise
203 public function checkCredentialsFromLdap($login, $password, $connect = null, $bind = null)
205 $connect = $connect ?? function($host) {
206 $resource = ldap_connect($host);
208 ldap_set_option($resource, LDAP_OPT_PROTOCOL_VERSION
, 3);
212 $bind = $bind ?? function($handle, $dn, $password) {
213 return ldap_bind($handle, $dn, $password);
217 $connect($this->configManager
->get('ldap.host')),
218 sprintf($this->configManager
->get('ldap.dn'), $login),
224 * Handle a failed login and ban the IP after too many failed attempts
226 * @param array $server The $_SERVER array
228 public function handleFailedLogin($server)
230 $this->banManager
->handleFailedAttempt($server);
234 * Handle a successful login
236 * @param array $server The $_SERVER array
238 public function handleSuccessfulLogin($server)
240 $this->banManager
->clearFailures($server);
244 * Check if the user can login from this IP
246 * @param array $server The $_SERVER array
248 * @return bool true if the user is allowed to login
250 public function canLogin($server)
252 return ! $this->banManager
->isBanned($server);