diff --git a/src/Authentication/Authenticators/JWT.php b/src/Authentication/Authenticators/JWT.php index b41c8d721..7dc1dd1f6 100644 --- a/src/Authentication/Authenticators/JWT.php +++ b/src/Authentication/Authenticators/JWT.php @@ -207,14 +207,17 @@ public function loggedIn(): bool /** @var IncomingRequest $request */ $request = service('request'); - $token = $this->getTokenFromHeader($request); + $token = $this->getTokenFromRequest($request); return $this->attempt([ 'token' => $token, ])->isOK(); } - public function getTokenFromHeader(RequestInterface $request): string + /** + * Gets token from Request. + */ + public function getTokenFromRequest(RequestInterface $request): string { assert($request instanceof IncomingRequest); diff --git a/src/Filters/JWTAuth.php b/src/Filters/JWTAuth.php index d0f67aad8..a650702b8 100644 --- a/src/Filters/JWTAuth.php +++ b/src/Filters/JWTAuth.php @@ -44,7 +44,7 @@ public function before(RequestInterface $request, $arguments = null) /** @var JWT $authenticator */ $authenticator = auth('jwt')->getAuthenticator(); - $token = $authenticator->getTokenFromHeader($request); + $token = $authenticator->getTokenFromRequest($request); $result = $authenticator->attempt(['token' => $token]); diff --git a/tests/Authentication/Authenticators/JWTAuthenticatorTest.php b/tests/Authentication/Authenticators/JWTAuthenticatorTest.php index d4ee37f90..13e64eacd 100644 --- a/tests/Authentication/Authenticators/JWTAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/JWTAuthenticatorTest.php @@ -282,4 +282,16 @@ private function generateJWT(?Time $clock = null): string return $generator->generateToken($this->user); } + + public function testGetTokenFromRequest(): void + { + $request = Services::incomingrequest(null, false); + + $jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; + $request->setHeader('Authorization', 'Bearer ' . $jwt); + + $token = $this->auth->getTokenFromRequest($request); + + $this->assertSame($jwt, $token); + } }