Lines
100.00%
7 / 7
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| BearerTokenCheck | 100.00% | 7 / 7 | 100.00% | 2 / 2 | 5 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| admitsRequest | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 4 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Http; | |
| 6 | ||
| 7 | use Amp\Http\Server\Request; | |
| 8 | use SensitiveParameter; | |
| 9 | ||
| 10 | final readonly class BearerTokenCheck | |
| 11 | { | |
| 12 | private const string BEARER_TOKEN = '~\ABearer +(\S+)\z~i'; | |
| 13 | ||
| 14 | public function __construct( | |
| 15 | #[SensitiveParameter] | |
| 16 | private ?string $token, | |
| 17 | ) {} | |
| 18 | ||
| 19 | public function admitsRequest(Request $request): bool | |
| 20 | { | |
| 21 | if ($this->token === null) { | |
| 22 | return true; | |
| 23 | } | |
| 24 | ||
| 25 | $authorization = $request->getHeader('authorization'); | |
| 26 | ||
| 27 | return $authorization !== null | |
| 28 | && preg_match(self::BEARER_TOKEN, $authorization, $matches) === 1 | |
| 29 | && hash_equals($this->token, $matches[1]); | |
| 30 | } | |
| 31 | } |