Skip to content

Commit

Permalink
Merge pull request #1040 from kenjis/fix-jwt-loggedIn
Browse files Browse the repository at this point in the history
fix: `JWT::loggedIn()` does not remove `Bearer` prefix
  • Loading branch information
datamweb committed Feb 24, 2024
2 parents a538a1a + 44e16bd commit 3fa4ec3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
27 changes: 24 additions & 3 deletions src/Authentication/Authenticators/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -206,14 +207,34 @@ public function loggedIn(): bool
/** @var IncomingRequest $request */
$request = service('request');

/** @var AuthJWT $config */
$config = config('AuthJWT');
$token = $this->getTokenFromRequest($request);

return $this->attempt([
'token' => $request->getHeaderLine($config->authenticatorHeader),
'token' => $token,
])->isOK();
}

/**
* Gets token from Request.
*/
public function getTokenFromRequest(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.
*/
Expand Down
21 changes: 1 addition & 20 deletions src/Filters/JWTAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Shield\Authentication\Authenticators\JWT;
use CodeIgniter\Shield\Config\AuthJWT;
use Config\Services;

/**
Expand All @@ -45,7 +44,7 @@ public function before(RequestInterface $request, $arguments = null)
/** @var JWT $authenticator */
$authenticator = auth('jwt')->getAuthenticator();

$token = $this->getTokenFromHeader($request);
$token = $authenticator->getTokenFromRequest($request);

$result = $authenticator->attempt(['token' => $token]);

Expand All @@ -62,24 +61,6 @@ public function before(RequestInterface $request, $arguments = null)
}
}

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;
}

/**
* We don't have anything to do here.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Authentication/Authenticators/JWTAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3fa4ec3

Please sign in to comment.