Refactor authentication system (#2602)

Make auth configuration available via UI, remove FOSUserBundle and SAML-Bundle dependency
This commit is contained in:
Kevin Papst
2021-06-10 15:34:13 +02:00
committed by GitHub
parent 286b63e2c8
commit 7f20cb045c
155 changed files with 5590 additions and 1802 deletions

View File

@@ -10,10 +10,12 @@
namespace App\Tests\Ldap;
use App\Configuration\LdapConfiguration;
use App\Configuration\SystemConfiguration;
use App\Entity\User;
use App\Ldap\LdapAuthenticationProvider;
use App\Ldap\LdapManager;
use App\Ldap\LdapUserProvider;
use App\Tests\Configuration\TestConfigLoader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
@@ -26,10 +28,33 @@ use Symfony\Component\Security\Core\User\UserChecker;
*/
class LdapAuthenticationProviderTest extends TestCase
{
private function getConfiguration(bool $active = true): LdapConfiguration
{
$systemConfig = new SystemConfiguration(new TestConfigLoader([]), ['ldap' => ['activate' => $active]]);
$config = new LdapConfiguration($systemConfig);
return $config;
}
public function testSupports()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(false);
$userProvider = new LdapUserProvider($manager);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
$token = new UsernamePasswordToken('foo', 'bar', $providerKey);
$sut = new LdapAuthenticationProvider($userChecker, $providerKey, $userProvider, $manager, $config, false);
$result = $sut->supports($token);
self::assertFalse($result);
}
public function testSupportsActive()
{
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = $this->getConfiguration(true);
$userProvider = new LdapUserProvider($manager);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -47,7 +72,7 @@ class LdapAuthenticationProviderTest extends TestCase
$this->expectExceptionMessage('The password in the token is empty. Check `erase_credentials` in your `security.yaml`');
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(true);
$userProvider = new LdapUserProvider($manager);
$providerKey = 'secured_area';
$userChecker = new UserChecker();
@@ -66,7 +91,7 @@ class LdapAuthenticationProviderTest extends TestCase
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$providerKey = 'secured_area';
@@ -86,7 +111,7 @@ class LdapAuthenticationProviderTest extends TestCase
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->onlyMethods(['bind'])->getMock();
$manager->expects($this->once())->method('bind')->willReturn(false);
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$providerKey = 'secured_area';
@@ -106,7 +131,7 @@ class LdapAuthenticationProviderTest extends TestCase
$user = (new User())->setUsername('foo')->setEnabled(true);
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->onlyMethods(['bind'])->getMock();
$manager->expects($this->once())->method('bind')->willReturn(false);
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->never())->method('loadUserByUsername');
$providerKey = 'secured_area';
@@ -127,7 +152,7 @@ class LdapAuthenticationProviderTest extends TestCase
$manager->expects($this->once())->method('updateUser')->willReturnCallback(function ($updateUser) use ($user) {
self::assertSame($updateUser, $user);
});
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willReturn($user);
$providerKey = 'secured_area';
@@ -149,7 +174,7 @@ class LdapAuthenticationProviderTest extends TestCase
$manager->expects($this->once())->method('updateUser')->willReturnCallback(function ($updateUser) use ($user) {
self::assertSame($updateUser, $user);
});
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->never())->method('loadUserByUsername');
$providerKey = 'secured_area';
@@ -168,7 +193,7 @@ class LdapAuthenticationProviderTest extends TestCase
$this->expectExceptionMessage('blub foo bar');
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willThrowException(new UsernameNotFoundException('blub foo bar'));
$providerKey = 'secured_area';
@@ -187,7 +212,7 @@ class LdapAuthenticationProviderTest extends TestCase
$this->expectExceptionCode('1234');
$manager = $this->getMockBuilder(LdapManager::class)->disableOriginalConstructor()->getMock();
$config = new LdapConfiguration([]);
$config = $this->getConfiguration(true);
$userProvider = $this->getMockBuilder(LdapUserProvider::class)->disableOriginalConstructor()->onlyMethods(['loadUserByUsername'])->getMock();
$userProvider->expects($this->once())->method('loadUserByUsername')->willThrowException(new \Exception('server away', 1234));
$providerKey = 'secured_area';

View File

