Skip to content

Commit

Permalink
refactor: rename getTokenFromHeader to getTokenFromRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 22, 2024
1 parent b74d082 commit 44e16bd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Authentication/Authenticators/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Filters/JWTAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

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 44e16bd

Please sign in to comment.