]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LoginManager.php
5 * User login management
9 /** @var array A reference to the $_GLOBALS array */
10 protected $globals = [];
12 /** @var ConfigManager Configuration Manager instance **/
13 protected $configManager = null;
15 /** @var SessionManager Session Manager instance **/
16 protected $sessionManager = null;
18 /** @var string Path to the file containing IP bans */
19 protected $banFile = '';
21 /** @var bool Whether the user is logged in **/
22 protected $isLoggedIn = false;
24 /** @var bool Whether the Shaarli instance is open to public edition **/
25 protected $openShaarli = false;
30 * @param array $globals The $GLOBALS array (reference)
31 * @param ConfigManager $configManager Configuration Manager instance
32 * @param SessionManager $sessionManager SessionManager instance
34 public function __construct(& $globals, $configManager, $sessionManager)
36 $this->globals
= &$globals;
37 $this->configManager
= $configManager;
38 $this->sessionManager
= $sessionManager;
39 $this->banFile
= $this->configManager
->get('resource.ban_file', 'data/ipbans.php');
41 if ($this->configManager
->get('security.open_shaarli')) {
42 $this->openShaarli
= true;
47 * Check user session state and validity (expiration)
49 * @param array $server The $_SERVER array
50 * @param array $session The $_SESSION array (reference)
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 $token Session token
55 * @return bool true if the user session is valid, false otherwise
57 public function checkLoginState($server, & $session, $cookie, $webPath, $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($server);
69 $this->isLoggedIn
= true;
73 // - the session does not exist on the server side
74 // - the session has expired
75 // - the client IP address has changed
76 if (empty($session['uid'])
77 || ($this->configManager
->get('security.session_protection_disabled') === false
78 && $session['ip'] != client_ip_id($server))
79 || time() >= $session['expires_on']
81 $this->sessionManager
->logout($webPath);
82 $this->isLoggedIn
= false;
86 // Extend session validity
87 if (! empty($session['longlastingsession'])) {
88 // "Stay signed in" is enabled
89 $session['expires_on'] = time() +
$session['longlastingsession'];
91 $session['expires_on'] = time() + SessionManager
::$INACTIVITY_TIMEOUT;
96 * Return whether the user is currently logged in
98 * @return true when the user is logged in, false otherwise
100 public function isLoggedIn()
102 if ($this->openShaarli
) {
105 return $this->isLoggedIn
;
109 * Check user credentials are valid
111 * @param array $server The $_SERVER array
112 * @param string $login Username
113 * @param string $password Password
115 * @return bool true if the provided credentials are valid, false otherwise
117 public function checkCredentials($server, $login, $password)
119 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
121 if ($login != $this->configManager
->get('credentials.login')
122 || $hash != $this->configManager
->get('credentials.hash')
125 $this->configManager
->get('resource.log'),
126 $server['REMOTE_ADDR'],
127 'Login failed for user ' . $login
132 $this->sessionManager
->storeLoginInfo($server);
134 $this->configManager
->get('resource.log'),
135 $server['REMOTE_ADDR'],
142 * Read a file containing banned IPs
144 protected function readBanFile()
146 if (! file_exists($this->banFile
)) {
149 include $this->banFile
;
153 * Write the banned IPs to a file
155 protected function writeBanFile()
157 if (! array_key_exists('IPBANS', $this->globals
)) {
162 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals
['IPBANS'], true) . ";\n?>"
167 * Handle a failed login and ban the IP after too many failed attempts
169 * @param array $server The $_SERVER array
171 public function handleFailedLogin($server)
173 $ip = $server['REMOTE_ADDR'];
174 $trusted = $this->configManager
->get('security.trusted_proxies', []);
176 if (in_array($ip, $trusted)) {
177 $ip = getIpAddressFromProxy($server, $trusted);
179 // the IP is behind a trusted forward proxy, but is not forwarded
180 // in the HTTP headers, so we do nothing
185 // increment the fail count for this IP
186 if (isset($this->globals
['IPBANS']['FAILURES'][$ip])) {
187 $this->globals
['IPBANS']['FAILURES'][$ip]++
;
189 $this->globals
['IPBANS']['FAILURES'][$ip] = 1;
192 if ($this->globals
['IPBANS']['FAILURES'][$ip] >= $this->configManager
->get('security.ban_after')) {
193 $this->globals
['IPBANS']['BANS'][$ip] = time() +
$this->configManager
->get('security.ban_duration', 1800);
195 $this->configManager
->get('resource.log'),
196 $server['REMOTE_ADDR'],
197 'IP address banned from login'
200 $this->writeBanFile();
204 * Handle a successful login
206 * @param array $server The $_SERVER array
208 public function handleSuccessfulLogin($server)
210 $ip = $server['REMOTE_ADDR'];
211 // FIXME unban when behind a trusted proxy?
213 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
214 unset($this->globals
['IPBANS']['BANS'][$ip]);
216 $this->writeBanFile();
220 * Check if the user can login from this IP
222 * @param array $server The $_SERVER array
224 * @return bool true if the user is allowed to login
226 public function canLogin($server)
228 $ip = $server['REMOTE_ADDR'];
230 if (! isset($this->globals
['IPBANS']['BANS'][$ip])) {
231 // the user is not banned
235 if ($this->globals
['IPBANS']['BANS'][$ip] > time()) {
236 // the user is still banned
240 // the ban has expired, the user can attempt to log in again
241 logm($this->configManager
->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
242 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
243 unset($this->globals
['IPBANS']['BANS'][$ip]);
245 $this->writeBanFile();