Skip to content

Commit

Permalink
Removed "services" prototype node from "custom_authenticator"
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed May 23, 2020
1 parent 8923514 commit 8e5eeec
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getPosition(): string

public function getKey(): string
{
return 'custom_authenticator';
return 'custom_authenticators';
}

/**
Expand All @@ -44,19 +44,27 @@ public function getKey(): string
public function addConfiguration(NodeDefinition $builder)
{
$builder
->fixXmlConfig('service')
->children()
->arrayNode('services')
->info('An array of service ids for all of your "authenticators"')
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->end()
->info('An array of service ids for all of your "authenticators"')
->requiresAtLeastOneElement()
->prototype('scalar')->end();

// get the parent array node builder ("firewalls") from inside the children builder
$factoryRootNode = $builder->end()->end();
$factoryRootNode
->fixXmlConfig('custom_authenticator')
->validate()
->ifTrue(function ($v) { return isset($v['custom_authenticators']) && empty($v['custom_authenticators']); })
->then(function ($v) {
unset($v['custom_authenticators']);

return $v;
})
->end()
;
}

public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): array
{
return $config['services'];
return $config;
}
}
66 changes: 64 additions & 2 deletions Tests/DependencyInjection/SecurityExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AuthenticatorInterface;
use Symfony\Component\Security\Guard\AuthenticatorInterface as GuardAuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\HttpBasicAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;

class SecurityExtensionTest extends TestCase
{
Expand Down Expand Up @@ -520,6 +524,41 @@ public function testAlwaysAuthenticateBeforeGrantingCannotBeTrueWithAuthenticati
$container->compile();
}

/**
* @dataProvider provideConfigureCustomAuthenticatorData
*/
public function testConfigureCustomAuthenticator(array $firewall, array $expectedAuthenticators)
{
$container = $this->getRawContainer();
$container->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'providers' => [
'first' => ['id' => 'users'],
],

'firewalls' => [
'main' => $firewall,
],
]);

$container->compile();

$this->assertEquals($expectedAuthenticators, array_map('strval', $container->getDefinition('security.authenticator.manager.main')->getArgument(0)));
}

public function provideConfigureCustomAuthenticatorData()
{
yield [
['custom_authenticator' => TestAuthenticator::class],
[TestAuthenticator::class],
];

yield [
['custom_authenticators' => [TestAuthenticator::class, HttpBasicAuthenticator::class]],
[TestAuthenticator::class, HttpBasicAuthenticator::class],
];
}

protected function getRawContainer()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -547,7 +586,30 @@ protected function getContainer()
}
}

class NullAuthenticator implements AuthenticatorInterface
class TestAuthenticator implements AuthenticatorInterface
{
public function supports(Request $request): ?bool
{
}

public function authenticate(Request $request): PassportInterface
{
}

public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
{
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
}
}

class NullAuthenticator implements GuardAuthenticatorInterface
{
public function start(Request $request, AuthenticationException $authException = null)
{
Expand Down

0 comments on commit 8e5eeec

Please sign in to comment.