Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)

This commit is contained in:
Kevin Papst
2022-12-31 21:19:55 +01:00
committed by GitHub
parent 95e06746bd
commit 90a0fd8a22
2164 changed files with 83700 additions and 86426 deletions

View File

@@ -1,144 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Saml\Provider;
use App\Configuration\SamlConfiguration;
use App\Configuration\SystemConfiguration;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Saml\Provider\SamlProvider;
use App\Saml\SamlTokenFactory;
use App\Saml\Token\SamlToken;
use App\Saml\User\SamlUserFactory;
use App\Security\DoctrineUserProvider;
use App\Tests\Configuration\TestConfigLoader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\ChainUserProvider;
/**
* @covers \App\Saml\Provider\SamlProvider
*/
class SamlProviderTest extends TestCase
{
protected function getSamlProvider(array $mapping = null, ?User $user = null, ?SamlUserFactory $userFactory = null): SamlProvider
{
if (null === $mapping) {
$mapping = [
'mapping' => [
['saml' => '$Email', 'kimai' => 'email'],
['saml' => '$title', 'kimai' => 'title'],
],
'roles' => [
'attribute' => '',
'mapping' => []
]
];
}
if (null === $userFactory) {
$configuration = new SystemConfiguration(new TestConfigLoader([]), [
'saml' => $mapping
]);
$userFactory = new SamlUserFactory(new SamlConfiguration($configuration));
}
$systemConfig = new SystemConfiguration(new TestConfigLoader([]), ['saml' => ['activate' => true]]);
$samlConfig = new SamlConfiguration($systemConfig);
$repository = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
if ($user !== null) {
$repository->expects($this->once())->method('loadUserByUsername')->willReturn($user);
}
$userProvider = new ChainUserProvider([new DoctrineUserProvider($repository)]);
$provider = new SamlProvider($repository, $userProvider, new SamlTokenFactory(), $userFactory, $samlConfig);
return $provider;
}
public function testSupportsToken()
{
$sut = $this->getSamlProvider();
self::assertFalse($sut->supports(new AnonymousToken('ads', 'ads')));
self::assertFalse($sut->supports(new UsernamePasswordToken('ads', 'ads', 'asd')));
self::assertTrue($sut->supports(new SamlToken([])));
}
public function testAuthenticateHydratesUser()
{
$user = new User();
$user->setAuth(User::AUTH_SAML);
$token = new SamlToken([]);
$token->setUser('foo1@example.com');
$token->setAttributes([
'Email' => ['foo@example.com'],
'title' => ['Tralalala'],
]);
self::assertFalse($token->isAuthenticated());
$sut = $this->getSamlProvider(null, $user);
$authToken = $sut->authenticate($token);
self::assertTrue($authToken->isAuthenticated());
/** @var User $tokenUser */
$tokenUser = $authToken->getUser();
self::assertSame($user, $tokenUser);
self::assertEquals('foo1@example.com', $tokenUser->getUsername());
self::assertEquals('Tralalala', $tokenUser->getTitle());
self::assertEquals('foo@example.com', $tokenUser->getEmail());
}
public function testAuthenticatCreatesNewUser()
{
$token = new SamlToken([]);
$token->setUser('foo1@example.com');
$token->setAttributes([
'Email' => ['foo@example.com'],
'title' => ['Tralalala'],
]);
self::assertFalse($token->isAuthenticated());
$sut = $this->getSamlProvider(null);
$authToken = $sut->authenticate($token);
self::assertTrue($authToken->isAuthenticated());
/** @var User $tokenUser */
$tokenUser = $authToken->getUser();
self::assertEquals('foo1@example.com', $tokenUser->getUsername());
self::assertEquals('Tralalala', $tokenUser->getTitle());
self::assertEquals('foo@example.com', $tokenUser->getEmail());
}
public function testAuthenticateThrowsAuthenticationException()
{
$this->expectException(AuthenticationException::class);
$this->expectExceptionMessage('Failed creating or hydrating user "foo1@example.com": Missing user attribute: Email');
$user = new User();
$user->setAuth(User::AUTH_SAML);
$token = new SamlToken([]);
$token->setUser('foo1@example.com');
$token->setAttributes([
'Chicken' => ['foo@example.com'],
]);
$sut = $this->getSamlProvider(null, $user);
$sut->authenticate($token);
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Saml;
use App\Saml\SamlBadge;
use App\Saml\SamlLoginAttributes;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Saml\SamlBadge
*/
class SamlBadgeTest extends TestCase
{
public function testConstruct()
{
$attributes = new SamlLoginAttributes();
$sut = new SamlBadge($attributes);
self::assertTrue($sut->isResolved());
self::assertEquals($attributes, $sut->getSamlLoginAttributes());
}
}

View File

@@ -7,23 +7,23 @@
* file that was distributed with this source code.
*/
namespace App\Tests\Saml\Logout;
namespace App\Tests\Saml;
use App\Entity\User;
use App\Saml\Logout\SamlLogoutHandler;
use App\Saml\SamlAuthFactory;
use App\Saml\Token\SamlToken;
use App\Saml\SamlLogoutSubscriber;
use App\Saml\SamlToken;
use OneLogin\Saml2\Auth;
use OneLogin\Saml2\Error;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\LogoutEvent;
/**
* @covers \App\Saml\Logout\SamlLogoutHandler
* @covers \App\Saml\SamlLogoutSubscriber
*/
class SamlLogoutHandlerTest extends TestCase
class SamlLogoutSubscriberTest extends TestCase
{
public function testLogout()
{
@@ -32,14 +32,13 @@ class SamlLogoutHandlerTest extends TestCase
$auth->expects($this->once())->method('getSLOurl')->willReturn('');
$request = new Request();
$response = new Response();
$token = new SamlToken([]);
$token = new SamlToken(new User(), 'secured_area', []);
$factory = $this->getMockBuilder(SamlAuthFactory::class)->disableOriginalConstructor()->getMock();
$factory->expects($this->once())->method('create')->willReturn($auth);
$sut = new SamlLogoutHandler($factory);
$sut->logout($request, $response, $token);
$sut = new SamlLogoutSubscriber($factory);
$sut->logout(new LogoutEvent($request, $token));
}
public function testLogoutWithWrongTokenWillNotCallMethods()
@@ -49,14 +48,13 @@ class SamlLogoutHandlerTest extends TestCase
$auth->expects($this->never())->method('getSLOurl');
$request = new Request();
$response = new Response();
$token = new UsernamePasswordToken(new User(), [], 'test');
$token = new UsernamePasswordToken(new User(), 'test');
$factory = $this->getMockBuilder(SamlAuthFactory::class)->disableOriginalConstructor()->getMock();
$factory->expects($this->never())->method('create');
$sut = new SamlLogoutHandler($factory);
$sut->logout($request, $response, $token);
$sut = new SamlLogoutSubscriber($factory);
$sut->logout(new LogoutEvent($request, $token));
}
public function testLogoutWithLogoutUrl()
@@ -73,15 +71,15 @@ class SamlLogoutHandlerTest extends TestCase
});
$request = new Request();
$response = new Response();
$token = new SamlToken([]);
$token->setUser((new User())->setUsername('tony'));
$user = new User();
$user->setUserIdentifier('tony');
$token = new SamlToken($user, 'secured_area', []);
$token->setAttribute('sessionIndex', 'foo-bar');
$factory = $this->getMockBuilder(SamlAuthFactory::class)->disableOriginalConstructor()->getMock();
$factory->expects($this->once())->method('create')->willReturn($auth);
$sut = new SamlLogoutHandler($factory);
$sut->logout($request, $response, $token);
$sut = new SamlLogoutSubscriber($factory);
$sut->logout(new LogoutEvent($request, $token));
}
}

