]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LoginManager.php
Delegate session operations to SessionManager
[github/shaarli/Shaarli.git] / application / LoginManager.php
1 <?php
2 namespace Shaarli;
3
4 use Shaarli\Config\ConfigManager;
5
6 /**
7 * User login management
8 */
9 class LoginManager
10 {
11 /** @var array A reference to the $_GLOBALS array */
12 protected $globals = [];
13
14 /** @var ConfigManager Configuration Manager instance **/
15 protected $configManager = null;
16
17 /** @var SessionManager Session Manager instance **/
18 protected $sessionManager = null;
19
20 /** @var string Path to the file containing IP bans */
21 protected $banFile = '';
22
23 /** @var bool Whether the user is logged in **/
24 protected $isLoggedIn = false;
25
26 /** @var bool Whether the Shaarli instance is open to public edition **/
27 protected $openShaarli = false;
28
29 /**
30 * Constructor
31 *
32 * @param array $globals The $GLOBALS array (reference)
33 * @param ConfigManager $configManager Configuration Manager instance
34 * @param SessionManager $sessionManager SessionManager instance
35 */
36 public function __construct(& $globals, $configManager, $sessionManager)
37 {
38 $this->globals = &$globals;
39 $this->configManager = $configManager;
40 $this->sessionManager = $sessionManager;
41 $this->banFile = $this->configManager->get('resource.ban_file', 'data/ipbans.php');
42 $this->readBanFile();
43 if ($this->configManager->get('security.open_shaarli')) {
44 $this->openShaarli = true;
45 }
46 }
47
48 /**
49 * Check user session state and validity (expiration)
50 *
51 * @param array $server The $_SERVER array
52 * @param array $session The $_SESSION array (reference)
53 * @param array $cookie The $_COOKIE array
54 * @param string $webPath Path on the server in which the cookie will be available on
55 * @param string $token Session token
56 *
57 * @return bool true if the user session is valid, false otherwise
58 */
59 public function checkLoginState($server, & $session, $cookie, $webPath, $token)
60 {
61 if (! $this->configManager->exists('credentials.login')) {
62 // Shaarli is not configured yet
63 $this->isLoggedIn = false;
64 return;
65 }
66
67 $clientIpId = client_ip_id($server);
68
69 if (isset($cookie[SessionManager::$LOGGED_IN_COOKIE])
70 && $cookie[SessionManager::$LOGGED_IN_COOKIE] === $token
71 ) {
72 $this->sessionManager->storeLoginInfo($clientIpId);
73 $this->isLoggedIn = true;
74 }
75
76 if ($this->sessionManager->hasSessionExpired()
77 || $this->sessionManager->hasClientIpChanged($clientIpId)
78 ) {
79 $this->sessionManager->logout($webPath);
80 $this->isLoggedIn = false;
81 return;
82 }
83
84 $this->sessionManager->extendSession();
85 }
86
87 /**
88 * Return whether the user is currently logged in
89 *
90 * @return true when the user is logged in, false otherwise
91 */
92 public function isLoggedIn()
93 {
94 if ($this->openShaarli) {
95 return true;
96 }
97 return $this->isLoggedIn;
98 }
99
100 /**
101 * Check user credentials are valid
102 *
103 * @param array $server The $_SERVER array
104 * @param string $login Username
105 * @param string $password Password
106 *
107 * @return bool true if the provided credentials are valid, false otherwise
108 */
109 public function checkCredentials($server, $login, $password)
110 {
111 $hash = sha1($password . $login . $this->configManager->get('credentials.salt'));
112
113 if ($login != $this->configManager->get('credentials.login')
114 || $hash != $this->configManager->get('credentials.hash')
115 ) {
116 logm(
117 $this->configManager->get('resource.log'),
118 $server['REMOTE_ADDR'],
119 'Login failed for user ' . $login
120 );
121 return false;
122 }
123
124 $clientIpId = client_ip_id($server);
125 $this->sessionManager->storeLoginInfo($clientIpId);
126 logm(
127 $this->configManager->get('resource.log'),
128 $server['REMOTE_ADDR'],
129 'Login successful'
130 );
131 return true;
132 }
133
134 /**
135 * Read a file containing banned IPs
136 */
137 protected function readBanFile()
138 {
139 if (! file_exists($this->banFile)) {
140 return;
141 }
142 include $this->banFile;
143 }
144
145 /**
146 * Write the banned IPs to a file
147 */
148 protected function writeBanFile()
149 {
150 if (! array_key_exists('IPBANS', $this->globals)) {
151 return;
152 }
153 file_put_contents(
154 $this->banFile,
155 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals['IPBANS'], true) . ";\n?>"
156 );
157 }
158
159 /**
160 * Handle a failed login and ban the IP after too many failed attempts
161 *
162 * @param array $server The $_SERVER array
163 */
164 public function handleFailedLogin($server)
165 {
166 $ip = $server['REMOTE_ADDR'];
167 $trusted = $this->configManager->get('security.trusted_proxies', []);
168
169 if (in_array($ip, $trusted)) {
170 $ip = getIpAddressFromProxy($server, $trusted);
171 if (! $ip) {
172 // the IP is behind a trusted forward proxy, but is not forwarded
173 // in the HTTP headers, so we do nothing
174 return;
175 }
176 }
177
178 // increment the fail count for this IP
179 if (isset($this->globals['IPBANS']['FAILURES'][$ip])) {
180 $this->globals['IPBANS']['FAILURES'][$ip]++;
181 } else {
182 $this->globals['IPBANS']['FAILURES'][$ip] = 1;
183 }
184
185 if ($this->globals['IPBANS']['FAILURES'][$ip] >= $this->configManager->get('security.ban_after')) {
186 $this->globals['IPBANS']['BANS'][$ip] = time() + $this->configManager->get('security.ban_duration', 1800);
187 logm(
188 $this->configManager->get('resource.log'),
189 $server['REMOTE_ADDR'],
190 'IP address banned from login'
191 );
192 }
193 $this->writeBanFile();
194 }
195
196 /**
197 * Handle a successful login
198 *
199 * @param array $server The $_SERVER array
200 */
201 public function handleSuccessfulLogin($server)
202 {
203 $ip = $server['REMOTE_ADDR'];
204 // FIXME unban when behind a trusted proxy?
205
206 unset($this->globals['IPBANS']['FAILURES'][$ip]);
207 unset($this->globals['IPBANS']['BANS'][$ip]);
208
209 $this->writeBanFile();
210 }
211
212 /**
213 * Check if the user can login from this IP
214 *
215 * @param array $server The $_SERVER array
216 *
217 * @return bool true if the user is allowed to login
218 */
219 public function canLogin($server)
220 {
221 $ip = $server['REMOTE_ADDR'];
222
223 if (! isset($this->globals['IPBANS']['BANS'][$ip])) {
224 // the user is not banned
225 return true;
226 }
227
228 if ($this->globals['IPBANS']['BANS'][$ip] > time()) {
229 // the user is still banned
230 return false;
231 }
232
233 // the ban has expired, the user can attempt to log in again
234 logm($this->configManager->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
235 unset($this->globals['IPBANS']['FAILURES'][$ip]);
236 unset($this->globals['IPBANS']['BANS'][$ip]);
237
238 $this->writeBanFile();
239 return true;
240 }
241 }