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
],
86 $this->sessionManager
= new SessionManager($this->session
, $this->configManager
);
87 $this->loginManager
= new LoginManager($this->configManager
, $this->sessionManager
);
88 $this->server
['REMOTE_ADDR'] = $this->ipAddr
;
92 * Record a failed login attempt
94 public function testHandleFailedLogin()
96 $this->loginManager
->handleFailedLogin($this->server
);
97 $this->loginManager
->handleFailedLogin($this->server
);
98 $this->assertFalse($this->loginManager
->canLogin($this->server
));
102 * Record a failed login attempt - IP behind a trusted proxy
104 public function testHandleFailedLoginBehindTrustedProxy()
107 'REMOTE_ADDR' => $this->trustedProxy
,
108 'HTTP_X_FORWARDED_FOR' => $this->ipAddr
,
110 $this->loginManager
->handleFailedLogin($server);
111 $this->loginManager
->handleFailedLogin($server);
112 $this->assertFalse($this->loginManager
->canLogin($server));
116 * Record a failed login attempt - IP behind a trusted proxy but not forwarded
118 public function testHandleFailedLoginBehindTrustedProxyNoIp()
121 'REMOTE_ADDR' => $this->trustedProxy
,
123 $this->loginManager
->handleFailedLogin($server);
124 $this->loginManager
->handleFailedLogin($server);
125 $this->assertTrue($this->loginManager
->canLogin($server));
131 public function testHandleSuccessfulLogin()
133 $this->assertTrue($this->loginManager
->canLogin($this->server
));
135 $this->loginManager
->handleSuccessfulLogin($this->server
);
136 $this->assertTrue($this->loginManager
->canLogin($this->server
));
140 * Erase failure records after successfully logging in from this IP
142 public function testHandleSuccessfulLoginAfterFailure()
144 $this->loginManager
->handleFailedLogin($this->server
);
145 $this->assertTrue($this->loginManager
->canLogin($this->server
));
147 $this->loginManager
->handleSuccessfulLogin($this->server
);
148 $this->loginManager
->handleFailedLogin($this->server
);
149 $this->assertTrue($this->loginManager
->canLogin($this->server
));
153 * The IP is not banned
155 public function testCanLoginIpNotBanned()
157 $this->assertTrue($this->loginManager
->canLogin($this->server
));
161 * Generate a token depending on the user credentials and client IP
163 public function testGenerateStaySignedInToken()
165 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
168 sha1($this->passwordHash
. $this->clientIpAddress
. $this->salt
),
169 $this->loginManager
->getStaySignedInToken()
174 * Generate a token depending on the user credentials with session protected disabled
176 public function testGenerateStaySignedInTokenSessionProtectionDisabled()
178 $this->configManager
->set('security.session_protection_disabled', true);
179 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
182 sha1($this->passwordHash
. $this->salt
),
183 $this->loginManager
->getStaySignedInToken()
188 * Check user login - Shaarli has not yet been configured
190 public function testCheckLoginStateNotConfigured()
192 $configManager = new \
FakeConfigManager([
193 'resource.ban_file' => $this->banFile
,
195 $loginManager = new LoginManager($configManager, null);
196 $loginManager->checkLoginState([], '');
198 $this->assertFalse($loginManager->isLoggedIn());
202 * Check user login - the client cookie does not match the server token
204 public function testCheckLoginStateStaySignedInWithInvalidToken()
206 // simulate a previous login
208 'ip' => $this->clientIpAddress
,
209 'expires_on' => time() +
100,
211 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
212 $this->cookie
[LoginManager
::$STAY_SIGNED_IN_COOKIE] = 'nope';
214 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
216 $this->assertTrue($this->loginManager
->isLoggedIn());
217 $this->assertTrue(empty($this->session
['username']));
221 * Check user login - the client cookie matches the server token
223 public function testCheckLoginStateStaySignedInWithValidToken()
225 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
226 $this->cookie
[LoginManager
::$STAY_SIGNED_IN_COOKIE] = $this->loginManager
->getStaySignedInToken();
228 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
230 $this->assertTrue($this->loginManager
->isLoggedIn());
231 $this->assertEquals($this->login
, $this->session
['username']);
232 $this->assertEquals($this->clientIpAddress
, $this->session
['ip']);
236 * Check user login - the session has expired
238 public function testCheckLoginStateSessionExpired()
240 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
241 $this->session
['expires_on'] = time() - 100;
243 $this->loginManager
->checkLoginState($this->cookie
, $this->clientIpAddress
);
245 $this->assertFalse($this->loginManager
->isLoggedIn());
249 * Check user login - the remote client IP has changed
251 public function testCheckLoginStateClientIpChanged()
253 $this->loginManager
->generateStaySignedInToken($this->clientIpAddress
);
255 $this->loginManager
->checkLoginState($this->cookie
, '10.7.157.98');
257 $this->assertFalse($this->loginManager
->isLoggedIn());
261 * Check user credentials - wrong login supplied
263 public function testCheckCredentialsWrongLogin()
266 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', $this->password
)
271 * Check user credentials - wrong password supplied
273 public function testCheckCredentialsWrongPassword()
276 $this->loginManager
->checkCredentials('', '', $this->login
, 'b4dp455wd')
281 * Check user credentials - wrong login and password supplied
283 public function testCheckCredentialsWrongLoginAndPassword()
286 $this->loginManager
->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
291 * Check user credentials - correct login and password supplied
293 public function testCheckCredentialsGoodLoginAndPassword()
296 $this->loginManager
->checkCredentials('', '', $this->login
, $this->password
)