2 namespace Shaarli\Security
;
4 use Shaarli\Config\ConfigManager
;
7 * User login management
11 /** @var array A reference to the $_GLOBALS array */
12 protected $globals = [];
14 /** @var ConfigManager Configuration Manager instance **/
15 protected $configManager = null;
17 /** @var SessionManager Session Manager instance **/
18 protected $sessionManager = null;
20 /** @var string Path to the file containing IP bans */
21 protected $banFile = '';
23 /** @var bool Whether the user is logged in **/
24 protected $isLoggedIn = false;
26 /** @var bool Whether the Shaarli instance is open to public edition **/
27 protected $openShaarli = false;
32 * @param array $globals The $GLOBALS array (reference)
33 * @param ConfigManager $configManager Configuration Manager instance
34 * @param SessionManager $sessionManager SessionManager instance
36 public function __construct(& $globals, $configManager, $sessionManager)
38 $this->globals
= &$globals;
39 $this->configManager
= $configManager;
40 $this->sessionManager
= $sessionManager;
41 $this->banFile
= $this->configManager
->get('resource.ban_file', 'data/ipbans.php');
43 if ($this->configManager
->get('security.open_shaarli')) {
44 $this->openShaarli
= true;
49 * Check user session state and validity (expiration)
51 * @param array $cookie The $_COOKIE array
52 * @param string $clientIpId Client IP address identifier
53 * @param string $token Session token
55 * @return bool true if the user session is valid, false otherwise
57 public function checkLoginState($cookie, $clientIpId, $token)
59 if (! $this->configManager
->exists('credentials.login')) {
60 // Shaarli is not configured yet
61 $this->isLoggedIn
= false;
65 if (isset($cookie[SessionManager
::$LOGGED_IN_COOKIE])
66 && $cookie[SessionManager
::$LOGGED_IN_COOKIE] === $token
68 $this->sessionManager
->storeLoginInfo($clientIpId);
69 $this->isLoggedIn
= true;
72 if ($this->sessionManager
->hasSessionExpired()
73 || $this->sessionManager
->hasClientIpChanged($clientIpId)
75 $this->sessionManager
->logout();
76 $this->isLoggedIn
= false;
80 $this->sessionManager
->extendSession();
84 * Return whether the user is currently logged in
86 * @return true when the user is logged in, false otherwise
88 public function isLoggedIn()
90 if ($this->openShaarli
) {
93 return $this->isLoggedIn
;
97 * Check user credentials are valid
99 * @param string $remoteIp Remote client IP address
100 * @param string $clientIpId Client IP address identifier
101 * @param string $login Username
102 * @param string $password Password
104 * @return bool true if the provided credentials are valid, false otherwise
106 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
108 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
110 if ($login != $this->configManager
->get('credentials.login')
111 || $hash != $this->configManager
->get('credentials.hash')
114 $this->configManager
->get('resource.log'),
116 'Login failed for user ' . $login
121 $this->sessionManager
->storeLoginInfo($clientIpId);
123 $this->configManager
->get('resource.log'),
131 * Read a file containing banned IPs
133 protected function readBanFile()
135 if (! file_exists($this->banFile
)) {
138 include $this->banFile
;
142 * Write the banned IPs to a file
144 protected function writeBanFile()
146 if (! array_key_exists('IPBANS', $this->globals
)) {
151 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals
['IPBANS'], true) . ";\n?>"
156 * Handle a failed login and ban the IP after too many failed attempts
158 * @param array $server The $_SERVER array
160 public function handleFailedLogin($server)
162 $ip = $server['REMOTE_ADDR'];
163 $trusted = $this->configManager
->get('security.trusted_proxies', []);
165 if (in_array($ip, $trusted)) {
166 $ip = getIpAddressFromProxy($server, $trusted);
168 // the IP is behind a trusted forward proxy, but is not forwarded
169 // in the HTTP headers, so we do nothing
174 // increment the fail count for this IP
175 if (isset($this->globals
['IPBANS']['FAILURES'][$ip])) {
176 $this->globals
['IPBANS']['FAILURES'][$ip]++
;
178 $this->globals
['IPBANS']['FAILURES'][$ip] = 1;
181 if ($this->globals
['IPBANS']['FAILURES'][$ip] >= $this->configManager
->get('security.ban_after')) {
182 $this->globals
['IPBANS']['BANS'][$ip] = time() +
$this->configManager
->get('security.ban_duration', 1800);
184 $this->configManager
->get('resource.log'),
185 $server['REMOTE_ADDR'],
186 'IP address banned from login'
189 $this->writeBanFile();
193 * Handle a successful login
195 * @param array $server The $_SERVER array
197 public function handleSuccessfulLogin($server)
199 $ip = $server['REMOTE_ADDR'];
200 // FIXME unban when behind a trusted proxy?
202 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
203 unset($this->globals
['IPBANS']['BANS'][$ip]);
205 $this->writeBanFile();
209 * Check if the user can login from this IP
211 * @param array $server The $_SERVER array
213 * @return bool true if the user is allowed to login
215 public function canLogin($server)
217 $ip = $server['REMOTE_ADDR'];
219 if (! isset($this->globals
['IPBANS']['BANS'][$ip])) {
220 // the user is not banned
224 if ($this->globals
['IPBANS']['BANS'][$ip] > time()) {
225 // the user is still banned
229 // the ban has expired, the user can attempt to log in again
230 logm($this->configManager
->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
231 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
232 unset($this->globals
['IPBANS']['BANS'][$ip]);
234 $this->writeBanFile();