Skip to content

Commit 3f30e12

Browse files
committed
bug #922 Fix error when trying to decode token using new authenticator system (fd6130)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- Fix error when trying to decode token using new authenticator system If you are using new authenticator system, you cannot decode the token in a service or controller using `$jwtManager->decode($token->getToken());` because the Token Class `getCredentials()` return empty array instead of token. Now i'm not sure about the `public function createToken(Passport $passport, string $firewallName): TokenInterface` in JwtAuthenticator. Do i need to adjust that one as well? Anyways, this Fix #921. Commits ------- d73d1b8 Fix error when trying to decode token using new authenticator system
2 parents 7f0cf79 + d73d1b8 commit 3f30e12

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

Security/Authenticator/JWTAuthenticator.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function ($userIdentifier) use($payload) {
117117
);
118118

119119
$passport->setAttribute('payload', $payload);
120+
$passport->setAttribute('token', $token);
120121

121122
return $passport;
122123
}
@@ -234,20 +235,24 @@ protected function loadUser(array $payload, string $identity): UserInterface
234235

235236
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
236237
{
237-
$token = parent::createAuthenticatedToken($passport, $firewallName);
238-
239238
if (!$passport instanceof SelfValidatingPassport) {
240239
throw new \LogicException(sprintf('Expected "%s" but got "%s".', SelfValidatingPassport::class, get_debug_type($passport)));
241240
}
242-
241+
242+
$token = new JWTPostAuthenticationToken($passport->getUser(), $firewallName, $passport->getUser()->getRoles(), $passport->getAttribute('token'));
243+
243244
$this->eventDispatcher->dispatch(new JWTAuthenticatedEvent($passport->getAttribute('payload'), $token), Events::JWT_AUTHENTICATED);
244245

245246
return $token;
246247
}
247248

248249
public function createToken(Passport $passport, string $firewallName): TokenInterface
249250
{
250-
$token = parent::createToken($passport, $firewallName);
251+
if (!$passport instanceof SelfValidatingPassport) {
252+
throw new \LogicException(sprintf('Expected "%s" but got "%s".', SelfValidatingPassport::class, get_debug_type($passport)));
253+
}
254+
255+
$token = new JWTPostAuthenticationToken($passport->getUser(), $firewallName, $passport->getUser()->getRoles(), $passport->getAttribute('token'));
251256

252257
$this->eventDispatcher->dispatch(new JWTAuthenticatedEvent($passport->getAttribute('payload'), $token), Events::JWT_AUTHENTICATED);
253258

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\Authenticator\Token;
4+
5+
use Symfony\Component\Security\Core\User\UserInterface;
6+
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
7+
8+
class JWTPostAuthenticationToken extends PostAuthenticationToken
9+
{
10+
private $token;
11+
12+
public function __construct(UserInterface $user, string $firewallName, array $roles, string $token)
13+
{
14+
parent::__construct($user, $firewallName, $roles);
15+
16+
$this->token = $token;
17+
}
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function getCredentials(): string
23+
{
24+
return $this->token;
25+
}
26+
}

Tests/Security/Authenticator/JWTAuthenticatorTest.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Lexik\Bundle\JWTAuthenticationBundle\Exception\MissingTokenException;
1515
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
1616
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authenticator\JWTAuthenticator;
17+
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authenticator\Token\JWTPostAuthenticationToken;
1718
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\PayloadAwareUserProviderInterface;
1819
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
1920
use Lexik\Bundle\JWTAuthenticationBundle\Tests\Stubs\User as AdvancedUserStub;
@@ -214,7 +215,7 @@ public function testCreateAuthenticatedToken()
214215
$user->method('getRoles')->willReturn(['ROLE_USER']);
215216

216217
$dispatcher = $this->getEventDispatcherMock();
217-
$dispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(new JWTAuthenticatedEvent(['claim' => 'val'], new PostAuthenticationToken($user, 'dummy', ['ROLE_USER']))), Events::JWT_AUTHENTICATED);
218+
$dispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(new JWTAuthenticatedEvent(['claim' => 'val'], new JWTPostAuthenticationToken($user, 'dummy', ['ROLE_USER'], 'dummytoken'))), Events::JWT_AUTHENTICATED);
218219

219220
$authenticator = new JWTAuthenticator(
220221
$this->getJWTManagerMock(),
@@ -225,13 +226,18 @@ public function testCreateAuthenticatedToken()
225226

226227
$passport = $this->createMock(SelfValidatingPassport::class);
227228
$passport->method('getUser')->willReturn($user);
228-
$passport->method('getAttribute')->with('payload')->willReturn(['claim' => 'val']);
229-
229+
$passport->method('getAttribute')
230+
->withConsecutive(['token', null], ['payload', null])
231+
->willReturnOnConsecutiveCalls('dummytoken', ['claim' => 'val']);
232+
230233
if (method_exists(FormLoginAuthenticator::class, 'createToken')) {
231-
$authenticator->createToken($passport, 'dummy');
234+
$token = $authenticator->createToken($passport, 'dummy');
232235
} else {
233-
$authenticator->createAuthenticatedToken($passport, 'dummy');
236+
$token = $authenticator->createAuthenticatedToken($passport, 'dummy');
234237
}
238+
239+
$this->assertInstanceOf(JWTPostAuthenticationToken::class, $token);
240+
$this->assertSame('dummytoken', $token->getCredentials());
235241
}
236242

237243
private function getJWTManagerMock($userIdentityField = null, $userIdClaim = null)

0 commit comments

Comments
 (0)