3 namespace Shaarli\Security
;
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';
60 /** @var CookieManager */
61 protected $cookieManager;
64 * Prepare or reset test resources
66 public function setUp()
68 if (file_exists($this->banFile
)) {
69 unlink($this->banFile
);
72 $this->passwordHash
= sha1($this->password
. $this->login
. $this->salt
);
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
],
89 $this->cookieManager
= $this->createMock(CookieManager
::class);
90 $this->cookieManager
->method('getCookieParameter')->willReturnCallback(function (string $key) {
91 return $this->cookie
[$key] ?? null;
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
;
99 * Record a failed login attempt
101 public function testHandleFailedLogin()
103 $this->loginManager
->handleFailedLogin($this->server
);
104 $this->loginManager
->handleFailedLogin($this->server
);
105 $this->assertFalse($this->loginManager
->canLogin($this->server
));
109 * Record a failed login attempt - IP behind a trusted proxy
111 public function testHandleFailedLoginBehindTrustedProxy()
114 'REMOTE_ADDR' => $this->trustedProxy
,
115 'HTTP_X_FORWARDED_FOR' => $this->ipAddr
,
117 $this->loginManager
->handleFailedLogin($server);
118 $this->loginManager
->handleFailedLogin($server);
119 $this->assertFalse($this->loginManager
->canLogin($server));
123 * Record a failed login attempt - IP behind a trusted proxy but not forwarded
125 public function testHandleFailedLoginBehindTrustedProxyNoIp()
128 'REMOTE_ADDR' => $this->trustedProxy
,
130 $this->loginManager
->handleFailedLogin($server);
131 $this->loginManager
->handleFailedLogin($server);
132 $this->assertTrue($this->loginManager
->canLogin($server));
138 public function testHandleSuccessfulLogin()
140 $this->assertTrue($this->loginManager
->canLogin($this->server
));
142 $this->loginManager
->handleSuccessfulLogin($this->server
);
143 $this->assertTrue($this->loginManager
->canLogin($this->server
));
147 * Erase failure records after successfully logging in from this IP
149 public function testHandleSuccessfulLoginAfterFailure()
151 $this->loginManager
->handleFailedLogin($this->server
);
152 $this->assertTrue($this->loginManager
->canLogin($this->server
));
154 $this->loginManager
->handleSuccessfulLogin($this->server
);
155 $this->loginManager
->handleFailedLogin($this->server
);
156 $this->assertTrue($this->loginManager
->canLogin($this->server
));
160 * The IP is not banned
162 public function testCanLoginIpNotBanned()
164 $this->assertTrue($this->loginManager
->canLogin($this->server
));
168 * Generate a token depending on the user credentials and client IP
170 public function testGenerateStaySignedInToken()
172 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
175 sha1($this->passwordHash
. $this->clientIpAddress
. $this->salt
),
176 $this->loginManager
->getStaySignedInToken()
181 * Generate a token depending on the user credentials with session protected disabled
183 public function testGenerateStaySignedInTokenSessionProtectionDisabled()
185 $this->configManager
->set('security.session_protection_disabled', true);
186 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
189 sha1($this->passwordHash
. $this->salt
),
190 $this->loginManager
->getStaySignedInToken()
195 * Check user login - Shaarli has not yet been configured
197 public function testCheckLoginStateNotConfigured()
199 $configManager = new \
FakeConfigManager([
200 'resource.ban_file' => $this->banFile
,
202 $loginManager = new LoginManager($configManager, null, $this->cookieManager
);
203 $loginManager->checkLoginState('');
205 $this->assertFalse($loginManager->isLoggedIn());
209 * Check user login - the client cookie does not match the server token
211 public function testCheckLoginStateStaySignedInWithInvalidToken()
213 // simulate a previous login
215 'ip' => $this->clientIpAddress
,
216 'expires_on' => time() +
100,
218 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
219 $this->cookie
[CookieManager
::STAY_SIGNED_IN
] = 'nope';
221 $this->loginManager
->checkLoginState($this->clientIpAddress
);
223 $this->assertTrue($this->loginManager
->isLoggedIn());
224 $this->assertTrue(empty($this->session
['username']));
228 * Check user login - the client cookie matches the server token
230 public function testCheckLoginStateStaySignedInWithValidToken()
232 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
233 $this->cookie
[CookieManager
::STAY_SIGNED_IN
] = $this->loginManager
->getStaySignedInToken();
235 $this->loginManager
->checkLoginState($this->clientIpAddress
);
237 $this->assertTrue($this->loginManager
->isLoggedIn());
238 $this->assertEquals($this->login
, $this->session
['username']);
239 $this->assertEquals($this->clientIpAddress
, $this->session
['ip']);
243 * Check user login - the session has expired
245 public function testCheckLoginStateSessionExpired()
247 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
248 $this->session
['expires_on'] = time() - 100;
250 $this->loginManager
->checkLoginState($this->clientIpAddress
);
252 $this->assertFalse($this->loginManager
->isLoggedIn());
256 * Check user login - the remote client IP has changed
258 public function testCheckLoginStateClientIpChanged()
260 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
262 $this->loginManager
->checkLoginState('10.7.157.98');
264 $this->assertFalse($this->loginManager
->isLoggedIn());
268 * Check user credentials - wrong login supplied
270 public function testCheckCredentialsWrongLogin()
273 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', $this->password
)
278 * Check user credentials - wrong password supplied
280 public function testCheckCredentialsWrongPassword()
283 $this->loginManager
->checkCredentials('', '', $this->login
, 'b4dp455wd')
288 * Check user credentials - wrong login and password supplied
290 public function testCheckCredentialsWrongLoginAndPassword()
293 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
298 * Check user credentials - correct login and password supplied
300 public function testCheckCredentialsGoodLoginAndPassword()
303 $this->loginManager
->checkCredentials('', '', $this->login
, $this->password
)
308 * Check user credentials through LDAP - server unreachable
310 public function testCheckCredentialsFromUnreachableLdap()
312 $this->configManager
->set('ldap.host', 'dummy');
314 $this->loginManager
->checkCredentials('', '', $this->login
, $this->password
)
319 * Check user credentials through LDAP - wrong login and password supplied
321 public function testCheckCredentialsFromLdapWrongLoginAndPassword()
323 $this->configManager
->set('ldap.host', 'dummy');
325 $this->loginManager
->checkCredentialsFromLdap($this->login
, $this->password
, function() { return null; }, function() { return false; })
330 * Check user credentials through LDAP - correct login and password supplied
332 public function testCheckCredentialsFromLdapGoodLoginAndPassword()
334 $this->configManager
->set('ldap.host', 'dummy');
336 $this->loginManager
->checkCredentialsFromLdap($this->login
, $this->password
, function() { return null; }, function() { return true; })