2 namespace Shaarli\Security
;
5 use Shaarli\Config\ConfigManager
;
8 * User login management
12 /** @var string Name of the cookie set after logging in **/
13 public static $STAY_SIGNED_IN_COOKIE = 'shaarli_staySignedIn';
15 /** @var array A reference to the $_GLOBALS array */
16 protected $globals = [];
18 /** @var ConfigManager Configuration Manager instance **/
19 protected $configManager = null;
21 /** @var SessionManager Session Manager instance **/
22 protected $sessionManager = null;
24 /** @var BanManager Ban Manager instance **/
25 protected $banManager;
27 /** @var bool Whether the user is logged in **/
28 protected $isLoggedIn = false;
30 /** @var bool Whether the Shaarli instance is open to public edition **/
31 protected $openShaarli = false;
33 /** @var string User sign-in token depending on remote IP and credentials */
34 protected $staySignedInToken = '';
39 * @param ConfigManager $configManager Configuration Manager instance
40 * @param SessionManager $sessionManager SessionManager instance
42 public function __construct($configManager, $sessionManager)
44 $this->configManager
= $configManager;
45 $this->sessionManager
= $sessionManager;
46 $this->banManager
= new BanManager(
47 $this->configManager
->get('security.trusted_proxies', []),
48 $this->configManager
->get('security.ban_after'),
49 $this->configManager
->get('security.ban_duration'),
50 $this->configManager
->get('resource.ban_file', 'data/ipbans.php'),
51 $this->configManager
->get('resource.log')
54 if ($this->configManager
->get('security.open_shaarli') === true) {
55 $this->openShaarli
= true;
60 * Generate a token depending on deployment salt, user password and client IP
62 * @param string $clientIpAddress The remote client IP address
64 public function generateStaySignedInToken($clientIpAddress)
66 if ($this->configManager
->get('security.session_protection_disabled') === true) {
67 $clientIpAddress = '';
69 $this->staySignedInToken
= sha1(
70 $this->configManager
->get('credentials.hash')
72 . $this->configManager
->get('credentials.salt')
77 * Return the user's client stay-signed-in token
79 * @return string User's client stay-signed-in token
81 public function getStaySignedInToken()
83 return $this->staySignedInToken
;
87 * Check user session state and validity (expiration)
89 * @param array $cookie The $_COOKIE array
90 * @param string $clientIpId Client IP address identifier
92 public function checkLoginState($cookie, $clientIpId)
94 if (! $this->configManager
->exists('credentials.login')) {
95 // Shaarli is not configured yet
96 $this->isLoggedIn
= false;
100 if (isset($cookie[self
::$STAY_SIGNED_IN_COOKIE])
101 && $cookie[self
::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken
103 // The user client has a valid stay-signed-in cookie
104 // Session information is updated with the current client information
105 $this->sessionManager
->storeLoginInfo($clientIpId);
106 } elseif ($this->sessionManager
->hasSessionExpired()
107 || $this->sessionManager
->hasClientIpChanged($clientIpId)
109 $this->sessionManager
->logout();
110 $this->isLoggedIn
= false;
114 $this->isLoggedIn
= true;
115 $this->sessionManager
->extendSession();
119 * Return whether the user is currently logged in
121 * @return true when the user is logged in, false otherwise
123 public function isLoggedIn()
125 if ($this->openShaarli
) {
128 return $this->isLoggedIn
;
132 * Check user credentials are valid
134 * @param string $remoteIp Remote client IP address
135 * @param string $clientIpId Client IP address identifier
136 * @param string $login Username
137 * @param string $password Password
139 * @return bool true if the provided credentials are valid, false otherwise
141 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
143 // Check login matches config
144 if ($login !== $this->configManager
->get('credentials.login')) {
150 $useLdapLogin = !empty($this->configManager
->get('ldap.host'));
151 if ((false === $useLdapLogin && $this->checkCredentialsFromLocalConfig($login, $password))
152 || (true === $useLdapLogin && $this->checkCredentialsFromLdap($login, $password))
154 $this->sessionManager
->storeLoginInfo($clientIpId);
156 $this->configManager
->get('resource.log'),
163 catch(Exception
$exception) {
165 $this->configManager
->get('resource.log'),
167 'Exception while checking credentials: ' . $exception
172 $this->configManager
->get('resource.log'),
174 'Login failed for user ' . $login
181 * Check user credentials from local config
183 * @param string $login Username
184 * @param string $password Password
186 * @return bool true if the provided credentials are valid, false otherwise
188 public function checkCredentialsFromLocalConfig($login, $password) {
189 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
191 return $login == $this->configManager
->get('credentials.login')
192 && $hash == $this->configManager
->get('credentials.hash');
196 * Check user credentials are valid through LDAP bind
198 * @param string $remoteIp Remote client IP address
199 * @param string $clientIpId Client IP address identifier
200 * @param string $login Username
201 * @param string $password Password
203 * @return bool true if the provided credentials are valid, false otherwise
205 public function checkCredentialsFromLdap($login, $password, $connect = null, $bind = null)
207 $connect = $connect ?? function($host) { return ldap_connect($host
); };
208 $bind = $bind ?? function($handle, $dn, $password) { return ldap_bind($handle
, $dn
, $password
); };
209 return $bind($connect($this->configManager
->get('ldap.host')), sprintf($this->configManager
->get('ldap.dn'), $login), $password);
213 * Handle a failed login and ban the IP after too many failed attempts
215 * @param array $server The $_SERVER array
217 public function handleFailedLogin($server)
219 $this->banManager
->handleFailedAttempt($server);
223 * Handle a successful login
225 * @param array $server The $_SERVER array
227 public function handleSuccessfulLogin($server)
229 $this->banManager
->clearFailures($server);
233 * Check if the user can login from this IP
235 * @param array $server The $_SERVER array
237 * @return bool true if the user is allowed to login
239 public function canLogin($server)
241 return ! $this->banManager
->isBanned($server);