mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-24 06:14:25 +02:00
parent
69aa6e3d22
commit
1dfc578c00
@ -29,38 +29,43 @@
|
|||||||
|
|
||||||
namespace Tests\Icinga\Authentication;
|
namespace Tests\Icinga\Authentication;
|
||||||
|
|
||||||
require_once('Zend/Config.php');
|
// @codingStandardsIgnoreStart
|
||||||
require_once('Zend/Config/Ini.php');
|
require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCase.php');
|
||||||
require_once('Zend/Db/Adapter/Abstract.php');
|
// @codingStandardsIgnoreEnd
|
||||||
require_once('Zend/Db.php');
|
|
||||||
require_once('Zend/Log.php');
|
|
||||||
require_once('../../library/Icinga/Util/ConfigAwareFactory.php');
|
|
||||||
require_once('../../library/Icinga/Authentication/UserBackend.php');
|
|
||||||
require_once('../../library/Icinga/Protocol/Ldap/Exception.php');
|
|
||||||
require_once('../../library/Icinga/Application/DbAdapterFactory.php');
|
|
||||||
require_once('../../library/Icinga/Application/Config.php');
|
|
||||||
require_once('../../library/Icinga/Authentication/Credentials.php');
|
|
||||||
require_once('../../library/Icinga/Authentication/Backend/DbUserBackend.php');
|
|
||||||
require_once('../../library/Icinga/User.php');
|
|
||||||
require_once('../../library/Icinga/Application/Logger.php');
|
|
||||||
|
|
||||||
use Zend_Config;
|
use \Icinga\Test\BaseTestCase;
|
||||||
use Zend_Db_Adapter_Abstract;
|
|
||||||
use Icinga\Authentication\Backend\DbUserBackend;
|
// @codingStandardsIgnoreStart
|
||||||
use Icinga\Application\DbAdapterFactory;
|
require_once 'Zend/Config.php';
|
||||||
use Icinga\Util\Crypto;
|
require_once 'Zend/Config/Ini.php';
|
||||||
use Icinga\Authentication\Credentials;
|
require_once 'Zend/Db/Adapter/Abstract.php';
|
||||||
use Icinga\User;
|
require_once 'Zend/Db.php';
|
||||||
|
require_once 'Zend/Log.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/Protocol/Ldap/Exception.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/Application/DbAdapterFactory.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/Application/Config.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/Authentication/Credentials.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/Authentication/Backend/DbUserBackend.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/User.php';
|
||||||
|
require_once BaseTestCase::$libDir . '/Application/Logger.php';
|
||||||
|
// @codingStandardsIgnoreEnd
|
||||||
|
|
||||||
|
use \PDO;
|
||||||
|
use \Zend_Db_Adapter_Pdo_Abstract;
|
||||||
|
use \Zend_Config;
|
||||||
|
use \Icinga\Authentication\Backend\DbUserBackend;
|
||||||
|
use \Icinga\Application\DbAdapterFactory;
|
||||||
|
use \Icinga\Authentication\Credentials;
|
||||||
|
use \Icinga\User;
|
||||||
use \Icinga\Application\Config;
|
use \Icinga\Application\Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test Class fpr DbUserBackend
|
* Test Class fpr DbUserBackend
|
||||||
*/
|
*/
|
||||||
class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
class DbUserBackendTest extends BaseTestCase
|
||||||
|
{
|
||||||
/*
|
|
||||||
* Mapping of columns
|
|
||||||
*/
|
|
||||||
const USER_NAME_COLUMN = 'username';
|
const USER_NAME_COLUMN = 'username';
|
||||||
|
|
||||||
const SALT_COLUMN = 'salt';
|
const SALT_COLUMN = 'salt';
|
||||||
@ -76,263 +81,157 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||||||
*/
|
*/
|
||||||
private $testTable = 'account';
|
private $testTable = 'account';
|
||||||
|
|
||||||
/**
|
|
||||||
* The database that is used to store the authentication data
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $testDatabase = 'icinga_unittest';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example users
|
* Example users
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $users;
|
private $userData = array(
|
||||||
|
array(
|
||||||
/**
|
self::USER_NAME_COLUMN => 'user1',
|
||||||
* The DbUserBackend configured to use MySQL
|
self::PASSWORD_COLUMN => 'secret1',
|
||||||
*
|
self::SALT_COLUMN => '8a7487a539c5d1d6766639d04d1ed1e6',
|
||||||
* @var DbUserBackend
|
self::ACTIVE_COLUMN => 1
|
||||||
*/
|
),
|
||||||
private $mysql;
|
array(
|
||||||
|
self::USER_NAME_COLUMN => 'user2',
|
||||||
|
self::PASSWORD_COLUMN => 'secret2',
|
||||||
/**
|
self::SALT_COLUMN => '04b5521ddd761b5a5b633be83faa494d',
|
||||||
* The DbUserBackend configured to use PostgreSQL
|
self::ACTIVE_COLUMN => 1
|
||||||
*
|
),
|
||||||
* @var DbUserBackend
|
array(
|
||||||
*/
|
self::USER_NAME_COLUMN => 'user3',
|
||||||
private $pgsql;
|
self::PASSWORD_COLUMN => 'secret3',
|
||||||
|
self::SALT_COLUMN => '08bb94ba3120338ae56db80ef551d324',
|
||||||
/**
|
self::ACTIVE_COLUMN => 0
|
||||||
* Contains the PDO names used for the different SQL databases.
|
)
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $dbTypeMap = Array(
|
|
||||||
'mysql' => 'PDO_MYSQL',
|
|
||||||
'pgsql' => 'PDO_PGSQL'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a preset configuration that can be used to access the database
|
|
||||||
* with the icinga_unittest account.
|
|
||||||
*
|
|
||||||
* @param String $dbType The database type as a string, like 'mysql' or 'pgsql'.
|
|
||||||
*
|
|
||||||
* @return Zend_Config The created resource configuration
|
|
||||||
*/
|
|
||||||
private function getResourceConfig($dbType)
|
|
||||||
{
|
|
||||||
return new Zend_Config(
|
|
||||||
array(
|
|
||||||
'type' => 'db',
|
|
||||||
'db' => $dbType,
|
|
||||||
'host' => 'localhost',
|
|
||||||
'username' => 'icinga_unittest',
|
|
||||||
'password' => 'icinga_unittest',
|
|
||||||
'dbname' => $this->testDatabase,
|
|
||||||
'table' => $this->testTable
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a backend with the given database type
|
|
||||||
*
|
|
||||||
* @param String $dbType The database type as a string, like 'mysql' or 'pgsql'.
|
|
||||||
*
|
|
||||||
* @return DbUserBackend|null
|
|
||||||
*/
|
|
||||||
private function createBackend($dbType)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$db = $this->createDb($this->getResourceConfig($dbType));
|
|
||||||
$this->setUpDb($db,$dbType);
|
|
||||||
return new DbUserBackend($db);
|
|
||||||
} catch(\Exception $e) {
|
|
||||||
echo 'CREATE_BACKEND_ERROR:'.$e->getMessage();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the db adapter
|
|
||||||
*
|
|
||||||
* @param $config The configuration to use
|
|
||||||
*
|
|
||||||
* @return Zend_Db_Adapter_Abstract The created adabter
|
|
||||||
*/
|
|
||||||
private function createDb($config)
|
|
||||||
{
|
|
||||||
return DbAdapterFactory::createDbAdapterFromConfig($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the backends and fill it with sample-data
|
|
||||||
*/
|
|
||||||
protected function setUp()
|
|
||||||
{
|
|
||||||
DbAdapterFactory::resetConfig();
|
|
||||||
$this->users = Array(
|
|
||||||
0 => Array(
|
|
||||||
self::USER_NAME_COLUMN => 'user1',
|
|
||||||
self::PASSWORD_COLUMN => 'secret1',
|
|
||||||
self::SALT_COLUMN => '8a7487a539c5d1d6766639d04d1ed1e6',
|
|
||||||
self::ACTIVE_COLUMN => 1
|
|
||||||
),
|
|
||||||
1 => Array(
|
|
||||||
self::USER_NAME_COLUMN => 'user2',
|
|
||||||
self::PASSWORD_COLUMN => 'secret2',
|
|
||||||
self::SALT_COLUMN => '04b5521ddd761b5a5b633be83faa494d',
|
|
||||||
self::ACTIVE_COLUMN => 1
|
|
||||||
),
|
|
||||||
2 => Array(
|
|
||||||
self::USER_NAME_COLUMN => 'user3',
|
|
||||||
self::PASSWORD_COLUMN => 'secret3',
|
|
||||||
self::SALT_COLUMN => '08bb94ba3120338ae56db80ef551d324',
|
|
||||||
self::ACTIVE_COLUMN => 0
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->mysql = $this->createBackend('mysql');
|
|
||||||
$this->pgsql = $this->createBackend('pgsql');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the authentication functions of the DbUserBackend using PostgreSQL as backend.
|
* Test the authentication functions of the DbUserBackend using PostgreSQL as backend.
|
||||||
|
*
|
||||||
|
* @dataProvider pgsqlDb
|
||||||
*/
|
*/
|
||||||
public function testCorrectUserLoginForPgsql()
|
public function testCorrectUserLoginForPgsql($db)
|
||||||
{
|
{
|
||||||
if (!empty($this->pgsql)) {
|
$this->setupDbProvider($db);
|
||||||
$this->runBackendAuthentication($this->pgsql);
|
$backend = new DbUserBackend($db);
|
||||||
$this->runBackendUsername($this->pgsql);
|
$this->runBackendAuthentication($backend);
|
||||||
}
|
$this->runBackendUsername($backend);
|
||||||
else {
|
|
||||||
$this->markTestSkipped();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the authentication functions of the DbUserBackend using MySQL as backend.
|
* Test the authentication functions of the DbUserBackend using MySQL as backend.
|
||||||
*/
|
|
||||||
public function testCorrectUserLoginForMySQL()
|
|
||||||
{
|
|
||||||
if(!empty($this->mysql)){
|
|
||||||
$this->runBackendAuthentication($this->mysql);
|
|
||||||
$this->runBackendUsername($this->mysql);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$this->markTestSkipped();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Try to drop all databases that may eventually be present
|
|
||||||
*/
|
|
||||||
public function tearDown()
|
|
||||||
{
|
|
||||||
try{
|
|
||||||
$db = $this->createDb($this->getResourceConfig('mysql'));
|
|
||||||
$this->tearDownDb($db);
|
|
||||||
} catch(\Exception $e) { }
|
|
||||||
try {
|
|
||||||
$db = $this->createDb($this->getResourceConfig('pgsql'));
|
|
||||||
$this->tearDownDb($db);
|
|
||||||
} catch(\Exception $e) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Drop the test database in the given db
|
|
||||||
*
|
*
|
||||||
* @param $db
|
* @dataProvider mysqlDb
|
||||||
*/
|
*/
|
||||||
private function tearDownDb($db)
|
public function testCorrectUserLoginForMySQL($db)
|
||||||
{
|
{
|
||||||
$db->exec('DROP TABLE '.$this->testTable);
|
$this->setupDbProvider($db);
|
||||||
|
$backend = new DbUserBackend($db);
|
||||||
|
$this->runBackendAuthentication($backend);
|
||||||
|
$this->runBackendUsername($backend);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fill the given database with the sample-data provided in users
|
* @param Zend_Db_Adapter_Pdo_Abstract $resource
|
||||||
*
|
|
||||||
* @param $db Zend_Db_Adapter_Abstract The database to set up
|
|
||||||
* @param $type String The database type as a string: 'mysql'|'pgsql'
|
|
||||||
*/
|
*/
|
||||||
private function setUpDb($db,$type)
|
public function setupDbProvider($resource)
|
||||||
{
|
{
|
||||||
try {
|
parent::setupDbProvider($resource);
|
||||||
$this->tearDownDb($db);
|
|
||||||
} catch (\Exception $e) {}
|
|
||||||
|
|
||||||
$setupScript = file_get_contents('../../etc/schema/accounts.' . $type . '.sql');
|
$type = $resource->getConnection()->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||||
$db->exec($setupScript);
|
|
||||||
|
|
||||||
for ($i = 0; $i < count($this->users); $i++) {
|
$dumpFile = BaseTestCase::$etcDir . '/schema/accounts.' . $type . '.sql';
|
||||||
$usr = $this->users[$i];
|
|
||||||
$data = Array(
|
$this->assertFileExists($dumpFile);
|
||||||
self::USER_NAME_COLUMN => $usr[self::USER_NAME_COLUMN],
|
|
||||||
self::PASSWORD_COLUMN => hash_hmac('sha256',
|
$this->loadSql($resource, $dumpFile);
|
||||||
|
|
||||||
|
for ($i = 0; $i < count($this->userData); $i++) {
|
||||||
|
$usr = $this->userData[$i];
|
||||||
|
$data = array(
|
||||||
|
self::USER_NAME_COLUMN => $usr[self::USER_NAME_COLUMN],
|
||||||
|
self::PASSWORD_COLUMN => hash_hmac(
|
||||||
|
'sha256',
|
||||||
$usr[self::SALT_COLUMN],
|
$usr[self::SALT_COLUMN],
|
||||||
$usr[self::PASSWORD_COLUMN]
|
$usr[self::PASSWORD_COLUMN]
|
||||||
),
|
),
|
||||||
self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN],
|
self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN],
|
||||||
self::SALT_COLUMN => $usr[self::SALT_COLUMN]
|
self::SALT_COLUMN => $usr[self::SALT_COLUMN]
|
||||||
);
|
);
|
||||||
$db->insert($this->testTable,$data);
|
$resource->insert($this->testTable, $data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the hasUsername test against an instance of DbUserBackend
|
* Run the hasUsername test against an instance of DbUserBackend
|
||||||
*
|
*
|
||||||
* @param $backend The backend that will be tested.
|
* @param DbUserBackend $backend The backend that will be tested.
|
||||||
*/
|
*/
|
||||||
private function runBackendUsername($backend)
|
private function runBackendUsername($backend)
|
||||||
{
|
{
|
||||||
// Known user
|
// Known user
|
||||||
$this->assertTrue($backend->hasUsername(
|
$this->assertTrue(
|
||||||
new Credentials(
|
$backend->hasUsername(
|
||||||
$this->users[0][self::USER_NAME_COLUMN],
|
new Credentials(
|
||||||
$this->users[0][self::PASSWORD_COLUMN])
|
$this->userData[0][self::USER_NAME_COLUMN],
|
||||||
), 'Assert that the user is known by the backend');
|
$this->userData[0][self::PASSWORD_COLUMN]
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'Assert that the user is known by the backend'
|
||||||
|
);
|
||||||
|
|
||||||
// Unknown user
|
// Unknown user
|
||||||
$this->assertFalse($backend->hasUsername(
|
$this->assertFalse(
|
||||||
new Credentials(
|
$backend->hasUsername(
|
||||||
'unknown user',
|
new Credentials(
|
||||||
'secret')
|
'unknown user',
|
||||||
), 'Assert that the user is not known by the backend');
|
'secret'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'Assert that the user is not known by the backend'
|
||||||
|
);
|
||||||
|
|
||||||
// Inactive user
|
// Inactive user
|
||||||
$this->assertFalse($backend->hasUsername(
|
$this->assertFalse(
|
||||||
new Credentials(
|
$backend->hasUsername(
|
||||||
$this->users[2][self::USER_NAME_COLUMN],
|
new Credentials(
|
||||||
$this->users[2][self::PASSWORD_COLUMN])
|
$this->userData[2][self::USER_NAME_COLUMN],
|
||||||
), 'Assert that the user is inactive and therefore not known by the backend');
|
$this->userData[2][self::PASSWORD_COLUMN]
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'Assert that the user is inactive and therefore not known by the backend'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the authentication test against an instance of DbUserBackend
|
* Run the authentication test against an instance of DbUserBackend
|
||||||
*
|
*
|
||||||
* @param $backend The backend that will be tested.
|
* @param DbUserBackend $backend The backend that will be tested.
|
||||||
*/
|
*/
|
||||||
private function runBackendAuthentication($backend)
|
private function runBackendAuthentication($backend)
|
||||||
{
|
{
|
||||||
// Known user
|
// Known user
|
||||||
$this->assertNotNull($backend->authenticate(
|
$this->assertNotNull(
|
||||||
new Credentials(
|
$backend->authenticate(
|
||||||
$this->users[0][self::USER_NAME_COLUMN],
|
new Credentials(
|
||||||
$this->users[0][self::PASSWORD_COLUMN])
|
$this->userData[0][self::USER_NAME_COLUMN],
|
||||||
), 'Assert that an existing, active user with the right credentials can authenticate.');
|
$this->userData[0][self::PASSWORD_COLUMN]
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'Assert that an existing, active user with the right credentials can authenticate.'
|
||||||
|
);
|
||||||
|
|
||||||
// Wrong password
|
// Wrong password
|
||||||
$this->assertNull(
|
$this->assertNull(
|
||||||
$backend->authenticate(
|
$backend->authenticate(
|
||||||
new Credentials(
|
new Credentials(
|
||||||
$this->users[1][self::USER_NAME_COLUMN],
|
$this->userData[1][self::USER_NAME_COLUMN],
|
||||||
'wrongpassword')
|
'wrongpassword'
|
||||||
), 'Assert that an existing user with an invalid password cannot authenticate'
|
)
|
||||||
|
),
|
||||||
|
'Assert that an existing user with an invalid password cannot authenticate'
|
||||||
);
|
);
|
||||||
|
|
||||||
// Nonexisting user
|
// Nonexisting user
|
||||||
@ -340,16 +239,21 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$backend->authenticate(
|
$backend->authenticate(
|
||||||
new Credentials(
|
new Credentials(
|
||||||
'nonexisting user',
|
'nonexisting user',
|
||||||
$this->users[1][self::PASSWORD_COLUMN])
|
$this->userData[1][self::PASSWORD_COLUMN]
|
||||||
), 'Assert that a non-existing user cannot authenticate.'
|
)
|
||||||
|
),
|
||||||
|
'Assert that a non-existing user cannot authenticate.'
|
||||||
);
|
);
|
||||||
|
|
||||||
// Inactive user
|
// Inactive user
|
||||||
$this->assertNull(
|
$this->assertNull(
|
||||||
$backend->authenticate(
|
$backend->authenticate(
|
||||||
new Credentials(
|
new Credentials(
|
||||||
$this->users[2][self::USER_NAME_COLUMN],
|
$this->userData[2][self::USER_NAME_COLUMN],
|
||||||
$this->users[2][self::PASSWORD_COLUMN])
|
$this->userData[2][self::PASSWORD_COLUMN]
|
||||||
), 'Assert that an inactive user cannot authenticate.');
|
)
|
||||||
|
),
|
||||||
|
'Assert that an inactive user cannot authenticate.'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user