Skip to content

Commit 01643fd

Browse files
committed
Add test for AccessTokenHeaderRegex and adjust regex
A new test was added to AccessTokenAuthenticatorTest to ensure that the regular expression in HeaderAccessTokenExtractor works correctly. The regular expression was tweaked to support a wider range of tokens, especially those ending with an equals sign.
1 parent a99669c commit 01643fd

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

AccessToken/HeaderAccessTokenExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929
private readonly string $tokenType = 'Bearer'
3030
) {
3131
$this->regex = sprintf(
32-
'/^%s([a-zA-Z0-9\-_\+~\/\.]+)$/',
32+
'/^%s([a-zA-Z0-9\-_\+~\/\.]+=*)$/',
3333
'' === $this->tokenType ? '' : preg_quote($this->tokenType).'\s+'
3434
);
3535
}

Tests/Authenticator/AccessTokenAuthenticatorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
1919
use Symfony\Component\Security\Http\AccessToken\AccessTokenExtractorInterface;
2020
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;
21+
use Symfony\Component\Security\Http\AccessToken\HeaderAccessTokenExtractor;
2122
use Symfony\Component\Security\Http\Authenticator\AccessTokenAuthenticator;
2223
use Symfony\Component\Security\Http\Authenticator\FallbackUserLoader;
2324
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
@@ -159,4 +160,31 @@ public function testAuthenticateWithFallbackUserLoader()
159160

160161
$this->assertEquals('test', $passport->getUser()->getUserIdentifier());
161162
}
163+
164+
/**
165+
* @dataProvider provideAccessTokenHeaderRegex
166+
*/
167+
public function testAccessTokenHeaderRegex(string $input, ?string $expectedToken)
168+
{
169+
// Given
170+
$extractor = new HeaderAccessTokenExtractor();
171+
$request = Request::create('/test', 'GET', [], [], [], ['HTTP_AUTHORIZATION' => $input]);
172+
173+
// When
174+
$token = $extractor->extractAccessToken($request);
175+
176+
// Then
177+
$this->assertEquals($expectedToken, $token);
178+
}
179+
180+
public function provideAccessTokenHeaderRegex(): array
181+
{
182+
return [
183+
['Bearer token', 'token'],
184+
['Bearer mF_9.B5f-4.1JqM', 'mF_9.B5f-4.1JqM'],
185+
['Bearer d3JvbmdfcmVnZXhwX2V4bWFwbGU=', 'd3JvbmdfcmVnZXhwX2V4bWFwbGU='],
186+
['Bearer Not Valid', null],
187+
['Bearer (NotOK123)', null],
188+
];
189+
}
162190
}

0 commit comments

Comments
 (0)