Skip to content

[Security] Removed "services" prototype node from "custom_authenticator" #36918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
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