-
-
Notifications
You must be signed in to change notification settings - Fork 611
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
Use TokenInterface::getAttribute() instead of deprecated getCredentials() #1251
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\UserNotFoundException; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
|
@@ -281,8 +282,10 @@ public function testCreateAuthenticatedToken() | |
$user = $this->createMock(UserInterface::class); | ||
$user->method('getRoles')->willReturn(['ROLE_USER']); | ||
|
||
$expectedToken = new JWTPostAuthenticationToken($user, 'dummy', ['ROLE_USER'], 'dummytoken'); | ||
$expectedToken->setAttribute('token', 'dummytoken'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue #1040 is not about how the JWT payload is stored in the token but about what other authentication systems are used by the project. For instance, we authenticate users with jwt when they interact with our api but users can also login on a web interface using their username/password/2FA. That authentication creates a See https://github.com/lexik/LexikJWTAuthenticationBundle/pull/1244/files#diff-89a63cacb64b31d43879018f8ede57c4f464869da9cc0115a00407187058f3e0 about how to properly cover issue #1040. |
||
$dispatcher = $this->getEventDispatcherMock(); | ||
$dispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(new JWTAuthenticatedEvent(['claim' => 'val'], new JWTPostAuthenticationToken($user, 'dummy', ['ROLE_USER'], 'dummytoken'))), Events::JWT_AUTHENTICATED); | ||
$dispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(new JWTAuthenticatedEvent(['claim' => 'val'], $expectedToken)), Events::JWT_AUTHENTICATED); | ||
|
||
$authenticator = new JWTAuthenticator( | ||
$this->getJWTManagerMock(), | ||
|
@@ -305,6 +308,7 @@ public function testCreateAuthenticatedToken() | |
|
||
$this->assertInstanceOf(JWTPostAuthenticationToken::class, $token); | ||
$this->assertSame('dummytoken', $token->getCredentials()); | ||
$this->assertSame('dummytoken', $token->getAttribute('token')); | ||
} | ||
|
||
public function testParsingAnInvalidTokenThrowsException() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line doesn't fix issue #1040.
Both
getCredentials()
and the "token" attribute only exists onJWTPostAuthenticationToken
. OtherTokenInterface
have neither. It means the code still crashes badly when the received token is not aJWTPostAuthenticationToken
.Beside, other classes implementing a valid
TokenInterface
might have an attribute namedtoken
for their own purpose. That token will certainly not be a JWT token.In other words, you can't assume any
TokenInterface
will work. You must either reject tokens based on their instance type (JWTPostAuthenticationToken
or some, yet to be created,JWTAuthenticationTokenInterface
) or strongly type the$token
argument of thedecode
method with one of these two types. That's what #1244 does.