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' => 2,
79 'security.ban_duration' => 3600,
80 'security.trusted_proxies' => [$this->trustedProxy
],
87 $this->sessionManager
= new SessionManager($this->session
, $this->configManager
);
88 $this->loginManager
= new LoginManager($this->configManager
, $this->sessionManager
);
89 $this->server
['REMOTE_ADDR'] = $this->ipAddr
;
93 * Record a failed login attempt
95 public function testHandleFailedLogin()
97 $this->loginManager
->handleFailedLogin($this->server
);
98 $this->loginManager
->handleFailedLogin($this->server
);
99 $this->assertFalse($this->loginManager
->canLogin($this->server
));
103 * Record a failed login attempt - IP behind a trusted proxy
105 public function testHandleFailedLoginBehindTrustedProxy()
108 'REMOTE_ADDR' => $this->trustedProxy
,
109 'HTTP_X_FORWARDED_FOR' => $this->ipAddr
,
111 $this->loginManager
->handleFailedLogin($server);
112 $this->loginManager
->handleFailedLogin($server);
113 $this->assertFalse($this->loginManager
->canLogin($server));
117 * Record a failed login attempt - IP behind a trusted proxy but not forwarded
119 public function testHandleFailedLoginBehindTrustedProxyNoIp()
122 'REMOTE_ADDR' => $this->trustedProxy
,
124 $this->loginManager
->handleFailedLogin($server);
125 $this->loginManager
->handleFailedLogin($server);
126 $this->assertTrue($this->loginManager
->canLogin($server));
132 public function testHandleSuccessfulLogin()
134 $this->assertTrue($this->loginManager
->canLogin($this->server
));
136 $this->loginManager
->handleSuccessfulLogin($this->server
);
137 $this->assertTrue($this->loginManager
->canLogin($this->server
));
141 * Erase failure records after successfully logging in from this IP
143 public function testHandleSuccessfulLoginAfterFailure()
145 $this->loginManager
->handleFailedLogin($this->server
);
146 $this->assertTrue($this->loginManager
->canLogin($this->server
));
148 $this->loginManager
->handleSuccessfulLogin($this->server
);
149 $this->loginManager
->handleFailedLogin($this->server
);
150 $this->assertTrue($this->loginManager
->canLogin($this->server
));
154 * The IP is not banned
156 public function testCanLoginIpNotBanned()
158 $this->assertTrue($this->loginManager
->canLogin($this->server
));
162 * Generate a token depending on the user credentials and client IP
164 public function testGenerateStaySignedInToken()
166 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
169 sha1($this->passwordHash
. $this->clientIpAddress
. $this->salt
),
170 $this->loginManager
->getStaySignedInToken()
175 * Generate a token depending on the user credentials with session protected disabled
177 public function testGenerateStaySignedInTokenSessionProtectionDisabled()
179 $this->configManager
->set('security.session_protection_disabled', true);
180 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
183 sha1($this->passwordHash
. $this->salt
),
184 $this->loginManager
->getStaySignedInToken()
189 * Check user login - Shaarli has not yet been configured
191 public function testCheckLoginStateNotConfigured()
193 $configManager = new \
FakeConfigManager([
194 'resource.ban_file' => $this->banFile
,
196 $loginManager = new LoginManager($configManager, null);
197 $loginManager->checkLoginState([], '');
199 $this->assertFalse($loginManager->isLoggedIn());
203 * Check user login - the client cookie does not match the server token
205 public function testCheckLoginStateStaySignedInWithInvalidToken()
207 // simulate a previous login
209 'ip' => $this->clientIpAddress
,
210 'expires_on' => time() +
100,
212 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
213 $this->cookie
[LoginManager
::$STAY_SIGNED_IN_COOKIE] = 'nope';
215 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
217 $this->assertTrue($this->loginManager
->isLoggedIn());
218 $this->assertTrue(empty($this->session
['username']));
222 * Check user login - the client cookie matches the server token
224 public function testCheckLoginStateStaySignedInWithValidToken()
226 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
227 $this->cookie
[LoginManager
::$STAY_SIGNED_IN_COOKIE] = $this->loginManager
->getStaySignedInToken();
229 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
231 $this->assertTrue($this->loginManager
->isLoggedIn());
232 $this->assertEquals($this->login
, $this->session
['username']);
233 $this->assertEquals($this->clientIpAddress
, $this->session
['ip']);
237 * Check user login - the session has expired
239 public function testCheckLoginStateSessionExpired()
241 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
242 $this->session
['expires_on'] = time() - 100;
244 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
246 $this->assertFalse($this->loginManager
->isLoggedIn());
250 * Check user login - the remote client IP has changed
252 public function testCheckLoginStateClientIpChanged()
254 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
256 $this->loginManager
->checkLoginState($this->cookie
, '10.7.157.98');
258 $this->assertFalse($this->loginManager
->isLoggedIn());
262 * Check user credentials - wrong login supplied
264 public function testCheckCredentialsWrongLogin()
267 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', $this->password
)
272 * Check user credentials - wrong password supplied
274 public function testCheckCredentialsWrongPassword()
277 $this->loginManager
->checkCredentials('', '', $this->login
, 'b4dp455wd')
282 * Check user credentials - wrong login and password supplied
284 public function testCheckCredentialsWrongLoginAndPassword()
287 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
292 * Check user credentials - correct login and password supplied
294 public function testCheckCredentialsGoodLoginAndPassword()
297 $this->loginManager
->checkCredentials('', '', $this->login
, $this->password
)
302 * Check user credentials through LDAP - server unreachable
304 public function testCheckCredentialsFromUnreachableLdap()
306 $this->configManager
->set('ldap.host', 'dummy');
308 $this->loginManager
->checkCredentials('', '', $this->login
, $this->password
)
313 * Check user credentials through LDAP - wrong login and password supplied
315 public function testCheckCredentialsFromLdapWrongLoginAndPassword()
317 $this->coddnfigManager
->set('ldap.host', 'dummy');
319 $this->loginManager
->checkCredentialsFromLdap($this->login
, $this->password
, function() { return null; }, function() { return false; })
324 * Check user credentials through LDAP - correct login and password supplied
326 public function testCheckCredentialsFromLdapGoodLoginAndPassword()
328 $this->configManager
->set('ldap.host', 'dummy');
330 $this->loginManager
->checkCredentialsFromLdap($this->login
, $this->password
, function() { return null; }, function() { return true; })