-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow using authorization query parameter
Signed-off-by: azjezz <azjezz@protonmail.com>
- Loading branch information
Showing
8 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Freddie\Security\JWT\Extractor; | ||
|
||
use Freddie\Helper\FlatQueryParser; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
use function is_string; | ||
use function strlen; | ||
use function BenTools\QueryString\query_string; | ||
|
||
final class QueryTokenExtractor implements PSR7TokenExtractorInterface | ||
{ | ||
public function __construct( | ||
private string $name = 'authorization', | ||
) { | ||
} | ||
|
||
public function extract(ServerRequestInterface $request): ?string | ||
{ | ||
$qs = query_string($request->getUri(), new FlatQueryParser()); | ||
$authorizationQuery = $qs->getParam($this->name); | ||
if (!is_string($authorizationQuery) || strlen($authorizationQuery) < 41) { | ||
return null; | ||
} | ||
|
||
return $authorizationQuery; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/Unit/Security/JWT/Extractor/QueryTokenExtractorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Freddie\Tests\Unit\Security\JWT\Extractor; | ||
|
||
use Freddie\Security\JWT\Extractor\QueryTokenExtractor; | ||
use React\Http\Message\ServerRequest; | ||
|
||
it('extracts token from authorization query parameter', function (string $uri, ?string $expected) { | ||
$extractor = new QueryTokenExtractor(); | ||
$request = new ServerRequest('GET', $uri); | ||
expect($extractor->extract($request))->toBe($expected); | ||
})->with(function () { | ||
$validToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30._esyynAyo2Z6PyGe0mM_SuQ3c-C7sMQJ1YxVLvlj80A'; | ||
|
||
yield ['/.well-known/mercure?authorization=' . $validToken, $validToken]; | ||
yield ['/.well-known/mercure?authorization=foo', null]; | ||
yield ['/.well-known/mercure?authorization', null]; | ||
yield ['/.well-known/mercure', null]; | ||
}); |