3 namespace Shaarli\Security
;
6 use Psr\Log\LoggerInterface
;
7 use Shaarli\Config\ConfigManager
;
10 * User login management
14 /** @var array A reference to the $_GLOBALS array */
15 protected $globals = [];
17 /** @var ConfigManager Configuration Manager instance **/
18 protected $configManager = null;
20 /** @var SessionManager Session Manager instance **/
21 protected $sessionManager = null;
23 /** @var BanManager Ban Manager instance **/
24 protected $banManager;
26 /** @var bool Whether the user is logged in **/
27 protected $isLoggedIn = false;
29 /** @var bool Whether the Shaarli instance is open to public edition **/
30 protected $openShaarli = false;
32 /** @var string User sign-in token depending on remote IP and credentials */
33 protected $staySignedInToken = '';
34 /** @var CookieManager */
35 protected $cookieManager;
36 /** @var LoggerInterface */
42 * @param ConfigManager $configManager Configuration Manager instance
43 * @param SessionManager $sessionManager SessionManager instance
44 * @param CookieManager $cookieManager CookieManager instance
45 * @param BanManager $banManager
46 * @param LoggerInterface $logger Used to log login attempts
48 public function __construct(
49 ConfigManager
$configManager,
50 SessionManager
$sessionManager,
51 CookieManager
$cookieManager,
52 BanManager
$banManager,
53 LoggerInterface
$logger
55 $this->configManager
= $configManager;
56 $this->sessionManager
= $sessionManager;
57 $this->cookieManager
= $cookieManager;
58 $this->banManager
= $banManager;
59 $this->logger
= $logger;
61 if ($this->configManager
->get('security.open_shaarli') === true) {
62 $this->openShaarli
= true;
67 * Generate a token depending on deployment salt, user password and client IP
69 * @param string $clientIpAddress The remote client IP address
71 public function generateStaySignedInToken($clientIpAddress)
73 if ($this->configManager
->get('security.session_protection_disabled') === true) {
74 $clientIpAddress = '';
76 $this->staySignedInToken
= sha1(
77 $this->configManager
->get('credentials.hash')
79 . $this->configManager
->get('credentials.salt')
84 * Return the user's client stay-signed-in token
86 * @return string User's client stay-signed-in token
88 public function getStaySignedInToken()
90 return $this->staySignedInToken
;
94 * Check user session state and validity (expiration)
96 * @param string $clientIpId Client IP address identifier
98 public function checkLoginState($clientIpId)
100 if (! $this->configManager
->exists('credentials.login')) {
101 // Shaarli is not configured yet
102 $this->isLoggedIn
= false;
106 if ($this->staySignedInToken
=== $this->cookieManager
->getCookieParameter(CookieManager
::STAY_SIGNED_IN
)) {
107 // The user client has a valid stay-signed-in cookie
108 // Session information is updated with the current client information
109 $this->sessionManager
->storeLoginInfo($clientIpId);
111 $this->sessionManager
->hasSessionExpired()
112 || $this->sessionManager
->hasClientIpChanged($clientIpId)
114 $this->sessionManager
->logout();
115 $this->isLoggedIn
= false;
119 $this->isLoggedIn
= true;
120 $this->sessionManager
->extendSession();
124 * Return whether the user is currently logged in
126 * @return true when the user is logged in, false otherwise
128 public function isLoggedIn(): bool
130 if ($this->openShaarli
) {
133 return $this->isLoggedIn
;
137 * Check user credentials are valid
139 * @param string $clientIpId Client IP address identifier
140 * @param string $login Username
141 * @param string $password Password
143 * @return bool true if the provided credentials are valid, false otherwise
145 public function checkCredentials($clientIpId, $login, $password)
149 $useLdapLogin = !empty($this->configManager
->get('ldap.host'));
151 $login === $this->configManager
->get('credentials.login')
153 (false === $useLdapLogin && $this->checkCredentialsFromLocalConfig($login, $password))
154 || (true === $useLdapLogin && $this->checkCredentialsFromLdap($login, $password))
157 $this->sessionManager
->storeLoginInfo($clientIpId);
158 $this->logger
->info(format_log('Login successful', $clientIpId));
162 } catch (Exception
$exception) {
163 $this->logger
->info(format_log('Exception while checking credentials: ' . $exception, $clientIpId));
166 $this->logger
->info(format_log('Login failed for user ' . $login, $clientIpId));
173 * Check user credentials from local config
175 * @param string $login Username
176 * @param string $password Password
178 * @return bool true if the provided credentials are valid, false otherwise
180 public function checkCredentialsFromLocalConfig($login, $password)
182 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
184 return $login == $this->configManager
->get('credentials.login')
185 && $hash == $this->configManager
->get('credentials.hash');
189 * Check user credentials are valid through LDAP bind
191 * @param string $remoteIp Remote client IP address
192 * @param string $clientIpId Client IP address identifier
193 * @param string $login Username
194 * @param string $password Password
196 * @return bool true if the provided credentials are valid, false otherwise
198 public function checkCredentialsFromLdap($login, $password, $connect = null, $bind = null)
200 $connect = $connect ?? function ($host) {
201 $resource = ldap_connect($host);
203 ldap_set_option($resource, LDAP_OPT_PROTOCOL_VERSION
, 3);
207 $bind = $bind ?? function ($handle, $dn, $password) {
208 return ldap_bind($handle, $dn, $password);
212 $connect($this->configManager
->get('ldap.host')),
213 sprintf($this->configManager
->get('ldap.dn'), $login),
219 * Handle a failed login and ban the IP after too many failed attempts
221 * @param array $server The $_SERVER array
223 public function handleFailedLogin($server)
225 $this->banManager
->handleFailedAttempt($server);
229 * Handle a successful login
231 * @param array $server The $_SERVER array
233 public function handleSuccessfulLogin($server)
235 $this->banManager
->clearFailures($server);
239 * Check if the user can login from this IP
241 * @param array $server The $_SERVER array
243 * @return bool true if the user is allowed to login
245 public function canLogin($server)
247 return ! $this->banManager
->isBanned($server);