Skip to content

Commit

Permalink
bug #976 Fix authentication with integer as useridentifier (Florian G…
Browse files Browse the repository at this point in the history
…uedon)

This PR was merged into the 2.x branch.

Discussion
----------

Fix authentication with integer as useridentifier

Fix authentication with integer as useridentifier caused argument type error with UserBadge (Maybe only with Symfony >= 6.0)

Commits
-------

8b722db Fix authentication with integer as useridentifier caused argument type error with UserBadge
  • Loading branch information
chalasr committed Jan 27, 2022
2 parents 6a196f3 + 8b722db commit ada31e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Security/Authenticator/JWTAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,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);
})
Expand Down
29 changes: 29 additions & 0 deletions Tests/Security/Authenticator/JWTAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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')
Expand Down

0 comments on commit ada31e7

Please sign in to comment.