Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04b2c2d

Browse files
author
Robin Chalas
committed
feature #27798 [Security] Use AuthenticationTrustResolver in SimplePreAuthenticationListener (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Security] Use AuthenticationTrustResolver in SimplePreAuthenticationListener | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes (minor) | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Minor, but would be consistent with how `ContextListener` checks for anonymous tokens. Commits ------- 27b89cb [Security] Use AuthenticationTrustResolver in SimplePreAuthenticationListener
2 parents 26989d4 + 27b89cb commit 04b2c2d

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 2 deletions
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)