Skip to content

[SecurityBundle] Fix using same handler for multiple authenticators #48937

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 @@ -164,7 +164,7 @@ protected function createAuthenticationSuccessHandler(ContainerBuilder $containe

if (isset($config['success_handler'])) {
$successHandler = $container->setDefinition($successHandlerId, new ChildDefinition('security.authentication.custom_success_handler'));
$successHandler->replaceArgument(0, new Reference($config['success_handler']));
$successHandler->replaceArgument(0, new ChildDefinition($config['success_handler']));
$successHandler->replaceArgument(1, $options);
$successHandler->replaceArgument(2, $id);
} else {
Expand All @@ -183,7 +183,7 @@ protected function createAuthenticationFailureHandler(ContainerBuilder $containe

if (isset($config['failure_handler'])) {
$failureHandler = $container->setDefinition($id, new ChildDefinition('security.authentication.custom_failure_handler'));
$failureHandler->replaceArgument(0, new Reference($config['failure_handler']));
$failureHandler->replaceArgument(0, new ChildDefinition($config['failure_handler']));
$failureHandler->replaceArgument(1, $options);
} else {
$failureHandler = $container->setDefinition($id, new ChildDefinition('security.authentication.failure_handler'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AbstractFactory;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

Expand Down Expand Up @@ -69,12 +70,19 @@ public function testDefaultFailureHandler($serviceId, $defaultHandlerInjection)
$this->assertEquals(new Reference('security.authentication.failure_handler.foo.abstract_factory'), $arguments['index_6']);
$failureHandler = $container->findDefinition((string) $arguments['index_6']);

$expectedFailureHandlerOptions = ['login_path' => '/bar'];
$methodCalls = $failureHandler->getMethodCalls();
if ($defaultHandlerInjection) {
$this->assertEquals('setOptions', $methodCalls[0][0]);
$this->assertEquals(['login_path' => '/bar'], $methodCalls[0][1][0]);
$this->assertEquals($expectedFailureHandlerOptions, $methodCalls[0][1][0]);
} else {
$this->assertCount(0, $methodCalls);
$this->assertInstanceOf(ChildDefinition::class, $failureHandler);
$this->assertEquals('security.authentication.custom_failure_handler', $failureHandler->getParent());
$failureHandlerArguments = $failureHandler->getArguments();
$this->assertInstanceOf(ChildDefinition::class, $failureHandlerArguments['index_0']);
$this->assertEquals($serviceId, $failureHandlerArguments['index_0']->getParent());
$this->assertEquals($expectedFailureHandlerOptions, $failureHandlerArguments['index_1']);
}
}

Expand Down Expand Up @@ -108,13 +116,22 @@ public function testDefaultSuccessHandler($serviceId, $defaultHandlerInjection)
$successHandler = $container->findDefinition((string) $arguments['index_5']);
$methodCalls = $successHandler->getMethodCalls();

$expectedSuccessHandlerOptions = ['default_target_path' => '/bar'];
$expectedFirewallName = 'foo';
if ($defaultHandlerInjection) {
$this->assertEquals('setOptions', $methodCalls[0][0]);
$this->assertEquals(['default_target_path' => '/bar'], $methodCalls[0][1][0]);
$this->assertEquals($expectedSuccessHandlerOptions, $methodCalls[0][1][0]);
$this->assertEquals('setFirewallName', $methodCalls[1][0]);
$this->assertEquals(['foo'], $methodCalls[1][1]);
$this->assertEquals($expectedFirewallName, $methodCalls[1][1][0]);
} else {
$this->assertCount(0, $methodCalls);
$this->assertInstanceOf(ChildDefinition::class, $successHandler);
$this->assertEquals('security.authentication.custom_success_handler', $successHandler->getParent());
$successHandlerArguments = $successHandler->getArguments();
$this->assertInstanceOf(ChildDefinition::class, $successHandlerArguments['index_0']);
$this->assertEquals($serviceId, $successHandlerArguments['index_0']->getParent());
$this->assertEquals($expectedSuccessHandlerOptions, $successHandlerArguments['index_1']);
$this->assertEquals($expectedFirewallName, $successHandlerArguments['index_2']);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,38 @@ public function testMultipleFirewalls()
$client->request('GET', '/firewall2/profile');
$this->assertResponseRedirects('http://localhost/login');
}

public function testCustomSuccessHandler()
{
$client = $this->createClient(['test_case' => 'Authenticator', 'root_config' => 'custom_handlers.yml']);

$client->request('POST', '/firewall1/login', [
'_username' => 'jane@example.org',
'_password' => 'test',
]);
$this->assertResponseRedirects('http://localhost/firewall1/test');

$client->request('POST', '/firewall1/dummy_login', [
'_username' => 'jane@example.org',
'_password' => 'test',
]);
$this->assertResponseRedirects('http://localhost/firewall1/dummy');
}

public function testCustomFailureHandler()
{
$client = $this->createClient(['test_case' => 'Authenticator', 'root_config' => 'custom_handlers.yml']);

$client->request('POST', '/firewall1/login', [
'_username' => 'jane@example.org',
'_password' => '',
]);
$this->assertResponseRedirects('http://localhost/firewall1/login');

$client->request('POST', '/firewall1/dummy_login', [
'_username' => 'jane@example.org',
'_password' => '',
]);
$this->assertResponseRedirects('http://localhost/firewall1/dummy_login');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle;

use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class AuthenticatorBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$this->configureSecurityExtension($container);
}

private function configureSecurityExtension(ContainerBuilder $container): void
{
/** @var SecurityExtension $extension */
$extension = $container->getExtension('security');

$extension->addAuthenticatorFactory(new DummyFormLoginFactory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle;

use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FormLoginFactory;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class DummyFormLoginFactory extends FormLoginFactory
{
public function getKey(): string
{
return 'dummy_form_login';
}

public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
{
$authenticatorId = 'security.authenticator.dummy_form_login.'.$firewallName;
$options = array_intersect_key($config, $this->options);
$authenticator = $container
->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.form_login'))
->replaceArgument(1, new Reference($userProviderId))
->replaceArgument(2, new Reference($this->createAuthenticationSuccessHandler($container, $firewallName, $config)))
->replaceArgument(3, new Reference($this->createAuthenticationFailureHandler($container, $firewallName, $config)))
->replaceArgument(4, $options);

if ($options['use_forward'] ?? false) {
$authenticator->addMethodCall('setHttpKernel', [new Reference('http_kernel')]);
}

return $authenticatorId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
return [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\AuthenticatorBundle(),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
imports:
- { resource: ./config.yml }
- { resource: ./security.yml }

security:
enable_authenticator_manager: true
firewalls:
firewall1:
pattern: /firewall1
provider: in_memory
entry_point: form_login
form_login:
check_path: /firewall1/login
success_handler: success_handler
failure_handler: failure_handler
default_target_path: /firewall1/test
login_path: /firewall1/login
dummy_form_login:
check_path: /firewall1/dummy_login
success_handler: success_handler
failure_handler: failure_handler
default_target_path: /firewall1/dummy
login_path: /firewall1/dummy_login

services:
success_handler:
class: Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler
arguments:
- '@security.http_utils'
failure_handler:
class: Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler
arguments:
- '@http_kernel'
- '@security.http_utils'
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ security_custom_profile:
firewall1_login:
path: /firewall1/login

firewall_dummy_login:
path: /firewall1/dummy_login

firewall2_profile:
path: /firewall2/profile
defaults:
Expand Down