]>
Commit | Line | Data |
---|---|---|
44acf706 | 1 | <?php |
44acf706 | 2 | |
c4ad3d4f | 3 | namespace Shaarli\Security; |
dea72c71 | 4 | |
a5a9cf23 | 5 | use Shaarli\TestCase; |
44acf706 V |
6 | |
7 | /** | |
8 | * Test coverage for LoginManager | |
9 | */ | |
10 | class 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 | ||
c4ad3d4f A |
60 | /** @var CookieManager */ |
61 | protected $cookieManager; | |
62 | ||
44acf706 V |
63 | /** |
64 | * Prepare or reset test resources | |
65 | */ | |
8f60e120 | 66 | protected function setUp(): void |
44acf706 V |
67 | { |
68 | if (file_exists($this->banFile)) { | |
69 | unlink($this->banFile); | |
70 | } | |
71 | ||
c689e108 V |
72 | $this->passwordHash = sha1($this->password . $this->login . $this->salt); |
73 | ||
44acf706 | 74 | $this->configManager = new \FakeConfigManager([ |
c689e108 V |
75 | 'credentials.login' => $this->login, |
76 | 'credentials.hash' => $this->passwordHash, | |
77 | 'credentials.salt' => $this->salt, | |
44acf706 V |
78 | 'resource.ban_file' => $this->banFile, |
79 | 'resource.log' => $this->logFile, | |
b49a04f7 | 80 | 'security.ban_after' => 2, |
44acf706 V |
81 | 'security.ban_duration' => 3600, |
82 | 'security.trusted_proxies' => [$this->trustedProxy], | |
cc2ded54 | 83 | 'ldap.host' => '', |
44acf706 V |
84 | ]); |
85 | ||
704637bf | 86 | $this->cookie = []; |
8edd7f15 | 87 | $this->session = []; |
704637bf | 88 | |
c4ad3d4f A |
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); | |
44acf706 V |
95 | $this->server['REMOTE_ADDR'] = $this->ipAddr; |
96 | } | |
97 | ||
44acf706 V |
98 | /** |
99 | * Record a failed login attempt | |
100 | */ | |
101 | public function testHandleFailedLogin() | |
102 | { | |
103 | $this->loginManager->handleFailedLogin($this->server); | |
44acf706 | 104 | $this->loginManager->handleFailedLogin($this->server); |
b49a04f7 | 105 | $this->assertFalse($this->loginManager->canLogin($this->server)); |
44acf706 V |
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); | |
44acf706 | 118 | $this->loginManager->handleFailedLogin($server); |
b49a04f7 | 119 | $this->assertFalse($this->loginManager->canLogin($server)); |
44acf706 V |
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); | |
44acf706 | 131 | $this->loginManager->handleFailedLogin($server); |
b49a04f7 | 132 | $this->assertTrue($this->loginManager->canLogin($server)); |
44acf706 V |
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); | |
44acf706 V |
152 | $this->assertTrue($this->loginManager->canLogin($this->server)); |
153 | ||
154 | $this->loginManager->handleSuccessfulLogin($this->server); | |
b49a04f7 | 155 | $this->loginManager->handleFailedLogin($this->server); |
44acf706 | 156 | $this->assertTrue($this->loginManager->canLogin($this->server)); |
44acf706 V |
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 | ||
c689e108 V |
167 | /** |
168 | * Generate a token depending on the user credentials and client IP | |
169 | */ | |
170 | public function testGenerateStaySignedInToken() | |
171 | { | |
704637bf | 172 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
c689e108 V |
173 | |
174 | $this->assertEquals( | |
704637bf | 175 | sha1($this->passwordHash . $this->clientIpAddress . $this->salt), |
c689e108 V |
176 | $this->loginManager->getStaySignedInToken() |
177 | ); | |
178 | } | |
704637bf | 179 | |
d9ba1cdd A |
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 | ||
704637bf V |
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 | ]); | |
c4ad3d4f A |
202 | $loginManager = new LoginManager($configManager, null, $this->cookieManager); |
203 | $loginManager->checkLoginState(''); | |
704637bf V |
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 | { | |
8edd7f15 V |
213 | // simulate a previous login |
214 | $this->session = [ | |
215 | 'ip' => $this->clientIpAddress, | |
216 | 'expires_on' => time() + 100, | |
217 | ]; | |
704637bf | 218 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
c4ad3d4f | 219 | $this->cookie[CookieManager::STAY_SIGNED_IN] = 'nope'; |
704637bf | 220 | |
c4ad3d4f | 221 | $this->loginManager->checkLoginState($this->clientIpAddress); |
704637bf | 222 | |
8edd7f15 V |
223 | $this->assertTrue($this->loginManager->isLoggedIn()); |
224 | $this->assertTrue(empty($this->session['username'])); | |
704637bf V |
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); | |
c4ad3d4f | 233 | $this->cookie[CookieManager::STAY_SIGNED_IN] = $this->loginManager->getStaySignedInToken(); |
704637bf | 234 | |
c4ad3d4f | 235 | $this->loginManager->checkLoginState($this->clientIpAddress); |
704637bf V |
236 | |
237 | $this->assertTrue($this->loginManager->isLoggedIn()); | |
8edd7f15 V |
238 | $this->assertEquals($this->login, $this->session['username']); |
239 | $this->assertEquals($this->clientIpAddress, $this->session['ip']); | |
704637bf V |
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 | ||
c4ad3d4f | 250 | $this->loginManager->checkLoginState($this->clientIpAddress); |
704637bf V |
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 | ||
c4ad3d4f | 262 | $this->loginManager->checkLoginState('10.7.157.98'); |
704637bf V |
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 | } | |
cc2ded54 SN |
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 | { | |
46846fd4 | 323 | $this->configManager->set('ldap.host', 'dummy'); |
cc2ded54 SN |
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 | } | |
44acf706 | 339 | } |