Skip to content

Commit

Permalink
Fixes TypeError in JWTManager
Browse files Browse the repository at this point in the history
When the TokenExtractor extracts a token, it will return
`string|false`.  The JWTAuthenticator then passes this value to
`JWTManager::doAuthenticate` which can only accept a string.  If a false
is returned by the TokenExtractor, PHP throws a type error.  This commit
checks the return value and throws an exception if it returns false.

Issue: #1066
  • Loading branch information
magikid committed Sep 6, 2022
1 parent d7cbef6 commit 0591806
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Security/Authenticator/JWTAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public function supports(Request $request): ?bool
public function doAuthenticate(Request $request) /*: Passport */
{
$token = $this->getTokenExtractor()->extract($request);
if ($token === false) {
throw new InvalidTokenException('Unable to extract JWT token');
}

try {
if (!$payload = $this->jwtManager->parse($token)) {
Expand Down
18 changes: 18 additions & 0 deletions Tests/Security/Authenticator/JWTAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,24 @@ public function testCreateAuthenticatedToken()
$this->assertSame('dummytoken', $token->getCredentials());
}

public function testParsingAnInvalidTokenThrowsException()
{
$jwtManager = $this->getJWTManagerMock();
$jwtManager->method('parse')
->willThrowException(new InvalidTokenException('Unable to extract JWT token'));

$authenticator = new JWTAuthenticator(
$jwtManager,
$this->getEventDispatcherMock(),
$this->getTokenExtractorMock(false),
$this->getUserProviderMock()
);

$this->expectException(InvalidTokenException::class);

$authenticator->authenticate($this->getRequestMock());
}

private function getJWTManagerMock($userIdentityField = null, $userIdClaim = null)
{
$jwtManager = $this->getMockBuilder(DummyJWTManager::class)
Expand Down

0 comments on commit 0591806

Please sign in to comment.