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