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 $webPath Path on the server in which the cookie will be available on
53 * @param string $clientIpId Client IP address identifier
54 * @param string $token Session token
56 * @return bool true if the user session is valid, false otherwise
58 public function checkLoginState($cookie, $webPath, $clientIpId, $token)
60 if (! $this->configManager
->exists('credentials.login')) {
61 // Shaarli is not configured yet
62 $this->isLoggedIn
= false;
66 if (isset($cookie[SessionManager
::$LOGGED_IN_COOKIE])
67 && $cookie[SessionManager
::$LOGGED_IN_COOKIE] === $token
69 $this->sessionManager
->storeLoginInfo($clientIpId);
70 $this->isLoggedIn
= true;
73 if ($this->sessionManager
->hasSessionExpired()
74 || $this->sessionManager
->hasClientIpChanged($clientIpId)
76 $this->sessionManager
->logout($webPath);
77 $this->isLoggedIn
= false;
81 $this->sessionManager
->extendSession();
85 * Return whether the user is currently logged in
87 * @return true when the user is logged in, false otherwise
89 public function isLoggedIn()
91 if ($this->openShaarli
) {
94 return $this->isLoggedIn
;
98 * Check user credentials are valid
100 * @param string $remoteIp Remote client IP address
101 * @param string $clientIpId Client IP address identifier
102 * @param string $login Username
103 * @param string $password Password
105 * @return bool true if the provided credentials are valid, false otherwise
107 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
109 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
111 if ($login != $this->configManager
->get('credentials.login')
112 || $hash != $this->configManager
->get('credentials.hash')
115 $this->configManager
->get('resource.log'),
117 'Login failed for user ' . $login
122 $this->sessionManager
->storeLoginInfo($clientIpId);
124 $this->configManager
->get('resource.log'),
132 * Read a file containing banned IPs
134 protected function readBanFile()
136 if (! file_exists($this->banFile
)) {
139 include $this->banFile
;
143 * Write the banned IPs to a file
145 protected function writeBanFile()
147 if (! array_key_exists('IPBANS', $this->globals
)) {
152 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals
['IPBANS'], true) . ";\n?>"
157 * Handle a failed login and ban the IP after too many failed attempts
159 * @param array $server The $_SERVER array
161 public function handleFailedLogin($server)
163 $ip = $server['REMOTE_ADDR'];
164 $trusted = $this->configManager
->get('security.trusted_proxies', []);
166 if (in_array($ip, $trusted)) {
167 $ip = getIpAddressFromProxy($server, $trusted);
169 // the IP is behind a trusted forward proxy, but is not forwarded
170 // in the HTTP headers, so we do nothing
175 // increment the fail count for this IP
176 if (isset($this->globals
['IPBANS']['FAILURES'][$ip])) {
177 $this->globals
['IPBANS']['FAILURES'][$ip]++
;
179 $this->globals
['IPBANS']['FAILURES'][$ip] = 1;
182 if ($this->globals
['IPBANS']['FAILURES'][$ip] >= $this->configManager
->get('security.ban_after')) {
183 $this->globals
['IPBANS']['BANS'][$ip] = time() +
$this->configManager
->get('security.ban_duration', 1800);
185 $this->configManager
->get('resource.log'),
186 $server['REMOTE_ADDR'],
187 'IP address banned from login'
190 $this->writeBanFile();
194 * Handle a successful login
196 * @param array $server The $_SERVER array
198 public function handleSuccessfulLogin($server)
200 $ip = $server['REMOTE_ADDR'];
201 // FIXME unban when behind a trusted proxy?
203 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
204 unset($this->globals
['IPBANS']['BANS'][$ip]);
206 $this->writeBanFile();
210 * Check if the user can login from this IP
212 * @param array $server The $_SERVER array
214 * @return bool true if the user is allowed to login
216 public function canLogin($server)
218 $ip = $server['REMOTE_ADDR'];
220 if (! isset($this->globals
['IPBANS']['BANS'][$ip])) {
221 // the user is not banned
225 if ($this->globals
['IPBANS']['BANS'][$ip] > time()) {
226 // the user is still banned
230 // the ban has expired, the user can attempt to log in again
231 logm($this->configManager
->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
232 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
233 unset($this->globals
['IPBANS']['BANS'][$ip]);
235 $this->writeBanFile();