2 namespace Shaarli\Security
;
4 require_once 'tests/utils/FakeConfigManager.php';
5 use \PHPUnit\Framework\TestCase
;
8 * Test coverage for LoginManager
10 class LoginManagerTest
extends TestCase
12 /** @var \FakeConfigManager Configuration Manager instance */
13 protected $configManager = null;
15 /** @var LoginManager Login Manager instance */
16 protected $loginManager = null;
18 /** @var SessionManager Session Manager instance */
19 protected $sessionManager = null;
21 /** @var string Banned IP filename */
22 protected $banFile = 'sandbox/ipbans.php';
24 /** @var string Log filename */
25 protected $logFile = 'sandbox/shaarli.log';
27 /** @var array Simulates the $_COOKIE array */
28 protected $cookie = [];
30 /** @var array Simulates the $GLOBALS array */
31 protected $globals = [];
33 /** @var array Simulates the $_SERVER array */
34 protected $server = [];
36 /** @var array Simulates the $_SESSION array */
37 protected $session = [];
39 /** @var string Advertised client IP address */
40 protected $clientIpAddress = '10.1.47.179';
42 /** @var string Local client IP address */
43 protected $ipAddr = '127.0.0.1';
45 /** @var string Trusted proxy IP address */
46 protected $trustedProxy = '10.1.1.100';
48 /** @var string User login */
49 protected $login = 'johndoe';
51 /** @var string User password */
52 protected $password = 'IC4nHazL0g1n?';
54 /** @var string Hash of the salted user password */
55 protected $passwordHash = '';
57 /** @var string Salt used by hash functions */
58 protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2';
61 * Prepare or reset test resources
63 public function setUp()
65 if (file_exists($this->banFile
)) {
66 unlink($this->banFile
);
69 $this->passwordHash
= sha1($this->password
. $this->login
. $this->salt
);
71 $this->configManager
= new \
FakeConfigManager([
72 'credentials.login' => $this->login
,
73 'credentials.hash' => $this->passwordHash
,
74 'credentials.salt' => $this->salt
,
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
],
84 $this->globals
= &$GLOBALS;
85 unset($this->globals
['IPBANS']);
88 'expires_on' => time() +
100,
89 'ip' => $this->clientIpAddress
,
92 $this->sessionManager
= new SessionManager($this->session
, $this->configManager
);
93 $this->loginManager
= new LoginManager($this->globals
, $this->configManager
, $this->sessionManager
);
94 $this->server
['REMOTE_ADDR'] = $this->ipAddr
;
100 public function tearDown()
102 unset($this->globals
['IPBANS']);
106 * Instantiate a LoginManager and load ban records
108 public function testReadBanFile()
112 "<?php\n\$GLOBALS['IPBANS']=array('FAILURES' => array('127.0.0.1' => 99));\n?>"
114 new LoginManager($this->globals
, $this->configManager
, null);
115 $this->assertEquals(99, $this->globals
['IPBANS']['FAILURES']['127.0.0.1']);
119 * Record a failed login attempt
121 public function testHandleFailedLogin()
123 $this->loginManager
->handleFailedLogin($this->server
);
124 $this->assertEquals(1, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
126 $this->loginManager
->handleFailedLogin($this->server
);
127 $this->assertEquals(2, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
131 * Record a failed login attempt - IP behind a trusted proxy
133 public function testHandleFailedLoginBehindTrustedProxy()
136 'REMOTE_ADDR' => $this->trustedProxy
,
137 'HTTP_X_FORWARDED_FOR' => $this->ipAddr
,
139 $this->loginManager
->handleFailedLogin($server);
140 $this->assertEquals(1, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
142 $this->loginManager
->handleFailedLogin($server);
143 $this->assertEquals(2, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
147 * Record a failed login attempt - IP behind a trusted proxy but not forwarded
149 public function testHandleFailedLoginBehindTrustedProxyNoIp()
152 'REMOTE_ADDR' => $this->trustedProxy
,
154 $this->loginManager
->handleFailedLogin($server);
155 $this->assertFalse(isset($this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]));
157 $this->loginManager
->handleFailedLogin($server);
158 $this->assertFalse(isset($this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]));
162 * Record a failed login attempt and ban the IP after too many failures
164 public function testHandleFailedLoginBanIp()
166 $this->loginManager
->handleFailedLogin($this->server
);
167 $this->assertEquals(1, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
168 $this->assertTrue($this->loginManager
->canLogin($this->server
));
170 $this->loginManager
->handleFailedLogin($this->server
);
171 $this->assertEquals(2, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
172 $this->assertTrue($this->loginManager
->canLogin($this->server
));
174 $this->loginManager
->handleFailedLogin($this->server
);
175 $this->assertEquals(3, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
176 $this->assertTrue($this->loginManager
->canLogin($this->server
));
178 $this->loginManager
->handleFailedLogin($this->server
);
179 $this->assertEquals(4, $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
]);
180 $this->assertFalse($this->loginManager
->canLogin($this->server
));
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
));
193 public function testHandleSuccessfulLogin()
195 $this->assertTrue($this->loginManager
->canLogin($this->server
));
197 $this->loginManager
->handleSuccessfulLogin($this->server
);
198 $this->assertTrue($this->loginManager
->canLogin($this->server
));
202 * Erase failure records after successfully logging in from this IP
204 public function testHandleSuccessfulLoginAfterFailure()
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
));
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
]));
218 * The IP is not banned
220 public function testCanLoginIpNotBanned()
222 $this->assertTrue($this->loginManager
->canLogin($this->server
));
228 public function testCanLoginIpBanned()
230 // ban the IP for an hour
231 $this->globals
['IPBANS']['FAILURES'][$this->ipAddr
] = 10;
232 $this->globals
['IPBANS']['BANS'][$this->ipAddr
] = time() +
3600;
234 $this->assertFalse($this->loginManager
->canLogin($this->server
));
238 * The IP is banned, and the ban duration is over
240 public function testCanLoginIpBanExpired()
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
));
248 $this->globals
['IPBANS']['BANS'][$this->ipAddr
] = time() - 3600;
249 $this->assertTrue($this->loginManager
->canLogin($this->server
));
253 * Generate a token depending on the user credentials and client IP
255 public function testGenerateStaySignedInToken()
257 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
260 sha1($this->passwordHash
. $this->clientIpAddress
. $this->salt
),
261 $this->loginManager
->getStaySignedInToken()
266 * Check user login - Shaarli has not yet been configured
268 public function testCheckLoginStateNotConfigured()
270 $configManager = new \
FakeConfigManager([
271 'resource.ban_file' => $this->banFile
,
273 $loginManager = new LoginManager($this->globals
, $configManager, null);
274 $loginManager->checkLoginState([], '');
276 $this->assertFalse($loginManager->isLoggedIn());
280 * Check user login - the client cookie does not match the server token
282 public function testCheckLoginStateStaySignedInWithInvalidToken()
284 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
285 $this->cookie
[LoginManager
::$STAY_SIGNED_IN_COOKIE] = 'nope';
287 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
289 $this->assertFalse($this->loginManager
->isLoggedIn());
293 * Check user login - the client cookie matches the server token
295 public function testCheckLoginStateStaySignedInWithValidToken()
297 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
298 $this->cookie
[LoginManager
::$STAY_SIGNED_IN_COOKIE] = $this->loginManager
->getStaySignedInToken();
300 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
302 $this->assertTrue($this->loginManager
->isLoggedIn());
306 * Check user login - the session has expired
308 public function testCheckLoginStateSessionExpired()
310 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
311 $this->session
['expires_on'] = time() - 100;
313 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
315 $this->assertFalse($this->loginManager
->isLoggedIn());
319 * Check user login - the remote client IP has changed
321 public function testCheckLoginStateClientIpChanged()
323 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
325 $this->loginManager
->checkLoginState($this->cookie
, '10.7.157.98');
327 $this->assertFalse($this->loginManager
->isLoggedIn());
331 * Check user credentials - wrong login supplied
333 public function testCheckCredentialsWrongLogin()
336 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', $this->password
)
341 * Check user credentials - wrong password supplied
343 public function testCheckCredentialsWrongPassword()
346 $this->loginManager
->checkCredentials('', '', $this->login
, 'b4dp455wd')
351 * Check user credentials - wrong login and password supplied
353 public function testCheckCredentialsWrongLoginAndPassword()
356 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
361 * Check user credentials - correct login and password supplied
363 public function testCheckCredentialsGoodLoginAndPassword()
366 $this->loginManager
->checkCredentials('', '', $this->login
, $this->password
)