Skip to content

Commit eb99626

Browse files
committed
feat: remove Bearer in token at the first time
1 parent fd1f5a1 commit eb99626

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/Authentication/Authenticators/JWT.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ public function check(array $credentials): Result
108108
]);
109109
}
110110

111-
if (strpos($credentials['token'], 'Bearer') === 0) {
112-
$credentials['token'] = trim(substr($credentials['token'], 6));
113-
}
114-
115111
// Check JWT
116112
try {
117113
$this->payload = $this->decodeJWT($credentials['token']);

src/Filters/JWTAuth.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ public function before(RequestInterface $request, $arguments = null)
3737
/** @var JWT $authenticator */
3838
$authenticator = auth('jwt')->getAuthenticator();
3939

40-
$result = $authenticator->attempt([
41-
'token' => $request->getHeaderLine(
42-
setting('Auth.authenticatorHeader')['jwt'] ?? 'Authorization'
43-
),
44-
]);
40+
$token = $this->getTokenFromHeader($request);
41+
42+
$result = $authenticator->attempt(['token' => $token]);
4543

4644
if (! $result->isOK()) {
4745
return Services::response()
@@ -56,6 +54,19 @@ public function before(RequestInterface $request, $arguments = null)
5654
}
5755
}
5856

57+
private function getTokenFromHeader(RequestInterface $request): string
58+
{
59+
$tokenHeader = $request->getHeaderLine(
60+
setting('Auth.authenticatorHeader')['jwt'] ?? 'Authorization'
61+
);
62+
63+
if (strpos($tokenHeader, 'Bearer') === 0) {
64+
return trim(substr($tokenHeader, 6));
65+
}
66+
67+
return $tokenHeader;
68+
}
69+
5970
/**
6071
* We don't have anything to do here.
6172
*

0 commit comments

Comments
 (0)