-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SecurityBundle] Fix using same handler for multiple authenticators
Using a reference for custom success/failure handler breaks using the same service for multiple authenticators. This as the options will be overriden by any later authenticators. So use a ChildDefinition instead so every authenticator gets a unique instance. Fixes: #48923
- Loading branch information
1 parent
8203ec9
commit e16ac30
Showing
8 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' => '[email protected]', | ||
'_password' => 'test', | ||
]); | ||
$this->assertResponseRedirects('http://localhost/firewall1/test'); | ||
|
||
$client->request('POST', '/firewall1/dummy_login', [ | ||
'_username' => '[email protected]', | ||
'_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' => '[email protected]', | ||
'_password' => '', | ||
]); | ||
$this->assertResponseRedirects('http://localhost/firewall1/login'); | ||
|
||
$client->request('POST', '/firewall1/dummy_login', [ | ||
'_username' => '[email protected]', | ||
'_password' => '', | ||
]); | ||
$this->assertResponseRedirects('http://localhost/firewall1/dummy_login'); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Tests/Functional/Bundle/AuthenticatorBundle/AuthenticatorBundle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Tests/Functional/Bundle/AuthenticatorBundle/DummyFormLoginFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters