Lines 100.00% 7 / 7
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 admitsRequest 100.00% 6 / 6 100.00% 1 / 1 4
10final 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}