2 namespace Shaarli\Security
;
4 require_once 'tests/utils/FakeConfigManager.php';
6 use PHPUnit\Framework\TestCase
;
9 * Test coverage for LoginManager
11 class LoginManagerTest
extends TestCase
13 /** @var \FakeConfigManager Configuration Manager instance */
14 protected $configManager = null;
16 /** @var LoginManager Login Manager instance */
17 protected $loginManager = null;
19 /** @var SessionManager Session Manager instance */
20 protected $sessionManager = null;
22 /** @var string Banned IP filename */
23 protected $banFile = 'sandbox/ipbans.php';
25 /** @var string Log filename */
26 protected $logFile = 'sandbox/shaarli.log';
28 /** @var array Simulates the $_COOKIE array */
29 protected $cookie = [];
31 /** @var array Simulates the $GLOBALS array */
32 protected $globals = [];
34 /** @var array Simulates the $_SERVER array */
35 protected $server = [];
37 /** @var array Simulates the $_SESSION array */
38 protected $session = [];
40 /** @var string Advertised client IP address */
41 protected $clientIpAddress = '10.1.47.179';
43 /** @var string Local client IP address */
44 protected $ipAddr = '127.0.0.1';
46 /** @var string Trusted proxy IP address */
47 protected $trustedProxy = '10.1.1.100';
49 /** @var string User login */
50 protected $login = 'johndoe';
52 /** @var string User password */
53 protected $password = 'IC4nHazL0g1n?';
55 /** @var string Hash of the salted user password */
56 protected $passwordHash = '';
58 /** @var string Salt used by hash functions */
59 protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2';
62 * Prepare or reset test resources
64 public function setUp()
66 if (file_exists($this->banFile
)) {
67 unlink($this->banFile
);
70 $this->passwordHash
= sha1($this->password
. $this->login
. $this->salt
);
72 $this->configManager
= new \
FakeConfigManager([
73 'credentials.login' => $this->login
,
74 'credentials.hash' => $this->passwordHash
,
75 'credentials.salt' => $this->salt
,
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
],
85 $this->globals
= &$GLOBALS;
86 unset($this->globals
['IPBANS']);
90 $this->sessionManager
= new SessionManager($this->session
, $this->configManager
);
91 $this->loginManager
= new LoginManager($this->globals
, $this->configManager
, $this->sessionManager
);
92 $this->server
['REMOTE_ADDR'] = $this->ipAddr
;
98 public function tearDown()
100 unset($this->globals
['IPBANS']);
104 * Instantiate a LoginManager and load ban records
106 public function testReadBanFile()
110 "<?php\n\$GLOBALS['IPBANS']=array('FAILURES' => array('127.0.0.1' => 99));\n?>"
112 new LoginManager($this->globals
, $this->configManager
, null);
113 $this->assertEquals(99, $this->globals
['IPBANS']['FAILURES']['127.0.0.1']);
117 * Record a failed login attempt
119 public function testHandleFailedLogin()
121 $this->loginManager
->handleFailedLogin($this->server
);
122 $this->assertEquals(1, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
124 $this->loginManager
->handleFailedLogin($this->server
);
125 $this->assertEquals(2, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
129 * Record a failed login attempt - IP behind a trusted proxy
131 public function testHandleFailedLoginBehindTrustedProxy()
134 'REMOTE_ADDR' => $this->trustedProxy
,
135 'HTTP_X_FORWARDED_FOR' => $this->ipAddr
,
137 $this->loginManager
->handleFailedLogin($server);
138 $this->assertEquals(1, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
140 $this->loginManager
->handleFailedLogin($server);
141 $this->assertEquals(2, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
145 * Record a failed login attempt - IP behind a trusted proxy but not forwarded
147 public function testHandleFailedLoginBehindTrustedProxyNoIp()
150 'REMOTE_ADDR' => $this->trustedProxy
,
152 $this->loginManager
->handleFailedLogin($server);
153 $this->assertFalse(isset($this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]));
155 $this->loginManager
->handleFailedLogin($server);
156 $this->assertFalse(isset($this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]));
160 * Record a failed login attempt and ban the IP after too many failures
162 public function testHandleFailedLoginBanIp()
164 $this->loginManager
->handleFailedLogin($this->server
);
165 $this->assertEquals(1, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
166 $this->assertTrue($this->loginManager
->canLogin($this->server
));
168 $this->loginManager
->handleFailedLogin($this->server
);
169 $this->assertEquals(2, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
170 $this->assertTrue($this->loginManager
->canLogin($this->server
));
172 $this->loginManager
->handleFailedLogin($this->server
);
173 $this->assertEquals(3, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
174 $this->assertTrue($this->loginManager
->canLogin($this->server
));
176 $this->loginManager
->handleFailedLogin($this->server
);
177 $this->assertEquals(4, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
178 $this->assertFalse($this->loginManager
->canLogin($this->server
));
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
));
191 public function testHandleSuccessfulLogin()
193 $this->assertTrue($this->loginManager
->canLogin($this->server
));
195 $this->loginManager
->handleSuccessfulLogin($this->server
);
196 $this->assertTrue($this->loginManager
->canLogin($this->server
));
200 * Erase failure records after successfully logging in from this IP
202 public function testHandleSuccessfulLoginAfterFailure()
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
));
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
]));
216 * The IP is not banned
218 public function testCanLoginIpNotBanned()
220 $this->assertTrue($this->loginManager
->canLogin($this->server
));
226 public function testCanLoginIpBanned()
228 // ban the IP for an hour
229 $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
] = 10;
230 $this->globals
['IPBANS']['BANS'][$this->ipAddr
] = time() +
3600;
232 $this->assertFalse($this->loginManager
->canLogin($this->server
));
236 * The IP is banned, and the ban duration is over
238 public function testCanLoginIpBanExpired()
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
));
246 $this->globals
['IPBANS']['BANS'][$this->ipAddr
] = time() - 3600;
247 $this->assertTrue($this->loginManager
->canLogin($this->server
));
251 * Generate a token depending on the user credentials and client IP
253 public function testGenerateStaySignedInToken()
255 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
258 sha1($this->passwordHash
. $this->clientIpAddress
. $this->salt
),
259 $this->loginManager
->getStaySignedInToken()
264 * Check user login - Shaarli has not yet been configured
266 public function testCheckLoginStateNotConfigured()
268 $configManager = new \
FakeConfigManager([
269 'resource.ban_file' => $this->banFile
,
271 $loginManager = new LoginManager($this->globals
, $configManager, null);
272 $loginManager->checkLoginState([], '');
274 $this->assertFalse($loginManager->isLoggedIn());
278 * Check user login - the client cookie does not match the server token
280 public function testCheckLoginStateStaySignedInWithInvalidToken()
282 // simulate a previous login
284 'ip' => $this->clientIpAddress
,
285 'expires_on' => time() +
100,
287 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
288 $this->cookie
[LoginManager
::$STAY_SIGNED_IN_COOKIE] = 'nope';
290 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
292 $this->assertTrue($this->loginManager
->isLoggedIn());
293 $this->assertTrue(empty($this->session
['username']));
297 * Check user login - the client cookie matches the server token
299 public function testCheckLoginStateStaySignedInWithValidToken()
301 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
302 $this->cookie
[LoginManager
::$STAY_SIGNED_IN_COOKIE] = $this->loginManager
->getStaySignedInToken();
304 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
306 $this->assertTrue($this->loginManager
->isLoggedIn());
307 $this->assertEquals($this->login
, $this->session
['username']);
308 $this->assertEquals($this->clientIpAddress
, $this->session
['ip']);
312 * Check user login - the session has expired
314 public function testCheckLoginStateSessionExpired()
316 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
317 $this->session
['expires_on'] = time() - 100;
319 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
321 $this->assertFalse($this->loginManager
->isLoggedIn());
325 * Check user login - the remote client IP has changed
327 public function testCheckLoginStateClientIpChanged()
329 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
331 $this->loginManager
->checkLoginState($this->cookie
, '10.7.157.98');
333 $this->assertFalse($this->loginManager
->isLoggedIn());
337 * Check user credentials - wrong login supplied
339 public function testCheckCredentialsWrongLogin()
342 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', $this->password
)
347 * Check user credentials - wrong password supplied
349 public function testCheckCredentialsWrongPassword()
352 $this->loginManager
->checkCredentials('', '', $this->login
, 'b4dp455wd')
357 * Check user credentials - wrong login and password supplied
359 public function testCheckCredentialsWrongLoginAndPassword()
362 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
367 * Check user credentials - correct login and password supplied
369 public function testCheckCredentialsGoodLoginAndPassword()
372 $this->loginManager
->checkCredentials('', '', $this->login
, $this->password
)