diff --git a/Security/Authenticator/JWTAuthenticator.php b/Security/Authenticator/JWTAuthenticator.php index c5bd1aef..8dd0595a 100644 --- a/Security/Authenticator/JWTAuthenticator.php +++ b/Security/Authenticator/JWTAuthenticator.php @@ -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 \LogicException('Unable to extract a JWT token from the request. Also, make sure to call `supports()` before `authenticate()` to get a proper client error.'); + } try { if (!$payload = $this->jwtManager->parse($token)) { diff --git a/Tests/Security/Authenticator/JWTAuthenticatorTest.php b/Tests/Security/Authenticator/JWTAuthenticatorTest.php index 20e47790..589e0502 100644 --- a/Tests/Security/Authenticator/JWTAuthenticatorTest.php +++ b/Tests/Security/Authenticator/JWTAuthenticatorTest.php @@ -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(\LogicException::class); + + $authenticator->authenticate($this->getRequestMock()); + } + private function getJWTManagerMock($userIdentityField = null, $userIdClaim = null) { $jwtManager = $this->getMockBuilder(DummyJWTManager::class)