LoginManagerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace Shaarli\Security;
  3. require_once 'tests/utils/FakeConfigManager.php';
  4. use \PHPUnit\Framework\TestCase;
  5. /**
  6. * Test coverage for LoginManager
  7. */
  8. class LoginManagerTest extends TestCase
  9. {
  10. /** @var \FakeConfigManager Configuration Manager instance */
  11. protected $configManager = null;
  12. /** @var LoginManager Login Manager instance */
  13. protected $loginManager = null;
  14. /** @var SessionManager Session Manager instance */
  15. protected $sessionManager = null;
  16. /** @var string Banned IP filename */
  17. protected $banFile = 'sandbox/ipbans.php';
  18. /** @var string Log filename */
  19. protected $logFile = 'sandbox/shaarli.log';
  20. /** @var array Simulates the $_COOKIE array */
  21. protected $cookie = [];
  22. /** @var array Simulates the $GLOBALS array */
  23. protected $globals = [];
  24. /** @var array Simulates the $_SERVER array */
  25. protected $server = [];
  26. /** @var array Simulates the $_SESSION array */
  27. protected $session = [];
  28. /** @var string Advertised client IP address */
  29. protected $clientIpAddress = '10.1.47.179';
  30. /** @var string Local client IP address */
  31. protected $ipAddr = '127.0.0.1';
  32. /** @var string Trusted proxy IP address */
  33. protected $trustedProxy = '10.1.1.100';
  34. /** @var string User login */
  35. protected $login = 'johndoe';
  36. /** @var string User password */
  37. protected $password = 'IC4nHazL0g1n?';
  38. /** @var string Hash of the salted user password */
  39. protected $passwordHash = '';
  40. /** @var string Salt used by hash functions */
  41. protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2';
  42. /**
  43. * Prepare or reset test resources
  44. */
  45. public function setUp()
  46. {
  47. if (file_exists($this->banFile)) {
  48. unlink($this->banFile);
  49. }
  50. $this->passwordHash = sha1($this->password . $this->login . $this->salt);
  51. $this->configManager = new \FakeConfigManager([
  52. 'credentials.login' => $this->login,
  53. 'credentials.hash' => $this->passwordHash,
  54. 'credentials.salt' => $this->salt,
  55. 'resource.ban_file' => $this->banFile,
  56. 'resource.log' => $this->logFile,
  57. 'security.ban_after' => 4,
  58. 'security.ban_duration' => 3600,
  59. 'security.trusted_proxies' => [$this->trustedProxy],
  60. ]);
  61. $this->cookie = [];
  62. $this->globals = &$GLOBALS;
  63. unset($this->globals['IPBANS']);
  64. $this->session = [];
  65. $this->sessionManager = new SessionManager($this->session, $this->configManager);
  66. $this->loginManager = new LoginManager($this->globals, $this->configManager, $this->sessionManager);
  67. $this->server['REMOTE_ADDR'] = $this->ipAddr;
  68. }
  69. /**
  70. * Wipe test resources
  71. */
  72. public function tearDown()
  73. {
  74. unset($this->globals['IPBANS']);
  75. }
  76. /**
  77. * Instantiate a LoginManager and load ban records
  78. */
  79. public function testReadBanFile()
  80. {
  81. file_put_contents(
  82. $this->banFile,
  83. "<?php\n\$GLOBALS['IPBANS']=array('FAILURES' => array('127.0.0.1' => 99));\n?>"
  84. );
  85. new LoginManager($this->globals, $this->configManager, null);
  86. $this->assertEquals(99, $this->globals['IPBANS']['FAILURES']['127.0.0.1']);
  87. }
  88. /**
  89. * Record a failed login attempt
  90. */
  91. public function testHandleFailedLogin()
  92. {
  93. $this->loginManager->handleFailedLogin($this->server);
  94. $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  95. $this->loginManager->handleFailedLogin($this->server);
  96. $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  97. }
  98. /**
  99. * Record a failed login attempt - IP behind a trusted proxy
  100. */
  101. public function testHandleFailedLoginBehindTrustedProxy()
  102. {
  103. $server = [
  104. 'REMOTE_ADDR' => $this->trustedProxy,
  105. 'HTTP_X_FORWARDED_FOR' => $this->ipAddr,
  106. ];
  107. $this->loginManager->handleFailedLogin($server);
  108. $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  109. $this->loginManager->handleFailedLogin($server);
  110. $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  111. }
  112. /**
  113. * Record a failed login attempt - IP behind a trusted proxy but not forwarded
  114. */
  115. public function testHandleFailedLoginBehindTrustedProxyNoIp()
  116. {
  117. $server = [
  118. 'REMOTE_ADDR' => $this->trustedProxy,
  119. ];
  120. $this->loginManager->handleFailedLogin($server);
  121. $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
  122. $this->loginManager->handleFailedLogin($server);
  123. $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
  124. }
  125. /**
  126. * Record a failed login attempt and ban the IP after too many failures
  127. */
  128. public function testHandleFailedLoginBanIp()
  129. {
  130. $this->loginManager->handleFailedLogin($this->server);
  131. $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  132. $this->assertTrue($this->loginManager->canLogin($this->server));
  133. $this->loginManager->handleFailedLogin($this->server);
  134. $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  135. $this->assertTrue($this->loginManager->canLogin($this->server));
  136. $this->loginManager->handleFailedLogin($this->server);
  137. $this->assertEquals(3, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  138. $this->assertTrue($this->loginManager->canLogin($this->server));
  139. $this->loginManager->handleFailedLogin($this->server);
  140. $this->assertEquals(4, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  141. $this->assertFalse($this->loginManager->canLogin($this->server));
  142. // handleFailedLogin is not supposed to be called at this point:
  143. // - no login form should be displayed once an IP has been banned
  144. // - yet this could happen when using custom templates / scripts
  145. $this->loginManager->handleFailedLogin($this->server);
  146. $this->assertEquals(5, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  147. $this->assertFalse($this->loginManager->canLogin($this->server));
  148. }
  149. /**
  150. * Nothing to do
  151. */
  152. public function testHandleSuccessfulLogin()
  153. {
  154. $this->assertTrue($this->loginManager->canLogin($this->server));
  155. $this->loginManager->handleSuccessfulLogin($this->server);
  156. $this->assertTrue($this->loginManager->canLogin($this->server));
  157. }
  158. /**
  159. * Erase failure records after successfully logging in from this IP
  160. */
  161. public function testHandleSuccessfulLoginAfterFailure()
  162. {
  163. $this->loginManager->handleFailedLogin($this->server);
  164. $this->loginManager->handleFailedLogin($this->server);
  165. $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  166. $this->assertTrue($this->loginManager->canLogin($this->server));
  167. $this->loginManager->handleSuccessfulLogin($this->server);
  168. $this->assertTrue($this->loginManager->canLogin($this->server));
  169. $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
  170. $this->assertFalse(isset($this->globals['IPBANS']['BANS'][$this->ipAddr]));
  171. }
  172. /**
  173. * The IP is not banned
  174. */
  175. public function testCanLoginIpNotBanned()
  176. {
  177. $this->assertTrue($this->loginManager->canLogin($this->server));
  178. }
  179. /**
  180. * The IP is banned
  181. */
  182. public function testCanLoginIpBanned()
  183. {
  184. // ban the IP for an hour
  185. $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10;
  186. $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600;
  187. $this->assertFalse($this->loginManager->canLogin($this->server));
  188. }
  189. /**
  190. * The IP is banned, and the ban duration is over
  191. */
  192. public function testCanLoginIpBanExpired()
  193. {
  194. // ban the IP for an hour
  195. $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10;
  196. $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600;
  197. $this->assertFalse($this->loginManager->canLogin($this->server));
  198. // lift the ban
  199. $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() - 3600;
  200. $this->assertTrue($this->loginManager->canLogin($this->server));
  201. }
  202. /**
  203. * Generate a token depending on the user credentials and client IP
  204. */
  205. public function testGenerateStaySignedInToken()
  206. {
  207. $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
  208. $this->assertEquals(
  209. sha1($this->passwordHash . $this->clientIpAddress . $this->salt),
  210. $this->loginManager->getStaySignedInToken()
  211. );
  212. }
  213. /**
  214. * Check user login - Shaarli has not yet been configured
  215. */
  216. public function testCheckLoginStateNotConfigured()
  217. {
  218. $configManager = new \FakeConfigManager([
  219. 'resource.ban_file' => $this->banFile,
  220. ]);
  221. $loginManager = new LoginManager($this->globals, $configManager, null);
  222. $loginManager->checkLoginState([], '');
  223. $this->assertFalse($loginManager->isLoggedIn());
  224. }
  225. /**
  226. * Check user login - the client cookie does not match the server token
  227. */
  228. public function testCheckLoginStateStaySignedInWithInvalidToken()
  229. {
  230. // simulate a previous login
  231. $this->session = [
  232. 'ip' => $this->clientIpAddress,
  233. 'expires_on' => time() + 100,
  234. ];
  235. $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
  236. $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope';
  237. $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
  238. $this->assertTrue($this->loginManager->isLoggedIn());
  239. $this->assertTrue(empty($this->session['username']));
  240. }
  241. /**
  242. * Check user login - the client cookie matches the server token
  243. */
  244. public function testCheckLoginStateStaySignedInWithValidToken()
  245. {
  246. $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
  247. $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken();
  248. $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
  249. $this->assertTrue($this->loginManager->isLoggedIn());
  250. $this->assertEquals($this->login, $this->session['username']);
  251. $this->assertEquals($this->clientIpAddress, $this->session['ip']);
  252. }
  253. /**
  254. * Check user login - the session has expired
  255. */
  256. public function testCheckLoginStateSessionExpired()
  257. {
  258. $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
  259. $this->session['expires_on'] = time() - 100;
  260. $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
  261. $this->assertFalse($this->loginManager->isLoggedIn());
  262. }
  263. /**
  264. * Check user login - the remote client IP has changed
  265. */
  266. public function testCheckLoginStateClientIpChanged()
  267. {
  268. $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
  269. $this->loginManager->checkLoginState($this->cookie, '10.7.157.98');
  270. $this->assertFalse($this->loginManager->isLoggedIn());
  271. }
  272. /**
  273. * Check user credentials - wrong login supplied
  274. */
  275. public function testCheckCredentialsWrongLogin()
  276. {
  277. $this->assertFalse(
  278. $this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password)
  279. );
  280. }
  281. /**
  282. * Check user credentials - wrong password supplied
  283. */
  284. public function testCheckCredentialsWrongPassword()
  285. {
  286. $this->assertFalse(
  287. $this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd')
  288. );
  289. }
  290. /**
  291. * Check user credentials - wrong login and password supplied
  292. */
  293. public function testCheckCredentialsWrongLoginAndPassword()
  294. {
  295. $this->assertFalse(
  296. $this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
  297. );
  298. }
  299. /**
  300. * Check user credentials - correct login and password supplied
  301. */
  302. public function testCheckCredentialsGoodLoginAndPassword()
  303. {
  304. $this->assertTrue(
  305. $this->loginManager->checkCredentials('', '', $this->login, $this->password)
  306. );
  307. }
  308. }