@@ -10,11 +10,13 @@
namespace App\Tests\Ldap;
use App\Configuration\LdapConfiguration;
use App\Configuration\SystemConfiguration;
use App\Entity\User;
use App\Ldap\LdapDriver;
use App\Ldap\LdapDriverException;
use App\Ldap\LdapManager;
use App\Ldap\LdapUserHydrator;
use App\Tests\Configuration\TestConfigLoader;
use App\Tests\Mocks\Security\RoleServiceFactory;
use PHPUnit\Framework\TestCase;
@@ -39,7 +41,7 @@ class LdapManagerTest extends TestCase
];
}
$config = new LdapConfiguration([
$conf = [
'user' => [
'attributes' => [],
'filter' => '(&(objectClass=inetOrgPerson))',
@@ -48,7 +50,9 @@ class LdapManagerTest extends TestCase
'baseDn' => 'ou=users, dc=kimai, dc=org',
],
'role' => $roleConfig,
]);
];
$systemConfig = new SystemConfiguration(new TestConfigLoader([]), ['ldap' => $conf]);
$config = new LdapConfiguration($systemConfig);
$roles = [
'ROLE_TEAMLEAD' => ['ROLE_USER'],

View File

@@ -10,8 +10,10 @@
namespace App\Tests\Ldap;
use App\Configuration\LdapConfiguration;
use App\Configuration\SystemConfiguration;
use App\Entity\User;
use App\Ldap\LdapUserHydrator;
use App\Tests\Configuration\TestConfigLoader;
use App\Tests\Mocks\Security\RoleServiceFactory;
use PHPUnit\Framework\TestCase;
@@ -22,7 +24,8 @@ class LdapUserHydratorTest extends TestCase
{
public function testEmptyHydrate()
{
$config = new LdapConfiguration([
$ldapConfig = [
'activate' => true,
'connection' => [
'host' => '1.1.1.1'
],
@@ -31,7 +34,10 @@ class LdapUserHydratorTest extends TestCase
'attributes' => []
],
'role' => [],
]);
];
$systemConfig = new SystemConfiguration(new TestConfigLoader([]), ['ldap' => $ldapConfig]);
$config = new LdapConfiguration($systemConfig);
$sut = new LdapUserHydrator($config, (new RoleServiceFactory($this))->create([]));
$user = $sut->hydrate(['dn' => 'blub']);
@@ -42,7 +48,7 @@ class LdapUserHydratorTest extends TestCase
public function testHydrate()
{
$config = new LdapConfiguration([
$ldapConfig = [
'connection' => [
'host' => '1.1.1.1'
],
@@ -58,7 +64,9 @@ class LdapUserHydratorTest extends TestCase
]
],
'role' => [],
]);
];
$systemConfig = new SystemConfiguration(new TestConfigLoader([]), ['ldap' => $ldapConfig]);
$config = new LdapConfiguration($systemConfig);
$ldapEntry = [
'uid' => ['Karl-Heinz'],
@@ -85,7 +93,7 @@ class LdapUserHydratorTest extends TestCase
public function testHydrateUser()
{
$config = new LdapConfiguration([
$ldapConfig = [
'connection' => [
'host' => '1.1.1.1'
],
@@ -100,7 +108,9 @@ class LdapUserHydratorTest extends TestCase
]
],
'role' => [],
]);
];
$systemConfig = new SystemConfiguration(new TestConfigLoader([]), ['ldap' => $ldapConfig]);
$config = new LdapConfiguration($systemConfig);
$ldapEntry = [
'uid' => ['Karl-Heinz'],
@@ -131,7 +141,7 @@ class LdapUserHydratorTest extends TestCase
public function testHydrateRoles()
{
$config = new LdapConfiguration([
$ldapConfig = [
'user' => [
'attributes' => []
],
@@ -145,7 +155,9 @@ class LdapUserHydratorTest extends TestCase
['ldap_value' => 'group4', 'role' => 'ROLE_SUPER_ADMIN'],
],
],
]);
];
$systemConfig = new SystemConfiguration(new TestConfigLoader([]), ['ldap' => $ldapConfig]);
$config = new LdapConfiguration($systemConfig);
$ldapGroups = [
// ROLE_TEAMLEAD