Skip to content

Commit 27b89cb

Browse files
[Security] Use AuthenticationTrustResolver in SimplePreAuthenticationListener
1 parent 9efa555 commit 27b89cb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<argument /> <!-- Authenticator -->
133133
<argument type="service" id="logger" on-invalid="null" />
134134
<argument type="service" id="event_dispatcher" on-invalid="null"/>
135+
<argument type="service" id="security.authentication.trust_resolver" />
135136
</service>
136137

137138
<service id="security.authentication.listener.x509" class="Symfony\Component\Security\Http\Firewall\X509AuthenticationListener" abstract="true">

src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
use Psr\Log\LoggerInterface;
1717
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1818
use Symfony\Component\HttpFoundation\Response;
19+
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
20+
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
1921
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
2022
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
23+
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
2124
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2225
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2326
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -42,8 +45,9 @@ class SimplePreAuthenticationListener implements ListenerInterface
4245
private $logger;
4346
private $dispatcher;
4447
private $sessionStrategy;
48+
private $trustResolver;
4549

46-
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, string $providerKey, SimplePreAuthenticatorInterface $simpleAuthenticator, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
50+
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, string $providerKey, SimplePreAuthenticatorInterface $simpleAuthenticator, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, AuthenticationTrustResolverInterface $trustResolver = null)
4751
{
4852
if (empty($providerKey)) {
4953
throw new \InvalidArgumentException('$providerKey must not be empty.');
@@ -55,6 +59,7 @@ public function __construct(TokenStorageInterface $tokenStorage, AuthenticationM
5559
$this->simpleAuthenticator = $simpleAuthenticator;
5660
$this->logger = $logger;
5761
$this->dispatcher = $dispatcher;
62+
$this->trustResolver = $trustResolver ?: new AuthenticationTrustResolver(AnonymousToken::class, RememberMeToken::class);
5863
}
5964

6065
/**
@@ -78,7 +83,7 @@ public function handle(GetResponseEvent $event)
7883
$this->logger->info('Attempting SimplePreAuthentication.', array('key' => $this->providerKey, 'authenticator' => get_class($this->simpleAuthenticator)));
7984
}
8085

81-
if (null !== $this->tokenStorage->getToken() && !$this->tokenStorage->getToken() instanceof AnonymousToken) {
86+
if ((null !== $token = $this->tokenStorage->getToken()) && !$this->trustResolver->isAnonymous($token)) {
8287
return;
8388
}
8489

0 commit comments

Comments
 (0)