diff --git a/src/Authentication/Authenticators/JWT.php b/src/Authentication/Authenticators/JWT.php index 84efc2fb5..e811912a5 100644 --- a/src/Authentication/Authenticators/JWT.php +++ b/src/Authentication/Authenticators/JWT.php @@ -14,6 +14,7 @@ namespace CodeIgniter\Shield\Authentication\Authenticators; use CodeIgniter\HTTP\IncomingRequest; +use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\I18n\Time; use CodeIgniter\Shield\Authentication\AuthenticationException; use CodeIgniter\Shield\Authentication\AuthenticatorInterface; @@ -209,11 +210,31 @@ public function loggedIn(): bool /** @var AuthJWT $config */ $config = config('AuthJWT'); + $token = $this->getTokenFromHeader($request); + return $this->attempt([ - 'token' => $request->getHeaderLine($config->authenticatorHeader), + 'token' => $token, ])->isOK(); } + private function getTokenFromHeader(RequestInterface $request): string + { + assert($request instanceof IncomingRequest); + + /** @var AuthJWT $config */ + $config = config('AuthJWT'); + + $tokenHeader = $request->getHeaderLine( + $config->authenticatorHeader ?? 'Authorization' + ); + + if (strpos($tokenHeader, 'Bearer') === 0) { + return trim(substr($tokenHeader, 6)); + } + + return $tokenHeader; + } + /** * Logs the given user in by saving them to the class. */