| Total Complexity | 6 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 13 | final class Verifier |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var HeaderValidator |
||
| 17 | */ |
||
| 18 | private $validator; |
||
| 19 | |||
| 20 | 86 | public function __construct() |
|
| 21 | { |
||
| 22 | 86 | $this->validator = (new HeaderValidator()) |
|
| 23 | 86 | ->addRule(Specification::AUTH_HEADER, Specification::AUTH_REGEXP) |
|
| 24 | 86 | ->addRule(Specification::SIGN_HEADER, Specification::SIGN_REGEXP); |
|
| 25 | 86 | } |
|
| 26 | |||
| 27 | 84 | public function verify(RequestInterface $request, string $secret): bool |
|
| 28 | { |
||
| 29 | 84 | if (false === $matches = $this->validator->conforms($request)) { |
|
|
|
|||
| 30 | 81 | return false; |
|
| 31 | } |
||
| 32 | |||
| 33 | 57 | $clientSideSignature = $matches[Specification::AUTH_HEADER][1]; |
|
| 34 | |||
| 35 | 57 | $serverSideSignature = HashCalculator::hmac( |
|
| 36 | 57 | RequestSerializer::serialize($this->withoutUnsignedHeaders($request)), |
|
| 37 | 57 | $secret |
|
| 38 | ); |
||
| 39 | |||
| 40 | 57 | return \hash_equals($serverSideSignature, $clientSideSignature); |
|
| 41 | } |
||
| 42 | |||
| 43 | 57 | private function withoutUnsignedHeaders(RequestInterface $request): RequestInterface |
|
| 54 | } |
||
| 55 | } |
||
| 56 |