8000 [Security] AuthenticatorManager to make "authenticators" first-class security by wouterj · Pull Request #33558 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] AuthenticatorManager to make "authenticators" first-class security #33558

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 30 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c321f4d
Created GuardAuthenticationManager to make Guard first-class Security
wouterj Sep 8, 2019
a6890db
Created HttpBasicAuthenticator and some Guard traits
wouterj Sep 8, 2019
9b7fddd
Integrated GuardAuthenticationManager in the SecurityBundle
wouterj Sep 8, 2019
a172bac
Added FormLogin and Anonymous authenticators
wouterj Dec 13, 2019
526f756
Added GuardManagerListener
wouterj Dec 13, 2019
5013258
Add provider key in PreAuthenticationGuardToken
wouterj Jan 26, 2020
5efa892
Create a new core AuthenticatorInterface
wouterj Jan 26, 2020
fa4b3ec
Implemented password migration for the new authenticators
wouterj Jan 26, 2020
4c06236
Fixes after testing in Demo application
wouterj Jan 26, 2020
873b949
Mark new core authenticators as experimental
wouterj Jan 26, 2020
b923e4c
Enabled remember me for the GuardManagerListener
wouterj Jan 26, 2020
b14a5e8
Moved new authenticator to the HTTP namespace
wouterj Jan 26, 2020
999ec27
Refactor to an event based authentication approach
wouterj Feb 6, 2020
7859977
Removed all mentions of 'guard' in the new system
wouterj Feb 6, 2020
1c810d5
Added support for lazy firewalls
wouterj Feb 9, 2020
ddf430f
Added remember me functionality
wouterj Feb 12, 2020
09bed16
Only load old manager if new system is disabled
wouterj Feb 22, 2020
44cc76f
Use one AuthenticatorManager per firewall
wouterj Feb 29, 2020
bf1a452
Merge AuthenticatorManager and AuthenticatorHandler
wouterj Mar 1, 2020
60d396f
Added automatically CSRF protected authenticators
wouterj Mar 1, 2020
59f49b2
Rename AuthenticatingListener
wouterj Mar 7, 2020
6b9d78d
Added tests
wouterj Mar 7, 2020
ba3754a
Differentiate between interactive and non-interactive authenticators
wouterj Mar 13, 2020
f5e11e5
Reverted changes to the Guard component
wouterj Mar 14, 2020
95edc80
Added pre-authenticated authenticators (X.509 & REMOTE_USER)
wouterj Mar 14, 2020
7ef6a7a
Use the firewall event dispatcher
wouterj Apr 4, 2020
0fe5083
Added JSON login authenticator
wouterj Apr 5, 2020
9ea32c4
Also use authentication failure/success handlers in FormLoginAuthenti…
wouterj Apr 6, 2020
50224aa
Introduce Passport & Badges to extend authenticators
wouterj Apr 9, 2020
b1e040f
Rename providerKey to firewallName for more consistent naming
wouterj Apr 10, 2020
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
Prev Previous commit
Next Next commit
Added support for lazy firewalls
  • Loading branch information
wouterj committed Apr 20, 2020
commit 1c810d5d2a62cf7c5da0109969011bc415df5561
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
->setDefinition('security.firewall.authenticator.'.$id, new ChildDefinition('security.firewall.authenticator'))
->replaceArgument(2, new Reference('security.firewall.authenticator.'.$id.'.locator'))
->replaceArgument(3, $id)
->addTag('kernel.event_listener', ['event' => KernelEvents::REQUEST])
;

