]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/security/LoginManagerTest.php
Optimize and cleanup imports
[github/shaarli/Shaarli.git] / tests / security / LoginManagerTest.php
CommitLineData
44acf706 1<?php
fab87c26 2namespace Shaarli\Security;
44acf706
V
3
4require_once 'tests/utils/FakeConfigManager.php';
dea72c71
V
5
6use PHPUnit\Framework\TestCase;
44acf706
V
7
8/**
9 * Test coverage for LoginManager
10 */
11class LoginManagerTest extends TestCase
12{
704637bf 13 /** @var \FakeConfigManager Configuration Manager instance */
44acf706 14 protected $configManager = null;
704637bf
V
15
16 /** @var LoginManager Login Manager instance */
44acf706 17 protected $loginManager = null;
704637bf
V
18
19 /** @var SessionManager Session Manager instance */
20 protected $sessionManager = null;
21
22 /** @var string Banned IP filename */
44acf706 23 protected $banFile = 'sandbox/ipbans.php';
704637bf
V
24
25 /** @var string Log filename */
44acf706 26 protected $logFile = 'sandbox/shaarli.log';
704637bf
V
27
28 /** @var array Simulates the $_COOKIE array */
29 protected $cookie = [];
30
31 /** @var array Simulates the $GLOBALS array */
44acf706 32 protected $globals = [];
704637bf
V
33
34 /** @var array Simulates the $_SERVER array */
44acf706 35 protected $server = [];
704637bf
V
36
37 /** @var array Simulates the $_SESSION array */
38 protected $session = [];
39
40 /** @var string Advertised client IP address */
41 protected $clientIpAddress = '10.1.47.179';
42
43 /** @var string Local client IP address */
44 protected $ipAddr = '127.0.0.1';
45
46 /** @var string Trusted proxy IP address */
44acf706
V
47 protected $trustedProxy = '10.1.1.100';
48
c689e108
V
49 /** @var string User login */
50 protected $login = 'johndoe';
51
52 /** @var string User password */
53 protected $password = 'IC4nHazL0g1n?';
54
55 /** @var string Hash of the salted user password */
56 protected $passwordHash = '';
57
58 /** @var string Salt used by hash functions */
59 protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2';
60
44acf706
V
61 /**
62 * Prepare or reset test resources
63 */
64 public function setUp()
65 {
66 if (file_exists($this->banFile)) {
67 unlink($this->banFile);
68 }
69
c689e108
V
70 $this->passwordHash = sha1($this->password . $this->login . $this->salt);
71
44acf706 72 $this->configManager = new \FakeConfigManager([
c689e108
V
73 'credentials.login' => $this->login,
74 'credentials.hash' => $this->passwordHash,
75 'credentials.salt' => $this->salt,
44acf706
V
76 'resource.ban_file' => $this->banFile,
77 'resource.log' => $this->logFile,
78 'security.ban_after' => 4,
79 'security.ban_duration' => 3600,
80 'security.trusted_proxies' => [$this->trustedProxy],
81 ]);
82
704637bf
V
83 $this->cookie = [];
84
44acf706
V
85 $this->globals = &$GLOBALS;
86 unset($this->globals['IPBANS']);
87
8edd7f15 88 $this->session = [];
704637bf
V
89
90 $this->sessionManager = new SessionManager($this->session, $this->configManager);
91 $this->loginManager = new LoginManager($this->globals, $this->configManager, $this->sessionManager);
44acf706
V
92 $this->server['REMOTE_ADDR'] = $this->ipAddr;
93 }
94
95 /**
96 * Wipe test resources
97 */
98 public function tearDown()
99 {
100 unset($this->globals['IPBANS']);
101 }
102
103 /**
104 * Instantiate a LoginManager and load ban records
105 */
106 public function testReadBanFile()
107 {
108 file_put_contents(
109 $this->banFile,
110 "<?php\n\$GLOBALS['IPBANS']=array('FAILURES' => array('127.0.0.1' => 99));\n?>"
111 );
63ea23c2 112 new LoginManager($this->globals, $this->configManager, null);
44acf706
V
113 $this->assertEquals(99, $this->globals['IPBANS']['FAILURES']['127.0.0.1']);
114 }
115
116 /**
117 * Record a failed login attempt
118 */
119 public function testHandleFailedLogin()
120 {
121 $this->loginManager->handleFailedLogin($this->server);
122 $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
123
124 $this->loginManager->handleFailedLogin($this->server);
125 $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
126 }
127
128 /**
129 * Record a failed login attempt - IP behind a trusted proxy
130 */
131 public function testHandleFailedLoginBehindTrustedProxy()
132 {
133 $server = [
134 'REMOTE_ADDR' => $this->trustedProxy,
135 'HTTP_X_FORWARDED_FOR' => $this->ipAddr,
136 ];
137 $this->loginManager->handleFailedLogin($server);
138 $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
139
140 $this->loginManager->handleFailedLogin($server);
141 $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
142 }
143
144 /**
145 * Record a failed login attempt - IP behind a trusted proxy but not forwarded
146 */
147 public function testHandleFailedLoginBehindTrustedProxyNoIp()
148 {
149 $server = [
150 'REMOTE_ADDR' => $this->trustedProxy,
151 ];
152 $this->loginManager->handleFailedLogin($server);
153 $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
154
155 $this->loginManager->handleFailedLogin($server);
156 $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
157 }
158
159 /**
160 * Record a failed login attempt and ban the IP after too many failures
161 */
162 public function testHandleFailedLoginBanIp()
163 {
164 $this->loginManager->handleFailedLogin($this->server);
165 $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
166 $this->assertTrue($this->loginManager->canLogin($this->server));
167
168 $this->loginManager->handleFailedLogin($this->server);
169 $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
170 $this->assertTrue($this->loginManager->canLogin($this->server));
171
172 $this->loginManager->handleFailedLogin($this->server);
173 $this->assertEquals(3, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
174 $this->assertTrue($this->loginManager->canLogin($this->server));
175
176 $this->loginManager->handleFailedLogin($this->server);
177 $this->assertEquals(4, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
178 $this->assertFalse($this->loginManager->canLogin($this->server));
179
180 // handleFailedLogin is not supposed to be called at this point:
181 // - no login form should be displayed once an IP has been banned
182 // - yet this could happen when using custom templates / scripts
183 $this->loginManager->handleFailedLogin($this->server);
184 $this->assertEquals(5, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
185 $this->assertFalse($this->loginManager->canLogin($this->server));
186 }
187
188 /**
189 * Nothing to do
190 */
191 public function testHandleSuccessfulLogin()
192 {
193 $this->assertTrue($this->loginManager->canLogin($this->server));
194
195 $this->loginManager->handleSuccessfulLogin($this->server);
196 $this->assertTrue($this->loginManager->canLogin($this->server));
197 }
198
199 /**
200 * Erase failure records after successfully logging in from this IP
201 */
202 public function testHandleSuccessfulLoginAfterFailure()
203 {
204 $this->loginManager->handleFailedLogin($this->server);
205 $this->loginManager->handleFailedLogin($this->server);
206 $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
207 $this->assertTrue($this->loginManager->canLogin($this->server));
208
209 $this->loginManager->handleSuccessfulLogin($this->server);
210 $this->assertTrue($this->loginManager->canLogin($this->server));
211 $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
212 $this->assertFalse(isset($this->globals['IPBANS']['BANS'][$this->ipAddr]));
213 }
214
215 /**
216 * The IP is not banned
217 */
218 public function testCanLoginIpNotBanned()
219 {
220 $this->assertTrue($this->loginManager->canLogin($this->server));
221 }
222
223 /**
224 * The IP is banned
225 */
226 public function testCanLoginIpBanned()
227 {
228 // ban the IP for an hour
229 $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10;
230 $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600;
231
232 $this->assertFalse($this->loginManager->canLogin($this->server));
233 }
234
235 /**
236 * The IP is banned, and the ban duration is over
237 */
238 public function testCanLoginIpBanExpired()
239 {
240 // ban the IP for an hour
241 $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10;
242 $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600;
243 $this->assertFalse($this->loginManager->canLogin($this->server));
244
245 // lift the ban
246 $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() - 3600;
247 $this->assertTrue($this->loginManager->canLogin($this->server));
248 }
c689e108
V
249
250 /**
251 * Generate a token depending on the user credentials and client IP
252 */
253 public function testGenerateStaySignedInToken()
254 {
704637bf 255 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
c689e108
V
256
257 $this->assertEquals(
704637bf 258 sha1($this->passwordHash . $this->clientIpAddress . $this->salt),
c689e108
V
259 $this->loginManager->getStaySignedInToken()
260 );
261 }
704637bf
V
262
263 /**
264 * Check user login - Shaarli has not yet been configured
265 */
266 public function testCheckLoginStateNotConfigured()
267 {
268 $configManager = new \FakeConfigManager([
269 'resource.ban_file' => $this->banFile,
270 ]);
271 $loginManager = new LoginManager($this->globals, $configManager, null);
272 $loginManager->checkLoginState([], '');
273
274 $this->assertFalse($loginManager->isLoggedIn());
275 }
276
277 /**
278 * Check user login - the client cookie does not match the server token
279 */
280 public function testCheckLoginStateStaySignedInWithInvalidToken()
281 {
8edd7f15
V
282 // simulate a previous login
283 $this->session = [
284 'ip' => $this->clientIpAddress,
285 'expires_on' => time() + 100,
286 ];
704637bf
V
287 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
288 $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope';
289
290 $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
291
8edd7f15
V
292 $this->assertTrue($this->loginManager->isLoggedIn());
293 $this->assertTrue(empty($this->session['username']));
704637bf
V
294 }
295
296 /**
297 * Check user login - the client cookie matches the server token
298 */
299 public function testCheckLoginStateStaySignedInWithValidToken()
300 {
301 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
302 $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken();
303
304 $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
305
306 $this->assertTrue($this->loginManager->isLoggedIn());
8edd7f15
V
307 $this->assertEquals($this->login, $this->session['username']);
308 $this->assertEquals($this->clientIpAddress, $this->session['ip']);
704637bf
V
309 }
310
311 /**
312 * Check user login - the session has expired
313 */
314 public function testCheckLoginStateSessionExpired()
315 {
316 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
317 $this->session['expires_on'] = time() - 100;
318
319 $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
320
321 $this->assertFalse($this->loginManager->isLoggedIn());
322 }
323
324 /**
325 * Check user login - the remote client IP has changed
326 */
327 public function testCheckLoginStateClientIpChanged()
328 {
329 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
330
331 $this->loginManager->checkLoginState($this->cookie, '10.7.157.98');
332
333 $this->assertFalse($this->loginManager->isLoggedIn());
334 }
335
336 /**
337 * Check user credentials - wrong login supplied
338 */
339 public function testCheckCredentialsWrongLogin()
340 {
341 $this->assertFalse(
342 $this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password)
343 );
344 }
345
346 /**
347 * Check user credentials - wrong password supplied
348 */
349 public function testCheckCredentialsWrongPassword()
350 {
351 $this->assertFalse(
352 $this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd')
353 );
354 }
355
356 /**
357 * Check user credentials - wrong login and password supplied
358 */
359 public function testCheckCredentialsWrongLoginAndPassword()
360 {
361 $this->assertFalse(
362 $this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
363 );
364 }
365
366 /**
367 * Check user credentials - correct login and password supplied
368 */
369 public function testCheckCredentialsGoodLoginAndPassword()
370 {
371 $this->assertTrue(
372 $this->loginManager->checkCredentials('', '', $this->login, $this->password)
373 );
374 }
44acf706 375}