2 namespace Shaarli\Security
;
4 use Shaarli\Config\ConfigManager
;
7 * User login management
11 /** @var string Name of the cookie set after logging in **/
12 public static $STAY_SIGNED_IN_COOKIE = 'shaarli_staySignedIn';
14 /** @var array A reference to the $_GLOBALS array */
15 protected $globals = [];
17 /** @var ConfigManager Configuration Manager instance **/
18 protected $configManager = null;
20 /** @var SessionManager Session Manager instance **/
21 protected $sessionManager = null;
23 /** @var string Path to the file containing IP bans */
24 protected $banFile = '';
26 /** @var bool Whether the user is logged in **/
27 protected $isLoggedIn = false;
29 /** @var bool Whether the Shaarli instance is open to public edition **/
30 protected $openShaarli = false;
32 /** @var string User sign-in token depending on remote IP and credentials */
33 protected $staySignedInToken = '';
38 * @param array $globals The $GLOBALS array (reference)
39 * @param ConfigManager $configManager Configuration Manager instance
40 * @param SessionManager $sessionManager SessionManager instance
42 public function __construct(& $globals, $configManager, $sessionManager)
44 $this->globals
= &$globals;
45 $this->configManager
= $configManager;
46 $this->sessionManager
= $sessionManager;
47 $this->banFile
= $this->configManager
->get('resource.ban_file', 'data/ipbans.php');
49 if ($this->configManager
->get('security.open_shaarli')) {
50 $this->openShaarli
= true;
55 * Generate a token depending on deployment salt, user password and client IP
57 * @param string $clientIpAddress The remote client IP address
59 public function generateStaySignedInToken($clientIpAddress)
61 $this->staySignedInToken
= sha1(
62 $this->configManager
->get('credentials.hash')
64 . $this->configManager
->get('credentials.salt')
69 * Return the user's client stay-signed-in token
71 * @return string User's client stay-signed-in token
73 public function getStaySignedInToken()
75 return $this->staySignedInToken
;
79 * Check user session state and validity (expiration)
81 * @param array $cookie The $_COOKIE array
82 * @param string $clientIpId Client IP address identifier
84 * @return bool true if the user session is valid, false otherwise
86 public function checkLoginState($cookie, $clientIpId)
88 if (! $this->configManager
->exists('credentials.login')) {
89 // Shaarli is not configured yet
90 $this->isLoggedIn
= false;
94 if (isset($cookie[self
::$STAY_SIGNED_IN_COOKIE])
95 && $cookie[self
::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken
97 $this->sessionManager
->storeLoginInfo($clientIpId);
98 $this->isLoggedIn
= true;
101 if ($this->sessionManager
->hasSessionExpired()
102 || $this->sessionManager
->hasClientIpChanged($clientIpId)
104 $this->sessionManager
->logout();
105 $this->isLoggedIn
= false;
109 $this->sessionManager
->extendSession();
113 * Return whether the user is currently logged in
115 * @return true when the user is logged in, false otherwise
117 public function isLoggedIn()
119 if ($this->openShaarli
) {
122 return $this->isLoggedIn
;
126 * Check user credentials are valid
128 * @param string $remoteIp Remote client IP address
129 * @param string $clientIpId Client IP address identifier
130 * @param string $login Username
131 * @param string $password Password
133 * @return bool true if the provided credentials are valid, false otherwise
135 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
137 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
139 if ($login != $this->configManager
->get('credentials.login')
140 || $hash != $this->configManager
->get('credentials.hash')
143 $this->configManager
->get('resource.log'),
145 'Login failed for user ' . $login
150 $this->sessionManager
->storeLoginInfo($clientIpId);
152 $this->configManager
->get('resource.log'),
160 * Read a file containing banned IPs
162 protected function readBanFile()
164 if (! file_exists($this->banFile
)) {
167 include $this->banFile
;
171 * Write the banned IPs to a file
173 protected function writeBanFile()
175 if (! array_key_exists('IPBANS', $this->globals
)) {
180 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals
['IPBANS'], true) . ";\n?>"
185 * Handle a failed login and ban the IP after too many failed attempts
187 * @param array $server The $_SERVER array
189 public function handleFailedLogin($server)
191 $ip = $server['REMOTE_ADDR'];
192 $trusted = $this->configManager
->get('security.trusted_proxies', []);
194 if (in_array($ip, $trusted)) {
195 $ip = getIpAddressFromProxy($server, $trusted);
197 // the IP is behind a trusted forward proxy, but is not forwarded
198 // in the HTTP headers, so we do nothing
203 // increment the fail count for this IP
204 if (isset($this->globals
['IPBANS']['FAILURES'][$ip])) {
205 $this->globals
['IPBANS']['FAILURES'][$ip]++
;
207 $this->globals
['IPBANS']['FAILURES'][$ip] = 1;
210 if ($this->globals
['IPBANS']['FAILURES'][$ip] >= $this->configManager
->get('security.ban_after')) {
211 $this->globals
['IPBANS']['BANS'][$ip] = time() +
$this->configManager
->get('security.ban_duration', 1800);
213 $this->configManager
->get('resource.log'),
214 $server['REMOTE_ADDR'],
215 'IP address banned from login'
218 $this->writeBanFile();
222 * Handle a successful login
224 * @param array $server The $_SERVER array
226 public function handleSuccessfulLogin($server)
228 $ip = $server['REMOTE_ADDR'];
229 // FIXME unban when behind a trusted proxy?
231 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
232 unset($this->globals
['IPBANS']['BANS'][$ip]);
234 $this->writeBanFile();
238 * Check if the user can login from this IP
240 * @param array $server The $_SERVER array
242 * @return bool true if the user is allowed to login
244 public function canLogin($server)
246 $ip = $server['REMOTE_ADDR'];
248 if (! isset($this->globals
['IPBANS']['BANS'][$ip])) {
249 // the user is not banned
253 if ($this->globals
['IPBANS']['BANS'][$ip] > time()) {
254 // the user is still banned
258 // the ban has expired, the user can attempt to log in again
259 logm($this->configManager
->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
260 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
261 unset($this->globals
['IPBANS']['BANS'][$ip]);
263 $this->writeBanFile();