$listeners[] = new Reference('security.firewall.authenticator.'.$id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,39 @@
*/
class LazyAuthenticatorManagerListener extends AuthenticatorManagerListener
{
private $guardLocator;
private $authenticatorLocator;

public function __construct(
AuthenticationManagerInterface $authenticationManager,
AuthenticatorHandler $authenticatorHandler,
ServiceLocator $guardLocator,
ServiceLocator $authenticatorLocator,
string $providerKey,
EventDispatcherInterface $eventDispatcher,
?LoggerInterface $logger = null
) {
parent::__construct($authenticationManager, $authenticatorHandler, [], $providerKey, $eventDispatcher, $logger);

$this->guardLocator = $guardLocator;
$this->authenticatorLocator = $authenticatorLocator;
}

protected function getSupportingAuthenticators(Request $request): array
{
$guardAuthenticators = [];
foreach ($this->guardLocator->getProvidedServices() as $key => $type) {
$guardAuthenticator = $this->guardLocator->get($key);
$authenticators = [];
$lazy = true;
foreach ($this->authenticatorLocator->getProvidedServices() as $key => $type) {
$authenticator = $this->authenticatorLocator->get($key);
if (null !== $this->logger) {
$this->logger->debug('Checking support on guard authenticator.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($guardAuthenticator)]);
$this->logger->debug('Checking support on guard authenticator.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($authenticator)]);
}

if ($guardAuthenticator->supports($request)) {
$guardAuthenticators[$key] = $guardAuthenticator;
if (false !== $supports = $authenticator->supports($request)) {
$authenticators[$key] = $authenticator;
$lazy = $lazy && null === $supports;
} elseif (null !== $this->logger) {
$this->logger->debug('Guard authenticator does not support the request.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($guardAuthenticator)]);
$this->logger->debug('Guard authenticator does not support the request.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($authenticator)]);
}
}

return $guardAuthenticators;
return [$authenticators, $lazy];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
class="Symfony\Component\Security\Http\Authenticator\AnonymousAuthenticator"
abstract="true">
<argument type="abstract">secret</argument>
<argument type="service" id="security.token_storage" />
<argument type="service" id="security.untracked_token_storage" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
*/
class GuardAuthenticationListener extends AbstractListener
{
use AuthenticatorManagerListenerTrait;

private $guardHandler;
private $authenticationManager;
private $providerKey;
Expand Down Expand Up @@ -75,7 +73,19 @@ public function supports(Request $request): ?bool
$this->logger->debug('Checking for guard authentication credentials.', $context);
}

$guardAuthenticators = $this->getSupportingAuthenticators($request);
$guardAuthenticators = [];
foreach ($this->authenticators as $key => $authenticator) {
if (null !== $this->logger) {
$this->logger->debug('Checking support on authenticator.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($authenticator)]);
}

if ($authenticator->supports($request)) {
$guardAuthenticators[$key] = $authenticator;
} elseif (null !== $this->logger) {
$this->logger->debug('Authenticator does not support the request.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($authenticator)]);
}
}

if (!$guardAuthenticators) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function __construct(string $secret, TokenStorageInterface $tokenStorage)
public function supports(Request $request): ?bool
{
// do not overwrite already stored tokens (i.e. from the session)
return null === $this->tokenStorage->getToken();
// the `null` return value indicates that this authenticator supports lazy firewalls
return null === $this->tokenStorage->getToken() ? null : false;
}

public function getCredentials(Request $request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
Expand All @@ -30,10 +31,8 @@
*
* @experimental in 5.1
*/
class AuthenticatorManagerListener
class AuthenticatorManagerListener extends AbstractListener
{
use AuthenticatorManagerListenerTrait;

private $authenticationManager;
private $authenticatorHandler;
private $authenticators;
Expand All @@ -54,15 +53,58 @@ public function __construct(AuthenticationManagerInterface $authenticationManage
$this->eventDispatcher = $eventDispatcher;
}

public function __invoke(RequestEvent $requestEvent)
public function supports(Request $request): ?bool
{
$request = $requestEvent->getRequest();
$authenticators = $this->getSupportingAuthenticators($request);
if (null !== $this->logger) {
$context = ['firewall_key' => $this->providerKey];

if ($this->authenticators instanceof \Countable || \is_array($this->authenticators)) {
$context['authenticators'] = \count($this->authenticators);
}

$this->logger->debug('Checking for guard authentication credentials.', $context);
}

[$authenticators, $lazy] = $this->getSupportingAuthenticators($request);
if (!$authenticators) {
return false;
}

$request->attributes->set('_guard_authenticators', $authenticators);

return $lazy ? null : true;
}

public function authenticate(RequestEvent $event)
{
$request = $event->getRequest();
$authenticators = $request->attributes->get('_guard_authenticators');
$request->attributes->remove('_guard_authenticators');
if (!$authenticators) {
return;
}

$this->executeAuthenticators($authenticators, $requestEvent);
$this->executeAuthenticators($authenticators, $event);
}

protected function getSupportingAuthenticators(Request $request): array
{
$authenticators = [];
$lazy = true;
foreach ($this->authenticators as $key => $authenticator) {
if (null !== $this->logger) {
$this->logger->debug('Checking support on authenticator.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($authenticator)]);
}

if (false !== $supports = $authenticator->supports($request)) {
$authenticators[$key] = $authenticator;
$lazy = $lazy && null === $supports;
} elseif (null !== $this->logger) {
$this->logger->debug('Authenticator does not support the request.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($authenticator)]);
}
}

return [$authenticators, $lazy];
}

/**
Expand All @@ -71,6 +113,15 @@ public function __invoke(RequestEvent $requestEvent)
protected function executeAuthenticators(array $authenticators, RequestEvent $event): void
{
foreach ($authenticators as $key => $authenticator) {
// recheck if the authenticator still supports the listener. support() is called
// eagerly (before token storage is initialized), whereas authenticate() is called
// lazily (after initialization). This is important for e.g. the AnonymousAuthenticator
// as its support is relying on the (initialized) token in the TokenStorage.
if (false === $authenticator->supports($event->getRequest())) {
$this->logger->debug('Skipping the "{authenticator}" authenticator as it did not support the request.', ['authenticator' => \get_class($authenticator)]);
continue;
}

$this->executeAuthenticator($key, $authenticator, $event);

if ($event->hasResponse()) {
Expand Down

This file was deleted.

0