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 BanManager Ban Manager instance **/
24 protected $banManager;
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 ConfigManager $configManager Configuration Manager instance
39 * @param SessionManager $sessionManager SessionManager instance
41 public function __construct($configManager, $sessionManager)
43 $this->configManager
= $configManager;
44 $this->sessionManager
= $sessionManager;
45 $this->banManager
= new BanManager(
46 $this->configManager
->get('security.trusted_proxies', []),
47 $this->configManager
->get('security.ban_after'),
48 $this->configManager
->get('security.ban_duration'),
49 $this->configManager
->get('resource.ban_file', 'data/ipbans.php'),
50 $this->configManager
->get('resource.log')
53 if ($this->configManager
->get('security.open_shaarli') === true) {
54 $this->openShaarli
= true;
59 * Generate a token depending on deployment salt, user password and client IP
61 * @param string $clientIpAddress The remote client IP address
63 public function generateStaySignedInToken($clientIpAddress)
65 if ($this->configManager
->get('security.session_protection_disabled') === true) {
66 $clientIpAddress = '';
68 $this->staySignedInToken
= sha1(
69 $this->configManager
->get('credentials.hash')
71 . $this->configManager
->get('credentials.salt')
76 * Return the user's client stay-signed-in token
78 * @return string User's client stay-signed-in token
80 public function getStaySignedInToken()
82 return $this->staySignedInToken
;
86 * Check user session state and validity (expiration)
88 * @param array $cookie The $_COOKIE array
89 * @param string $clientIpId Client IP address identifier
91 public function checkLoginState($cookie, $clientIpId)
93 if (! $this->configManager
->exists('credentials.login')) {
94 // Shaarli is not configured yet
95 $this->isLoggedIn
= false;
99 if (isset($cookie[self
::$STAY_SIGNED_IN_COOKIE])
100 && $cookie[self
::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken
102 // The user client has a valid stay-signed-in cookie
103 // Session information is updated with the current client information
104 $this->sessionManager
->storeLoginInfo($clientIpId);
105 } elseif ($this->sessionManager
->hasSessionExpired()
106 || $this->sessionManager
->hasClientIpChanged($clientIpId)
108 $this->sessionManager
->logout();
109 $this->isLoggedIn
= false;
113 $this->isLoggedIn
= true;
114 $this->sessionManager
->extendSession();
118 * Return whether the user is currently logged in
120 * @return true when the user is logged in, false otherwise
122 public function isLoggedIn()
124 if ($this->openShaarli
) {
127 return $this->isLoggedIn
;
131 * Check user credentials are valid
133 * @param string $remoteIp Remote client IP address
134 * @param string $clientIpId Client IP address identifier
135 * @param string $login Username
136 * @param string $password Password
138 * @return bool true if the provided credentials are valid, false otherwise
140 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
142 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
144 if ($login != $this->configManager
->get('credentials.login')
145 || $hash != $this->configManager
->get('credentials.hash')
148 $this->configManager
->get('resource.log'),
150 'Login failed for user ' . $login
155 $this->sessionManager
->storeLoginInfo($clientIpId);
157 $this->configManager
->get('resource.log'),
165 * Handle a failed login and ban the IP after too many failed attempts
167 * @param array $server The $_SERVER array
169 public function handleFailedLogin($server)
171 $this->banManager
->handleFailedAttempt($server);
175 * Handle a successful login
177 * @param array $server The $_SERVER array
179 public function handleSuccessfulLogin($server)
181 $this->banManager
->clearFailures($server);
185 * Check if the user can login from this IP
187 * @param array $server The $_SERVER array
189 * @return bool true if the user is allowed to login
191 public function canLogin($server)
193 return ! $this->banManager
->isBanned($server);