Skip to content

Commit e9850ab

Browse files
committed
make bearer header case insensitive
1 parent 3e322fe commit e9850ab

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88
### Added
99
- Added sensitive parameter to avoid sensitive data being included in stack traces (PR #1483)
1010

11+
### Fixed
12+
- Made the Bearer header case insensitive to match the specs correctly (PR #XXX)
13+
1114
## [9.2.0] - released 2025-02-15
1215
### Added
1316
- Added a new function to the provided ClientTrait, `supportsGrantType` to allow the auth server to issue the response `unauthorized_client` when applicable (PR #1420)

src/AuthorizationValidators/BearerTokenValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function validateAuthorization(ServerRequestInterface $request): ServerRe
9494
}
9595

9696
$header = $request->getHeader('authorization');
97-
$jwt = trim((string) preg_replace('/^\s*Bearer\s/', '', $header[0]));
97+
$jwt = trim((string) preg_replace('/^\s*Bearer\s/i', '', $header[0]));
9898

9999
if ($jwt === '') {
100100
throw OAuthServerException::accessDenied('Missing "Bearer" token');

tests/AuthorizationValidators/BearerTokenValidatorTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function testBearerTokenValidatorAcceptsExpiredTokenWithinLeeway(): void
105105
self::assertArrayHasKey('authorization', $validRequest->getHeaders());
106106
}
107107

108-
public function testBearerTokenValidatorRejectsExpiredTokenBeyondLeeway(): void
108+
public function testBearerTokenValidatorIsNotCaseSensitive(): void
109109
{
110110
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
111111

@@ -135,4 +135,31 @@ public function testBearerTokenValidatorRejectsExpiredTokenBeyondLeeway(): void
135135

136136
$bearerTokenValidator->validateAuthorization($request);
137137
}
138+
139+
public function testBearerTokenValidatorCaseInsensitiveWithBearerHeader(): void
140+
{
141+
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
142+
143+
$bearerTokenValidator = new BearerTokenValidator($accessTokenRepositoryMock);
144+
$bearerTokenValidator->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
145+
146+
$bearerTokenValidatorReflection = new ReflectionClass(BearerTokenValidator::class);
147+
$jwtConfiguration = $bearerTokenValidatorReflection->getProperty('jwtConfiguration');
148+
149+
$validJwt = $jwtConfiguration->getValue($bearerTokenValidator)->builder()
150+
->permittedFor('client-id')
151+
->identifiedBy('token-id')
152+
->issuedAt(new DateTimeImmutable())
153+
->canOnlyBeUsedAfter(new DateTimeImmutable())
154+
->expiresAt((new DateTimeImmutable())->add(new DateInterval('PT1H')))
155+
->relatedTo('user-id')
156+
->withClaim('scopes', 'scope1 scope2 scope3 scope4')
157+
->getToken(new Sha256(), InMemory::file(__DIR__ . '/../Stubs/private.key'));
158+
159+
$request = (new ServerRequest())->withHeader('authorization', sprintf('bEaReR %s', $validJwt->toString()));
160+
161+
$validRequest = $bearerTokenValidator->validateAuthorization($request);
162+
163+
self::assertArrayHasKey('authorization', $validRequest->getHeaders());
164+
}
138165
}

0 commit comments

Comments
 (0)