]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/security/LoginManagerTest.php
Merge branch 'links_per_page'
[github/shaarli/Shaarli.git] / tests / security / LoginManagerTest.php
1 <?php
2
3 namespace Shaarli\Security;
4
5 use PHPUnit\Framework\TestCase;
6
7 /**
8 * Test coverage for LoginManager
9 */
10 class LoginManagerTest extends TestCase
11 {
12 /** @var \FakeConfigManager Configuration Manager instance */
13 protected $configManager = null;
14
15 /** @var LoginManager Login Manager instance */
16 protected $loginManager = null;
17
18 /** @var SessionManager Session Manager instance */
19 protected $sessionManager = null;
20
21 /** @var string Banned IP filename */
22 protected $banFile = 'sandbox/ipbans.php';
23
24 /** @var string Log filename */
25 protected $logFile = 'sandbox/shaarli.log';
26
27 /** @var array Simulates the $_COOKIE array */
28 protected $cookie = [];
29
30 /** @var array Simulates the $GLOBALS array */
31 protected $globals = [];
32
33 /** @var array Simulates the $_SERVER array */
34 protected $server = [];
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 */
46 protected $trustedProxy = '10.1.1.100';
47
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
60 /** @var CookieManager */
61 protected $cookieManager;
62
63 /**
64 * Prepare or reset test resources
65 */
66 public function setUp()
67 {
68 if (file_exists($this->banFile)) {
69 unlink($this->banFile);
70 }
71
72 $this->passwordHash = sha1($this->password . $this->login . $this->salt);
73
74 $this->configManager = new \FakeConfigManager([
75 'credentials.login' => $this->login,
76 'credentials.hash' => $this->passwordHash,
77 'credentials.salt' => $this->salt,
78 'resource.ban_file' => $this->banFile,
79 'resource.log' => $this->logFile,
80 'security.ban_after' => 2,
81 'security.ban_duration' => 3600,
82 'security.trusted_proxies' => [$this->trustedProxy],
83 'ldap.host' => '',
84 ]);
85
86 $this->cookie = [];
87 $this->session = [];
88
89 $this->cookieManager = $this->createMock(CookieManager::class);
90 $this->cookieManager->method('getCookieParameter')->willReturnCallback(function (string $key) {
91 return $this->cookie[$key] ?? null;
92 });
93 $this->sessionManager = new SessionManager($this->session, $this->configManager, 'session_path');
94 $this->loginManager = new LoginManager($this->configManager, $this->sessionManager, $this->cookieManager);
95 $this->server['REMOTE_ADDR'] = $this->ipAddr;
96 }
97
98 /**
99 * Record a failed login attempt
100 */
101 public function testHandleFailedLogin()
102 {
103 $this->loginManager->handleFailedLogin($this->server);
104 $this->loginManager->handleFailedLogin($this->server);
105 $this->assertFalse($this->loginManager->canLogin($this->server));
106 }
107
108 /**
109 * Record a failed login attempt - IP behind a trusted proxy
110 */
111 public function testHandleFailedLoginBehindTrustedProxy()
112 {
113 $server = [
114 'REMOTE_ADDR' => $this->trustedProxy,
115 'HTTP_X_FORWARDED_FOR' => $this->ipAddr,
116 ];
117 $this->loginManager->handleFailedLogin($server);
118 $this->loginManager->handleFailedLogin($server);
119 $this->assertFalse($this->loginManager->canLogin($server));
120 }
121
122 /**
123 * Record a failed login attempt - IP behind a trusted proxy but not forwarded
124 */
125 public function testHandleFailedLoginBehindTrustedProxyNoIp()
126 {
127 $server = [
128 'REMOTE_ADDR' => $this->trustedProxy,
129 ];
130 $this->loginManager->handleFailedLogin($server);
131 $this->loginManager->handleFailedLogin($server);
132 $this->assertTrue($this->loginManager->canLogin($server));
133 }
134
135 /**
136 * Nothing to do
137 */
138 public function testHandleSuccessfulLogin()
139 {
140 $this->assertTrue($this->loginManager->canLogin($this->server));
141
142 $this->loginManager->handleSuccessfulLogin($this->server);
143 $this->assertTrue($this->loginManager->canLogin($this->server));
144 }
145
146 /**
147 * Erase failure records after successfully logging in from this IP
148 */
149 public function testHandleSuccessfulLoginAfterFailure()
150 {
151 $this->loginManager->handleFailedLogin($this->server);
152 $this->assertTrue($this->loginManager->canLogin($this->server));
153
154 $this->loginManager->handleSuccessfulLogin($this->server);
155 $this->loginManager->handleFailedLogin($this->server);
156 $this->assertTrue($this->loginManager->canLogin($this->server));
157 }
158
159 /**
160 * The IP is not banned
161 */
162 public function testCanLoginIpNotBanned()
163 {
164 $this->assertTrue($this->loginManager->canLogin($this->server));
165 }
166
167 /**
168 * Generate a token depending on the user credentials and client IP
169 */
170 public function testGenerateStaySignedInToken()
171 {
172 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
173
174 $this->assertEquals(
175 sha1($this->passwordHash . $this->clientIpAddress . $this->salt),
176 $this->loginManager->getStaySignedInToken()
177 );
178 }
179
180 /**
181 * Generate a token depending on the user credentials with session protected disabled
182 */
183 public function testGenerateStaySignedInTokenSessionProtectionDisabled()
184 {
185 $this->configManager->set('security.session_protection_disabled', true);
186 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
187
188 $this->assertEquals(
189 sha1($this->passwordHash . $this->salt),
190 $this->loginManager->getStaySignedInToken()
191 );
192 }
193
194 /**
195 * Check user login - Shaarli has not yet been configured
196 */
197 public function testCheckLoginStateNotConfigured()
198 {
199 $configManager = new \FakeConfigManager([
200 'resource.ban_file' => $this->banFile,
201 ]);
202 $loginManager = new LoginManager($configManager, null, $this->cookieManager);
203 $loginManager->checkLoginState('');
204
205 $this->assertFalse($loginManager->isLoggedIn());
206 }
207
208 /**
209 * Check user login - the client cookie does not match the server token
210 */
211 public function testCheckLoginStateStaySignedInWithInvalidToken()
212 {
213 // simulate a previous login
214 $this->session = [
215 'ip' => $this->clientIpAddress,
216 'expires_on' => time() + 100,
217 ];
218 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
219 $this->cookie[CookieManager::STAY_SIGNED_IN] = 'nope';
220
221 $this->loginManager->checkLoginState($this->clientIpAddress);
222
223 $this->assertTrue($this->loginManager->isLoggedIn());
224 $this->assertTrue(empty($this->session['username']));
225 }
226
227 /**
228 * Check user login - the client cookie matches the server token
229 */
230 public function testCheckLoginStateStaySignedInWithValidToken()
231 {
232 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
233 $this->cookie[CookieManager::STAY_SIGNED_IN] = $this->loginManager->getStaySignedInToken();
234
235 $this->loginManager->checkLoginState($this->clientIpAddress);
236
237 $this->assertTrue($this->loginManager->isLoggedIn());
238 $this->assertEquals($this->login, $this->session['username']);
239 $this->assertEquals($this->clientIpAddress, $this->session['ip']);
240 }
241
242 /**
243 * Check user login - the session has expired
244 */
245 public function testCheckLoginStateSessionExpired()
246 {
247 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
248 $this->session['expires_on'] = time() - 100;
249
250 $this->loginManager->checkLoginState($this->clientIpAddress);
251
252 $this->assertFalse($this->loginManager->isLoggedIn());
253 }
254
255 /**
256 * Check user login - the remote client IP has changed
257 */
258 public function testCheckLoginStateClientIpChanged()
259 {
260 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
261
262 $this->loginManager->checkLoginState('10.7.157.98');
263
264 $this->assertFalse($this->loginManager->isLoggedIn());
265 }
266
267 /**
268 * Check user credentials - wrong login supplied
269 */
270 public function testCheckCredentialsWrongLogin()
271 {
272 $this->assertFalse(
273 $this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password)
274 );
275 }
276
277 /**
278 * Check user credentials - wrong password supplied
279 */
280 public function testCheckCredentialsWrongPassword()
281 {
282 $this->assertFalse(
283 $this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd')
284 );
285 }
286
287 /**
288 * Check user credentials - wrong login and password supplied
289 */
290 public function testCheckCredentialsWrongLoginAndPassword()
291 {
292 $this->assertFalse(
293 $this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
294 );
295 }
296
297 /**
298 * Check user credentials - correct login and password supplied
299 */
300 public function testCheckCredentialsGoodLoginAndPassword()
301 {
302 $this->assertTrue(
303 $this->loginManager->checkCredentials('', '', $this->login, $this->password)
304 );
305 }
306
307 /**
308 * Check user credentials through LDAP - server unreachable
309 */
310 public function testCheckCredentialsFromUnreachableLdap()
311 {
312 $this->configManager->set('ldap.host', 'dummy');
313 $this->assertFalse(
314 $this->loginManager->checkCredentials('', '', $this->login, $this->password)
315 );
316 }
317
318 /**
319 * Check user credentials through LDAP - wrong login and password supplied
320 */
321 public function testCheckCredentialsFromLdapWrongLoginAndPassword()
322 {
323 $this->configManager->set('ldap.host', 'dummy');
324 $this->assertFalse(
325 $this->loginManager->checkCredentialsFromLdap($this->login, $this->password, function() { return null; }, function() { return false; })
326 );
327 }
328
329 /**
330 * Check user credentials through LDAP - correct login and password supplied
331 */
332 public function testCheckCredentialsFromLdapGoodLoginAndPassword()
333 {
334 $this->configManager->set('ldap.host', 'dummy');
335 $this->assertTrue(
336 $this->loginManager->checkCredentialsFromLdap($this->login, $this->password, function() { return null; }, function() { return true; })
337 );
338 }
339 }