8000 [SecurityBundle] Link UserProviderListener to correct firewall dispatcher by Matth-- · Pull Request #41509 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[SecurityBundle] Link UserProviderListener to correct firewall dispatcher #41509

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 1 commit into from
Jun 3, 2021
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
[SecurityBundle] Link UserProviderListener to correct firewall dispat…
…cher
  • Loading branch information
Matth-- committed Jun 2, 2021
commit 46bdeb8507a0f5cd0bfdd56cee5da1c44fcd8812
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ private function createFirewall(ContainerBuilder $container, string $id, array $

$config->replaceArgument(4, $firewall['stateless']);

$firewallEventDispatcherId = 'security.event_dispatcher.'.$id;

// Provider id (must be configured explicitly per firewall/authenticator if more than one provider is set)
$defaultProvider = null;
if (isset($firewall['provider'])) {
Expand All @@ -349,7 +351,7 @@ private function createFirewall(ContainerBuilder $container, string $id, array $

if ($this->authenticatorManagerEnabled) {
$container->setDefinition('security.listener.'.$id.'.user_provider', new ChildDefinition('security.listener.user_provider.abstract'))
->addTag('kernel.event_listener', ['event' => CheckPassportEvent::class, 'priority' => 2048, 'method' => 'checkPassport'])
->addTag('kernel.event_listener', ['dispatcher' => $firewallEventDispatcherId, 'event' => CheckPassportEvent::class, 'priority' => 2048, 'method' => 'checkPassport'])
->replaceArgument(0, new Reference($defaultProvider));
}
} elseif (1 === \count($providerIds)) {
Expand All @@ -359,7 +361,6 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
$config->replaceArgument(5, $defaultProvider);

// Register Firewall-specific event dispatcher
$firewallEventDispatcherId = 'security.event_dispatcher.'.$id;
$container->register($firewallEventDispatcherId, EventDispatcher::class);

// Register listeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,28 @@ public function provideEmails()
yield ['jane@example.org', true];
yield ['john@example.org', false];
}

/**
* @dataProvider provideEmailsWithFirewalls
*/
public function testLoginUsersWithMultipleFirewalls(string $username, string $firewallContext)
{
$client = $this->createClient(['test_case' => 'Authenticator', 'root_config' => 'multiple_firewall_user_provider.yml']);
$client->request('GET', '/main/login/check');

$client->request('POST', '/'.$firewallContext.'/login/check', [
'_username' => $username,
'_password' => 'test',
]);
$this->assertResponseRedirects('/'.$firewallContext.'/user_profile');

$client->request('GET', '/'.$firewallContext.'/user_profile');
$this->assertEquals('Welcome '.$username.'!', $client->getResponse()->getContent());
}

public function provideEmailsWithFirewalls()
{
yield ['jane@example.org', 'main'];
yield ['john@example.org', 'custom'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;

class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
{
use TargetPathTrait;

/** @var UrlGeneratorInterface */
private $urlGenerator;

public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}

public function authenticate(Request $request): PassportInterface
{
$username = $request->request->get('_username', '');

$request->getSession()->set(Security::LAST_USERNAME, $username);

return new Passport(
new UserBadge($username),
new PasswordCredentials($request->request->get('_password', '')),
[]
);
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $firewallName): ?Response
{
return new RedirectResponse($this->urlGenerator->generate('security_'.$firewallName.'_profile'));
}

protected function getLoginUrl(Request $request): string
{
return strpos($request->getUri(), 'main') ?
$this->urlGenerator->generate('security_main_login') :
$this->urlGenerator->generate('security_custom_login')
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class SecurityController extends AbstractController
{
public function checkAction()
{
return new Response('OK');
}

public function profileAction()
{
return new Response('Welcome '.$this->getUser()->getUsername().'!');
}
}
6D4E
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ services:
calls:
- ['setContainer', ['@Psr\Container\ContainerInterface']]
tags: [container.service_subscriber]
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\SecurityController:
public: true
calls:
- ['setContainer', ['@Psr\Container\ContainerInterface']]
tags: [container.service_subscriber]
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator: ~
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\LoginFormAuthenticator:
public: true
arguments: ['@router']
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
imports:
- { resource: ./config.yml }
- { resource: ./security.yml }

security:
firewalls:
main:
pattern: ^/main
provider: in_memory
custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\LoginFormAuthenticator
custom:
pattern: ^/custom
provider: in_memory2
custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\LoginFormAuthenticator
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,19 @@ profile:
path: /profile
defaults:
_controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ProfileController

security_main_login:
path: /main/login/check
defaults: { _controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\SecurityController::checkAction }

security_custom_login:
path: /custom/login/check
defaults: { _controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\SecurityController::checkAction }

security_main_profile:
path: /main/user_profile
defaults: { _controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\SecurityController::profileAction }

security_custom_profile:
path: /custom/user_profile
defaults: { _controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\SecurityController::profileAction }
0