2017-10-25 23:03:31 +02:00
|
|
|
<?php
|
2018-04-27 22:12:22 +02:00
|
|
|
namespace Shaarli\Security;
|
2017-10-25 23:03:31 +02:00
|
|
|
|
|
|
|
require_once 'tests/utils/FakeConfigManager.php';
|
2019-01-12 23:55:38 +01:00
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-10-25 23:03:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test coverage for LoginManager
|
|
|
|
*/
|
|
|
|
class LoginManagerTest extends TestCase
|
|
|
|
{
|
2018-05-06 17:12:48 +02:00
|
|
|
/** @var \FakeConfigManager Configuration Manager instance */
|
2017-10-25 23:03:31 +02:00
|
|
|
protected $configManager = null;
|
2018-05-06 17:12:48 +02:00
|
|
|
|
|
|
|
/** @var LoginManager Login Manager instance */
|
2017-10-25 23:03:31 +02:00
|
|
|
protected $loginManager = null;
|
2018-05-06 17:12:48 +02:00
|
|
|
|
|
|
|
/** @var SessionManager Session Manager instance */
|
|
|
|
protected $sessionManager = null;
|
|
|
|
|
|
|
|
/** @var string Banned IP filename */
|
2017-10-25 23:03:31 +02:00
|
|
|
protected $banFile = 'sandbox/ipbans.php';
|
2018-05-06 17:12:48 +02:00
|
|
|
|
|
|
|
/** @var string Log filename */
|
2017-10-25 23:03:31 +02:00
|
|
|
protected $logFile = 'sandbox/shaarli.log';
|
2018-05-06 17:12:48 +02:00
|
|
|
|
|
|
|
/** @var array Simulates the $_COOKIE array */
|
|
|
|
protected $cookie = [];
|
|
|
|
|
|
|
|
/** @var array Simulates the $GLOBALS array */
|
2017-10-25 23:03:31 +02:00
|
|
|
protected $globals = [];
|
2018-05-06 17:12:48 +02:00
|
|
|
|
|
|
|
/** @var array Simulates the $_SERVER array */
|
2017-10-25 23:03:31 +02:00
|
|
|
protected $server = [];
|
2018-05-06 17:12:48 +02:00
|
|
|
|
|
|
|
/** @var array Simulates the $_SESSION array */
|
|
|
|
protected $session = [];
|
|
|
|
|
|
|
|
/** @var string Advertised client IP address */
|
|
|
|
protected $clientIpAddress = '10.1.47.179';
|
|
|
|
|
|
|
|
/** @var string Local client IP address */
|
|
|
|
protected $ipAddr = '127.0.0.1';
|
|
|
|
|
|
|
|
/** @var string Trusted proxy IP address */
|
2017-10-25 23:03:31 +02:00
|
|
|
protected $trustedProxy = '10.1.1.100';
|
|
|
|
|
2018-05-06 17:06:36 +02:00
|
|
|
/** @var string User login */
|
|
|
|
protected $login = 'johndoe';
|
|
|
|
|
|
|
|
/** @var string User password */
|
|
|
|
protected $password = 'IC4nHazL0g1n?';
|
|
|
|
|
|
|
|
/** @var string Hash of the salted user password */
|
|
|
|
protected $passwordHash = '';
|
|
|
|
|
|
|
|
/** @var string Salt used by hash functions */
|
|
|
|
protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2';
|
|
|
|
|
2017-10-25 23:03:31 +02:00
|
|
|
/**
|
|
|
|
* Prepare or reset test resources
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
if (file_exists($this->banFile)) {
|
|
|
|
unlink($this->banFile);
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:06:36 +02:00
|
|
|
$this->passwordHash = sha1($this->password . $this->login . $this->salt);
|
|
|
|
|
2017-10-25 23:03:31 +02:00
|
|
|
$this->configManager = new \FakeConfigManager([
|
2018-05-06 17:06:36 +02:00
|
|
|
'credentials.login' => $this->login,
|
|
|
|
'credentials.hash' => $this->passwordHash,
|
|
|
|
'credentials.salt' => $this->salt,
|
2017-10-25 23:03:31 +02:00
|
|
|
'resource.ban_file' => $this->banFile,
|
|
|
|
'resource.log' => $this->logFile,
|
2019-02-09 16:44:48 +01:00
|
|
|
'security.ban_after' => 2,
|
2017-10-25 23:03:31 +02:00
|
|
|
'security.ban_duration' => 3600,
|
|
|
|
'security.trusted_proxies' => [$this->trustedProxy],
|
2020-03-02 17:08:19 +01:00
|
|
|
'ldap.host' => '',
|
2017-10-25 23:03:31 +02:00
|
|
|
]);
|
|
|
|
|
2018-05-06 17:12:48 +02:00
|
|
|
$this->cookie = [];
|
2018-05-30 02:09:09 +02:00
|
|
|
$this->session = [];
|
2018-05-06 17:12:48 +02:00
|
|
|
|
|
|
|
$this->sessionManager = new SessionManager($this->session, $this->configManager);
|
2019-02-09 16:44:48 +01:00
|
|
|
$this->loginManager = new LoginManager($this->configManager, $this->sessionManager);
|
2017-10-25 23:03:31 +02:00
|
|
|
$this->server['REMOTE_ADDR'] = $this->ipAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record a failed login attempt
|
|
|
|
*/
|
|
|
|
public function testHandleFailedLogin()
|
|
|
|
{
|
|
|
|
$this->loginManager->handleFailedLogin($this->server);
|
|
|
|
$this->loginManager->handleFailedLogin($this->server);
|
2019-02-09 16:44:48 +01:00
|
|
|
$this->assertFalse($this->loginManager->canLogin($this->server));
|
2017-10-25 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record a failed login attempt - IP behind a trusted proxy
|
|
|
|
*/
|
|
|
|
public function testHandleFailedLoginBehindTrustedProxy()
|
|
|
|
{
|
|
|
|
$server = [
|
|
|
|
'REMOTE_ADDR' => $this->trustedProxy,
|
|
|
|
'HTTP_X_FORWARDED_FOR' => $this->ipAddr,
|
|
|
|
];
|
|
|
|
$this->loginManager->handleFailedLogin($server);
|
|
|
|
$this->loginManager->handleFailedLogin($server);
|
2019-02-09 16:44:48 +01:00
|
|
|
$this->assertFalse($this->loginManager->canLogin($server));
|
2017-10-25 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record a failed login attempt - IP behind a trusted proxy but not forwarded
|
|
|
|
*/
|
|
|
|
public function testHandleFailedLoginBehindTrustedProxyNoIp()
|
|
|
|
{
|
|
|
|
$server = [
|
|
|
|
'REMOTE_ADDR' => $this->trustedProxy,
|
|
|
|
];
|
|
|
|
$this->loginManager->handleFailedLogin($server);
|
|
|
|
$this->loginManager->handleFailedLogin($server);
|
2019-02-09 16:44:48 +01:00
|
|
|
$this->assertTrue($this->loginManager->canLogin($server));
|
2017-10-25 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nothing to do
|
|
|
|
*/
|
|
|
|
public function testHandleSuccessfulLogin()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->loginManager->canLogin($this->server));
|
|
|
|
|
|
|
|
$this->loginManager->handleSuccessfulLogin($this->server);
|
|
|
|
$this->assertTrue($this->loginManager->canLogin($this->server));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Erase failure records after successfully logging in from this IP
|
|
|
|
*/
|
|
|
|
public function testHandleSuccessfulLoginAfterFailure()
|
|
|
|
{
|
|
|
|
$this->loginManager->handleFailedLogin($this->server);
|
|
|
|
$this->assertTrue($this->loginManager->canLogin($this->server));
|
|
|
|
|
|
|
|
$this->loginManager->handleSuccessfulLogin($this->server);
|
2019-02-09 16:44:48 +01:00
|
|
|
$this->loginManager->handleFailedLogin($this->server);
|
2017-10-25 23:03:31 +02:00
|
|
|
$this->assertTrue($this->loginManager->canLogin($this->server));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The IP is not banned
|
|
|
|
*/
|
|
|
|
public function testCanLoginIpNotBanned()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->loginManager->canLogin($this->server));
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:06:36 +02:00
|
|
|
/**
|
|
|
|
* Generate a token depending on the user credentials and client IP
|
|
|
|
*/
|
|
|
|
public function testGenerateStaySignedInToken()
|
|
|
|
{
|
2018-05-06 17:12:48 +02:00
|
|
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
2018-05-06 17:06:36 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2018-05-06 17:12:48 +02:00
|
|
|
sha1($this->passwordHash . $this->clientIpAddress . $this->salt),
|
2018-05-06 17:06:36 +02:00
|
|
|
$this->loginManager->getStaySignedInToken()
|
|
|
|
);
|
|
|
|
}
|
2018-05-06 17:12:48 +02:00
|
|
|
|
2018-07-17 14:13:37 +02:00
|
|
|
/**
|
|
|
|
* Generate a token depending on the user credentials with session protected disabled
|
|
|
|
*/
|
|
|
|
public function testGenerateStaySignedInTokenSessionProtectionDisabled()
|
|
|
|
{
|
|
|
|
$this->configManager->set('security.session_protection_disabled', true);
|
|
|
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sha1($this->passwordHash . $this->salt),
|
|
|
|
$this->loginManager->getStaySignedInToken()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:12:48 +02:00
|
|
|
/**
|
|
|
|
* Check user login - Shaarli has not yet been configured
|
|
|
|
*/
|
|
|
|
public function testCheckLoginStateNotConfigured()
|
|
|
|
{
|
|
|
|
$configManager = new \FakeConfigManager([
|
|
|
|
'resource.ban_file' => $this->banFile,
|
|
|
|
]);
|
2019-02-09 16:44:48 +01:00
|
|
|
$loginManager = new LoginManager($configManager, null);
|
2018-05-06 17:12:48 +02:00
|
|
|
$loginManager->checkLoginState([], '');
|
|
|
|
|
|
|
|
$this->assertFalse($loginManager->isLoggedIn());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user login - the client cookie does not match the server token
|
|
|
|
*/
|
|
|
|
public function testCheckLoginStateStaySignedInWithInvalidToken()
|
|
|
|
{
|
2018-05-30 02:09:09 +02:00
|
|
|
// simulate a previous login
|
|
|
|
$this->session = [
|
|
|
|
'ip' => $this->clientIpAddress,
|
|
|
|
'expires_on' => time() + 100,
|
|
|
|
];
|
2018-05-06 17:12:48 +02:00
|
|
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
|
|
|
$this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope';
|
|
|
|
|
|
|
|
$this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
|
|
|
|
|
2018-05-30 02:09:09 +02:00
|
|
|
$this->assertTrue($this->loginManager->isLoggedIn());
|
|
|
|
$this->assertTrue(empty($this->session['username']));
|
2018-05-06 17:12:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user login - the client cookie matches the server token
|
|
|
|
*/
|
|
|
|
public function testCheckLoginStateStaySignedInWithValidToken()
|
|
|
|
{
|
|
|
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
|
|
|
$this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken();
|
|
|
|
|
|
|
|
$this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
|
|
|
|
|
|
|
|
$this->assertTrue($this->loginManager->isLoggedIn());
|
2018-05-30 02:09:09 +02:00
|
|
|
$this->assertEquals($this->login, $this->session['username']);
|
|
|
|
$this->assertEquals($this->clientIpAddress, $this->session['ip']);
|
2018-05-06 17:12:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user login - the session has expired
|
|
|
|
*/
|
|
|
|
public function testCheckLoginStateSessionExpired()
|
|
|
|
{
|
|
|
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
|
|
|
$this->session['expires_on'] = time() - 100;
|
|
|
|
|
|
|
|
$this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
|
|
|
|
|
|
|
|
$this->assertFalse($this->loginManager->isLoggedIn());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user login - the remote client IP has changed
|
|
|
|
*/
|
|
|
|
public function testCheckLoginStateClientIpChanged()
|
|
|
|
{
|
|
|
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
|
|
|
|
|
|
|
$this->loginManager->checkLoginState($this->cookie, '10.7.157.98');
|
|
|
|
|
|
|
|
$this->assertFalse($this->loginManager->isLoggedIn());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user credentials - wrong login supplied
|
|
|
|
*/
|
|
|
|
public function testCheckCredentialsWrongLogin()
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user credentials - wrong password supplied
|
|
|
|
*/
|
|
|
|
public function testCheckCredentialsWrongPassword()
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user credentials - wrong login and password supplied
|
|
|
|
*/
|
|
|
|
public function testCheckCredentialsWrongLoginAndPassword()
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user credentials - correct login and password supplied
|
|
|
|
*/
|
|
|
|
public function testCheckCredentialsGoodLoginAndPassword()
|
|
|
|
{
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->loginManager->checkCredentials('', '', $this->login, $this->password)
|
|
|
|
);
|
|
|
|
}
|
2020-03-02 17:08:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user credentials through LDAP - server unreachable
|
|
|
|
*/
|
|
|
|
public function testCheckCredentialsFromUnreachableLdap()
|
|
|
|
{
|
|
|
|
$this->configManager->set('ldap.host', 'dummy');
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->loginManager->checkCredentials('', '', $this->login, $this->password)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user credentials through LDAP - wrong login and password supplied
|
|
|
|
*/
|
|
|
|
public function testCheckCredentialsFromLdapWrongLoginAndPassword()
|
|
|
|
{
|
2020-03-02 18:23:55 +01:00
|
|
|
$this->configManager->set('ldap.host', 'dummy');
|
2020-03-02 17:08:19 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$this->loginManager->checkCredentialsFromLdap($this->login, $this->password, function() { return null; }, function() { return false; })
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user credentials through LDAP - correct login and password supplied
|
|
|
|
*/
|
|
|
|
public function testCheckCredentialsFromLdapGoodLoginAndPassword()
|
|
|
|
{
|
|
|
|
$this->configManager->set('ldap.host', 'dummy');
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->loginManager->checkCredentialsFromLdap($this->login, $this->password, function() { return null; }, function() { return true; })
|
|
|
|
);
|
|
|
|
}
|
2017-10-25 23:03:31 +02:00
|
|
|
}
|