Skip to content

Commit 39e565c

Browse files
authored
Merge pull request #721 from cakephp/bugfix/token-auth
Fix token auth prefix removal.
2 parents 1366eb4 + f5d38f9 commit 39e565c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Authenticator/TokenAuthenticator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ protected function getToken(ServerRequestInterface $request): ?string
6363
*/
6464
protected function stripTokenPrefix(string $token, string $prefix): string
6565
{
66-
return trim(str_ireplace($prefix, '', $token));
66+
$prefixLength = mb_strlen($prefix);
67+
if (mb_substr(mb_strtolower($token), 0, $prefixLength) === mb_strtolower($prefix)) {
68+
$token = mb_substr($token, $prefixLength);
69+
}
70+
71+
return trim($token);
6772
}
6873

6974
/**

tests/TestCase/Authenticator/TokenAuthenticatorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public function testTokenPrefix()
150150
$result = $tokenAuth->authenticate($requestWithHeaders);
151151
$this->assertInstanceOf(Result::class, $result);
152152
$this->assertSame(Result::FAILURE_IDENTITY_NOT_FOUND, $result->getStatus());
153+
154+
// should not remove prefix from token
155+
$requestWithHeaders = $this->request->withAddedHeader('X-Dipper-Auth', 'mari mariano');
156+
$tokenAuth = new TokenAuthenticator($this->identifiers, [
157+
'header' => 'X-Dipper-Auth',
158+
'tokenPrefix' => 'mari',
159+
]);
160+
$result = $tokenAuth->authenticate($requestWithHeaders);
161+
$this->assertInstanceOf(Result::class, $result);
162+
$this->assertSame(Result::SUCCESS, $result->getStatus());
153163
}
154164

155165
/**

0 commit comments

Comments
 (0)