View File

@@ -0,0 +1,125 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Saml;
use App\Configuration\SamlConfiguration;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Saml\SamlLoginAttributes;
use App\Saml\SamlProvider;
use App\Tests\Configuration\TestConfigLoader;
use App\Tests\Mocks\SystemConfigurationFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* @covers \App\Saml\SamlProvider
*/
class SamlProviderTest extends TestCase
{
protected function getSamlProvider(array $mapping = null, ?User $user = null): SamlProvider
{
if (null === $mapping) {
$mapping = [
'mapping' => [
['saml' => '$Email', 'kimai' => 'email'],
['saml' => '$title', 'kimai' => 'title'],
],
'roles' => [
'attribute' => '',
'mapping' => []
]
];
}
$configuration = SystemConfigurationFactory::create(new TestConfigLoader([]), [
'saml' => $mapping
]);
$samlConfig = new SamlConfiguration($configuration);
// can be replaced, once loadUserByIdentifier is in the interface with SF6?
$userProvider = $this->getMockBuilder(UserProviderInterface::class)->disableOriginalConstructor();
$userProvider->onlyMethods(['refreshUser', 'supportsClass', 'loadUserByIdentifier']);
$userProvider = $userProvider->getMock();
$repository = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
if ($user !== null) {
$userProvider->method('loadUserByIdentifier')->willReturn($user);
} else {
$userProvider->method('loadUserByIdentifier')->willReturn(new User());
}
$provider = new SamlProvider($repository, $userProvider, $samlConfig);
return $provider;
}
public function testFindUserHydratesUser()
{
$user = new User();
$user->setAuth(User::AUTH_INTERNAL);
$user->setUserIdentifier('foo1@example.com');
$user->setTitle('jagfkjhsgf');
$token = new SamlLoginAttributes();
$token->setUserIdentifier($user->getUserIdentifier());
$token->setAttributes([
'Email' => ['foo@example.com'],
'title' => ['Tralalala'],
]);
$sut = $this->getSamlProvider(null, $user);
$tokenUser = $sut->findUser($token);
self::assertSame($user, $tokenUser);
self::assertTrue($tokenUser->isSamlUser());
self::assertEquals('foo1@example.com', $tokenUser->getUserIdentifier());
self::assertEquals('Tralalala', $tokenUser->getTitle());
self::assertEquals('foo@example.com', $tokenUser->getEmail());
}
public function testFindUserCreatesNewUser()
{
$token = new SamlLoginAttributes();
$token->setUserIdentifier('foo2@example.com');
$token->setAttributes([
'Email' => ['foo@example.com'],
'title' => ['Tralalala'],
]);
$sut = $this->getSamlProvider(null);
$tokenUser = $sut->findUser($token);
self::assertTrue($tokenUser->isSamlUser());
self::assertEquals('foo2@example.com', $tokenUser->getUserIdentifier());
self::assertEquals('Tralalala', $tokenUser->getTitle());
self::assertEquals('foo@example.com', $tokenUser->getEmail());
}
public function testAuthenticateThrowsAuthenticationException()
{
$this->expectException(AuthenticationException::class);
$this->expectExceptionMessage('Failed creating or hydrating user "foo1@example.com": Missing user attribute: Email');
$user = new User();
$user->setAuth(User::AUTH_SAML);
$user->setUserIdentifier('foo1@example.com');
$token = new SamlLoginAttributes();
$token->setUserIdentifier($user->getUserIdentifier());
$token->setAttributes([
'Chicken' => ['foo@example.com'],
]);
$sut = $this->getSamlProvider(null, $user);
$sut->findUser($token);
}
}

