]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LoginManager.php
5 * User login management
9 protected $globals = [];
10 protected $configManager = null;
11 protected $sessionManager = null;
12 protected $banFile = '';
13 protected $isLoggedIn = false;
14 protected $openShaarli = false;
19 * @param array $globals The $GLOBALS array (reference)
20 * @param ConfigManager $configManager Configuration Manager instance
21 * @param SessionManager $sessionManager SessionManager instance
23 public function __construct(& $globals, $configManager, $sessionManager)
25 $this->globals
= &$globals;
26 $this->configManager
= $configManager;
27 $this->sessionManager
= $sessionManager;
28 $this->banFile
= $this->configManager
->get('resource.ban_file', 'data/ipbans.php');
30 if ($this->configManager
->get('security.open_shaarli')) {
31 $this->openShaarli
= true;
36 * Check user session state and validity (expiration)
38 * @param array $server The $_SERVER array
39 * @param array $session The $_SESSION array (reference)
40 * @param array $cookie The $_COOKIE array
41 * @param string $webPath Path on the server in which the cookie will be available on
42 * @param string $token Session token
44 * @return bool true if the user session is valid, false otherwise
46 public function checkLoginState($server, & $session, $cookie, $webPath, $token)
48 if (! $this->configManager
->exists('credentials.login')) {
49 // Shaarli is not configured yet
50 $this->isLoggedIn
= false;
54 if (isset($cookie[SessionManager
::$LOGGED_IN_COOKIE])
55 && $cookie[SessionManager
::$LOGGED_IN_COOKIE] === $token
57 $this->sessionManager
->storeLoginInfo($server);
58 $this->isLoggedIn
= true;
62 // - the session does not exist on the server side
63 // - the session has expired
64 // - the client IP address has changed
65 if (empty($session['uid'])
66 || ($this->configManager
->get('security.session_protection_disabled') === false
67 && $session['ip'] != client_ip_id($server))
68 || time() >= $session['expires_on']
70 $this->sessionManager
->logout($webPath);
71 $this->isLoggedIn
= false;
75 // Extend session validity
76 if (! empty($session['longlastingsession'])) {
77 // "Stay signed in" is enabled
78 $session['expires_on'] = time() +
$session['longlastingsession'];
80 $session['expires_on'] = time() + SessionManager
::$INACTIVITY_TIMEOUT;
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 array $server The $_SERVER array
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($server, $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'),
115 $server['REMOTE_ADDR'],
116 'Login failed for user ' . $login
121 $this->sessionManager
->storeLoginInfo($server);
123 $this->configManager
->get('resource.log'),
124 $server['REMOTE_ADDR'],
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();