From 8b722dbb6038d8cdccecb6f23255de12bf63660b Mon Sep 17 00:00:00 2001 From: Florian Guedon Date: Sun, 9 Jan 2022 21:45:04 +0100 Subject: [PATCH] Fix authentication with integer as useridentifier caused argument type error with UserBadge --- Security/Authenticator/JWTAuthenticator.php | 2 +- .../Authenticator/JWTAuthenticatorTest.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Security/Authenticator/JWTAuthenticator.php b/Security/Authenticator/JWTAuthenticator.php index 5a88512e..45020a87 100644 --- a/Security/Authenticator/JWTAuthenticator.php +++ b/Security/Authenticator/JWTAuthenticator.php @@ -115,7 +115,7 @@ public function doAuthenticate(Request $request) /*: Passport */ } $passport = new SelfValidatingPassport( - new UserBadge($payload[$idClaim], + new UserBadge((string)$payload[$idClaim], function ($userIdentifier) use($payload) { return $this->loadUser($payload, $userIdentifier); }) diff --git a/Tests/Security/Authenticator/JWTAuthenticatorTest.php b/Tests/Security/Authenticator/JWTAuthenticatorTest.php index 26208e0f..9ee08b46 100644 --- a/Tests/Security/Authenticator/JWTAuthenticatorTest.php +++ b/Tests/Security/Authenticator/JWTAuthenticatorTest.php @@ -69,6 +69,35 @@ public function testAuthenticate() { $this->assertSame($userStub, ($authenticator->authenticate($this->getRequestMock()))->getUser()); } + public function testAuthenticateWithIntegerIdentifier() { + $userIdClaim = 'sub'; + $payload = [$userIdClaim => 1]; + $rawToken = 'token'; + $userRoles = ['ROLE_USER']; + + $userStub = new AdvancedUserStub(1, 'password', 'user@gmail.com', $userRoles); + + $jwtManager = $this->getJWTManagerMock(null, $userIdClaim); + $jwtManager + ->method('parse') + ->willReturn(['sub' => 1]); + + $userProvider = $this->getUserProviderMock(); + $userProvider + ->method('loadUserByIdentifierAndPayload') + ->with($payload['sub'], $payload) + ->willReturn($userStub); + + $authenticator = new JWTAuthenticator( + $jwtManager, + $this->getEventDispatcherMock(), + $this->getTokenExtractorMock($rawToken), + $userProvider + ); + + $this->assertSame($userStub, ($authenticator->authenticate($this->getRequestMock()))->getUser()); + } + public function testAuthenticateWithExpiredTokenThrowsException() { $jwtManager = $this->getJWTManagerMock(); $jwtManager->method('parse')