View File

@@ -1,37 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Saml;
use App\Entity\User;
use App\Saml\SamlTokenFactory;
use App\Saml\Token\SamlToken;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Saml\SamlTokenFactory
*/
class SamlTokenFactoryTest extends TestCase
{
public function testCreateToken()
{
$user = new User();
$user->setUsername('foobar');
$factory = new SamlTokenFactory();
$sut = $factory->createToken($user, ['foo' => 'bar', 'bar' => 'world'], ['ROLE_ADMIN', 'ROLE_TEST']);
self::assertInstanceOf(SamlToken::class, $sut);
self::assertEquals('bar', $sut->getAttribute('foo'));
self::assertEquals('world', $sut->getAttribute('bar'));
self::assertEquals(['ROLE_ADMIN', 'ROLE_TEST'], $sut->getRoleNames());
self::assertSame($user, $sut->getUser());
self::assertEquals('foobar', $sut->getUsername());
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Saml;
use App\Entity\User;
use App\Saml\SamlToken;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Saml\SamlToken
*/
class SamlTokenTest extends TestCase
{
public function testConstruct()
{
$user = new User();
$user->setUserIdentifier('foo');
$sut = new SamlToken($user, 'firewallName', []);
self::assertEquals('foo', $sut->getUserIdentifier());
}
}

View File

@@ -9,8 +9,9 @@
namespace App\Tests\Saml\Security;
use App\Entity\User;
use App\Saml\SamlToken;
use App\Saml\Security\SamlAuthenticationSuccessHandler;
use App\Saml\Token\SamlToken;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\HttpUtils;
@@ -80,9 +81,11 @@ class SamlAuthenticationSuccessHandlerTest extends TestCase
private function getSamlToken()
{
$token = new SamlToken([]);
$user = new User();
$user->setUserIdentifier('admin');
$token = new SamlToken($user, 'secured_area', []);
$token->setAttributes(['foo' => 'bar']);
$token->setUser('admin');
return $token;
}

View File

@@ -1,45 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Saml\Security;
use App\Saml\Security\SamlFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @covers \App\Saml\Security\SamlFactory
*/
class SamlFactoryTest extends TestCase
{
public function testStaticValues()
{
$sut = new SamlFactory();
self::assertEquals('kimai_saml', $sut->getKey());
self::assertEquals('pre_auth', $sut->getPosition());
}
public function testCreate()
{
$container = new ContainerBuilder();
$sut = new SamlFactory();
$result = $sut->create($container, 'test', ['foo' => 'bar', 'login_path' => null, 'use_forward' => null], 'fosuserbundle', 'secured_area');
self::assertEquals([
'security.authentication.provider.saml.test',
'kimai.saml_listener.test',
'secured_area'
], $result);
$definition = $container->getDefinition('security.authentication.provider.saml.test');
self::assertInstanceOf(ChildDefinition::class, $definition);
self::assertCount(1, $definition->getArguments());
}
}

View File

@@ -1,28 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Saml\Token;
use App\Saml\Token\SamlToken;
use App\Saml\Token\SamlTokenInterface;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Saml\Token\SamlToken
*/
class SamlTokenTest extends TestCase
{
public function testCreateToken()
{
$sut = new SamlToken();
self::assertInstanceOf(SamlTokenInterface::class, $sut);
self::assertNull($sut->getCredentials());
}
}

View File

@@ -1,196 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Saml\User;
use App\Configuration\SamlConfiguration;
use App\Configuration\SystemConfiguration;
use App\Entity\User;
use App\Saml\Token\SamlToken;
use App\Saml\User\SamlUserFactory;
use App\Tests\Configuration\TestConfigLoader;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Saml\User\SamlUserFactory
*/
class SamlUserFactoryTest extends TestCase
{
private function createUserFactory(array $saml): SamlUserFactory
{
$configuration = new SystemConfiguration(new TestConfigLoader([]), [
'saml' => $saml
]);
return new SamlUserFactory(new SamlConfiguration($configuration));
}
public function testCreateUserThrowsExceptionOnMissingAttribute()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Missing user attribute: title');
$mapping = [
'mapping' => [
['saml' => '$Email', 'kimai' => 'email'],
['saml' => '$title', 'kimai' => 'title'],
],
'roles' => [
'attribute' => '',
'mapping' => []
]
];
$attributes = [
'Email' => ['test@example.com'],
];
$token = new SamlToken();
$token->setAttributes($attributes);
$sut = $this->createUserFactory($mapping);
$user = $sut->createUser($token);
}
public function testCreateUserThrowsExceptionOnMissingAttributeInMultiple()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Missing user attribute: test');
$mapping = [
'mapping' => [
['saml' => '$Email $test', 'kimai' => 'email'],
],
'roles' => [
'attribute' => '',
'mapping' => []
]
];
$attributes = [
'Email' => ['test@example.com'],
];
$token = new SamlToken();
$token->setAttributes($attributes);
$sut = $this->createUserFactory($mapping);
$user = $sut->createUser($token);
}
public function testCreateUserThrowsExceptionOnInvalidMapping()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid mapping field given: foo');
$mapping = [
'mapping' => [
['saml' => '$Email', 'kimai' => 'email'],
['saml' => '$Email', 'kimai' => 'foo'],
],
'roles' => [
'attribute' => '',
'mapping' => []
]
];
$attributes = [
'Email' => ['test@example.com'],
];
$token = new SamlToken();
$token->setAttributes($attributes);
$sut = $this->createUserFactory($mapping);
$user = $sut->createUser($token);
}
public function testCreateUser()
{
$mapping = [
'mapping' => [
['saml' => '$avatar', 'kimai' => 'avatar'],
['saml' => '$Email', 'kimai' => 'email'],
['saml' => 'A static super title', 'kimai' => 'title'],
// double space between "$LastName $FOOO" on purpose!!!
['saml' => '$FirstName $LastName $FOOO me', 'kimai' => 'alias'],
],
'roles' => [
'attribute' => 'RoLeS',
'mapping' => [
['saml' => 'fooobar', 'kimai' => 'ROLE_ADMIN'],
['saml' => 'ROLE_1', 'kimai' => 'ROLE_TEAMLEAD'],
['saml' => 'ROLE_2', 'kimai' => 'ROLE_2'],
]
]
];
$attributes = [
'RoLeS' => ['ROLE_1', 'ROLE_2', 'ROLE_3'],
'Email' => ['test@example.com'],
'FOOO' => ['test', 'test2'],
'FirstName' => ['Kevin'],
'LastName' => ['Papst'],
'avatar' => ['http://www.example.com/test.jpg'],
];
$token = new SamlToken();
$token->setUser('foo@example.com');
$token->setAttributes($attributes);
$sut = $this->createUserFactory($mapping);
$user = $sut->createUser($token);
self::assertInstanceOf(User::class, $user);
self::assertTrue($user->isEnabled());
self::assertEquals('', $user->getPassword());
self::assertEquals('test@example.com', $user->getEmail());
self::assertEquals('foo@example.com', $user->getUsername());
self::assertEquals('A static super title', $user->getTitle());
self::assertEquals('Kevin Papst test me', $user->getAlias());
self::assertEquals(['ROLE_TEAMLEAD', 'ROLE_2', 'ROLE_USER'], $user->getRoles());
}
public function testCreateUserDoesOverwriteUsername()
{
$mapping = [
'mapping' => [
['saml' => '$avatar', 'kimai' => 'avatar'],
['saml' => '$Email', 'kimai' => 'email'],
['saml' => 'A static super title', 'kimai' => 'title'],
['saml' => 'Mr. T', 'kimai' => 'username'],
],
'roles' => [
'attribute' => null,
'mapping' => []
]
];
$attributes = [
'Email' => ['test@example.com'],
'FOOO' => ['test', 'test2'],
'avatar' => ['http://www.example.com/test.jpg'],
];
$token = new SamlToken();
$token->setUser('foo@example.com');
$token->setAttributes($attributes);
$sut = $this->createUserFactory($mapping);
$user = $sut->createUser($token);
self::assertInstanceOf(User::class, $user);
self::assertTrue($user->isEnabled());
self::assertEquals('', $user->getPassword());
self::assertEquals('test@example.com', $user->getEmail());
self::assertEquals('foo@example.com', $user->getUsername());
self::assertEquals('A static super title', $user->getTitle());
self::assertEquals(['ROLE_USER'], $user->getRoles());
}
}