Skip to content

Commit a48ca00

Browse files
committed
Fix AbstractAuthenticator::createToken() BC layer
1 parent 1d19615 commit a48ca00

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/Symfony/Component/Security/Http/Authentication/AuthenticatorManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
2525
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
2626
use Symfony\Component\Security\Core\User\UserInterface;
27+
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
2728
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
2829
use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface;
2930
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\BadgeInterface;
@@ -77,7 +78,7 @@ public function authenticateUser(UserInterface $user, AuthenticatorInterface $au
7778
// create an authentication token for the User
7879
// @deprecated since 5.3, change to $user->getUserIdentifier() in 6.0
7980
$passport = new SelfValidatingPassport(new UserBadge(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), function () use ($user) { return $user; }), $badges);
80-
$token = method_exists($authenticator, 'createToken') ? $authenticator->createToken($passport, $this->firewallName) : $authenticator->createAuthenticatedToken($passport, $this->firewallName);
81+
$token = method_exists($authenticator, 'createToken') || ($authenticator instanceof AbstractAuthenticator && AbstractAuthenticator::class === (new \ReflectionMethod($authenticator, 'createAuthenticatedToken'))->getDeclaringClass()->getName()) ? $authenticator->createToken($passport, $this->firewallName) : $authenticator->createAuthenticatedToken($passport, $this->firewallName);
8182

8283
// announce the authentication token
8384
$token = $this->eventDispatcher->dispatch(new AuthenticationTokenCreatedEvent($token, $passport))->getAuthenticatedToken();
@@ -191,7 +192,7 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
191192
}
192193

193194
// create the authentication token
194-
$authenticatedToken = method_exists($authenticator, 'createToken') ? $authenticator->createToken($passport, $this->firewallName) : $authenticator->createAuthenticatedToken($passport, $this->firewallName);
195+
$authenticatedToken = method_exists($authenticator, 'createToken') || ($authenticator instanceof AbstractAuthenticator && AbstractAuthenticator::class === (new \ReflectionMethod($authenticator, 'createAuthenticatedToken'))->getDeclaringClass()->getName()) ? $authenticator->createToken($passport, $this->firewallName) : $authenticator->createAuthenticatedToken($passport, $this->firewallName);
195196

196197
// announce the authentication token
197198
$authenticatedToken = $this->eventDispatcher->dispatch(new AuthenticationTokenCreatedEvent($authenticatedToken, $passport))->getAuthenticatedToken();

src/Symfony/Component/Security/Http/Authenticator/AbstractAuthenticator.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,24 @@
2626
abstract class AbstractAuthenticator implements AuthenticatorInterface
2727
{
2828
/**
29-
* Shortcut to create a PostAuthenticationToken for you, if you don't really
30-
* care about which authenticated token you're using.
31-
*
32-
* @return PostAuthenticationToken
29+
* @deprecated since Symfony 5.4, implement createToken in 6.0
3330
*/
34-
public function createToken(Passport $passport, string $firewallName): TokenInterface
31+
public function __call(string $method, array $arguments)
3532
{
33+
if ('createToken' !== $method) {
34+
return $this->$method(...$arguments);
35+
}
36+
37+
$passport = $arguments[0] ?? null;
38+
$firewallName = $arguments[1] ?? null;
39+
40+
if (!$passport instanceof Passport) {
41+
throw new \TypeError(sprintf('The "$passport" argument of method "%s::createToken() must be an instance of "%s", "%s" given.', __CLASS__, Passport::class, get_debug_type($passport)));
42+
}
43+
if (!\is_string($firewallName)) {
44+
throw new \TypeError(sprintf('The "$firewallName" argument of method "%s::createToken() must be a string, "%s" given.', __CLASS__, get_debug_type($firewallName)));
45+
}
46+
3647
return new PostAuthenticationToken($passport->getUser(), $firewallName, $passport->getUser()->getRoles());
3748
}
3849

src/Symfony/Component/Security/Http/Tests/Authentication/AuthenticatorManagerTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
2323
use Symfony\Component\Security\Core\User\InMemoryUser;
2424
use Symfony\Component\Security\Http\Authentication\AuthenticatorManager;
25+
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
2526
use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface;
2627
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
2728
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
@@ -269,9 +270,12 @@ public function testInteractiveAuthenticator()
269270
$this->assertSame($this->response, $response);
270271
}
271272

273+
/**
274+
* @group legacy
275+
*/
272276
public function testLegacyInteractiveAuthenticator()
273277
{
274-
$authenticator = $this->createMock(InteractiveAuthenticatorInterface::class);
278+
$authenticator = $this->createMock(LegacyTestInteractiveAuthenticator::class);
275279
$authenticator->expects($this->any())->method('isInteractive')->willReturn(true);
276280
$this->request->attributes->set('_security_authenticators', [$authenticator]);
277281

@@ -330,3 +334,7 @@ public function createToken(Passport $passport, string $firewallName): TokenInte
330334
{
331335
}
332336
}
337+
338+
abstract class LegacyTestInteractiveAuthenticator extends AbstractAuthenticator implements InteractiveAuthenticatorInterface
339+
{
340+
}

0 commit comments

Comments
 (0)