LoginManagerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Shaarli;
  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. protected $configManager = null;
  11. protected $loginManager = null;
  12. protected $banFile = 'sandbox/ipbans.php';
  13. protected $logFile = 'sandbox/shaarli.log';
  14. protected $globals = [];
  15. protected $ipAddr = '127.0.0.1';
  16. protected $server = [];
  17. protected $trustedProxy = '10.1.1.100';
  18. /**
  19. * Prepare or reset test resources
  20. */
  21. public function setUp()
  22. {
  23. if (file_exists($this->banFile)) {
  24. unlink($this->banFile);
  25. }
  26. $this->configManager = new \FakeConfigManager([
  27. 'resource.ban_file' => $this->banFile,
  28. 'resource.log' => $this->logFile,
  29. 'security.ban_after' => 4,
  30. 'security.ban_duration' => 3600,
  31. 'security.trusted_proxies' => [$this->trustedProxy],
  32. ]);
  33. $this->globals = &$GLOBALS;
  34. unset($this->globals['IPBANS']);
  35. $this->loginManager = new LoginManager($this->globals, $this->configManager);
  36. $this->server['REMOTE_ADDR'] = $this->ipAddr;
  37. }
  38. /**
  39. * Wipe test resources
  40. */
  41. public function tearDown()
  42. {
  43. unset($this->globals['IPBANS']);
  44. }
  45. /**
  46. * Instantiate a LoginManager and load ban records
  47. */
  48. public function testReadBanFile()
  49. {
  50. file_put_contents(
  51. $this->banFile,
  52. "<?php\n\$GLOBALS['IPBANS']=array('FAILURES' => array('127.0.0.1' => 99));\n?>"
  53. );
  54. new LoginManager($this->globals, $this->configManager);
  55. $this->assertEquals(99, $this->globals['IPBANS']['FAILURES']['127.0.0.1']);
  56. }
  57. /**
  58. * Record a failed login attempt
  59. */
  60. public function testHandleFailedLogin()
  61. {
  62. $this->loginManager->handleFailedLogin($this->server);
  63. $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  64. $this->loginManager->handleFailedLogin($this->server);
  65. $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  66. }
  67. /**
  68. * Record a failed login attempt - IP behind a trusted proxy
  69. */
  70. public function testHandleFailedLoginBehindTrustedProxy()
  71. {
  72. $server = [
  73. 'REMOTE_ADDR' => $this->trustedProxy,
  74. 'HTTP_X_FORWARDED_FOR' => $this->ipAddr,
  75. ];
  76. $this->loginManager->handleFailedLogin($server);
  77. $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  78. $this->loginManager->handleFailedLogin($server);
  79. $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  80. }
  81. /**
  82. * Record a failed login attempt - IP behind a trusted proxy but not forwarded
  83. */
  84. public function testHandleFailedLoginBehindTrustedProxyNoIp()
  85. {
  86. $server = [
  87. 'REMOTE_ADDR' => $this->trustedProxy,
  88. ];
  89. $this->loginManager->handleFailedLogin($server);
  90. $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
  91. $this->loginManager->handleFailedLogin($server);
  92. $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
  93. }
  94. /**
  95. * Record a failed login attempt and ban the IP after too many failures
  96. */
  97. public function testHandleFailedLoginBanIp()
  98. {
  99. $this->loginManager->handleFailedLogin($this->server);
  100. $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  101. $this->assertTrue($this->loginManager->canLogin($this->server));
  102. $this->loginManager->handleFailedLogin($this->server);
  103. $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  104. $this->assertTrue($this->loginManager->canLogin($this->server));
  105. $this->loginManager->handleFailedLogin($this->server);
  106. $this->assertEquals(3, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  107. $this->assertTrue($this->loginManager->canLogin($this->server));
  108. $this->loginManager->handleFailedLogin($this->server);
  109. $this->assertEquals(4, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  110. $this->assertFalse($this->loginManager->canLogin($this->server));
  111. // handleFailedLogin is not supposed to be called at this point:
  112. // - no login form should be displayed once an IP has been banned
  113. // - yet this could happen when using custom templates / scripts
  114. $this->loginManager->handleFailedLogin($this->server);
  115. $this->assertEquals(5, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]);
  116. $this->assertFalse($this->loginManager->canLogin($this->server));
  117. }
  118. /**
  119. * Nothing to do
  120. */
  121. public function testHandleSuccessfulLogin()
  122. {
  123. $this->assertTrue($this->loginManager->canLogin($this->server));
  124. $this->loginManager->handleSuccessfulLogin($this->server);
  125. $this->assertTrue($this->loginManager->canLogin($this->server));
  126. }
  127. /**
  128. * Erase failure records after successfully logging in from this IP
  129. */
  130. public function testHandleSuccessfulLoginAfterFailure()
  131. {
  132. $this->loginManager->handleFailedLogin($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->handleSuccessfulLogin($this->server);
  137. $this->assertTrue($this->loginManager->canLogin($this->server));
  138. $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr]));
  139. $this->assertFalse(isset($this->globals['IPBANS']['BANS'][$this->ipAddr]));
  140. }
  141. /**
  142. * The IP is not banned
  143. */
  144. public function testCanLoginIpNotBanned()
  145. {
  146. $this->assertTrue($this->loginManager->canLogin($this->server));
  147. }
  148. /**
  149. * The IP is banned
  150. */
  151. public function testCanLoginIpBanned()
  152. {
  153. // ban the IP for an hour
  154. $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10;
  155. $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600;
  156. $this->assertFalse($this->loginManager->canLogin($this->server));
  157. }
  158. /**
  159. * The IP is banned, and the ban duration is over
  160. */
  161. public function testCanLoginIpBanExpired()
  162. {
  163. // ban the IP for an hour
  164. $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10;
  165. $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600;
  166. $this->assertFalse($this->loginManager->canLogin($this->server));
  167. // lift the ban
  168. $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() - 3600;
  169. $this->assertTrue($this->loginManager->canLogin($this->server));
  170. }
  171. }