1 | <?php |
||
10 | * Interface for validating token data. |
||
11 | */ |
||
12 | final class TokenValidator implements TokenValidatorInterface |
||
13 | { |
||
14 | /** |
||
15 | * Validates the given token against the private key and incoming request. |
||
16 | * |
||
17 | * @param string $privateKey The private API key. |
||
18 | * @param Token $token The token extracted from the request. |
||
19 | * @param ServerRequestInterface $request The incoming PSR-7 request. |
||
20 | * |
||
21 | * @return boolean |
||
22 | * |
||
23 | * @throws AuthenticationException 401 Thrown hash is not valid. |
||
24 | */ |
||
25 | public function validate(string $privateKey, Token $token, ServerRequestInterface $request) : bool |
||
26 | { |
||
27 | $method = $request->getMethod(); |
||
28 | $uri = (string)$request->getUri(); |
||
29 | $base64 = base64_encode((string)$request->getBody()); |
||
30 | $data = "{$privateKey}{$method}{$uri}{$token->getTimeStamp()}{$token->getNonce()}{$base64}"; |
||
31 | |||
32 | if (hash('sha256', $data) !== $token->getSignature()) { |
||
33 | throw new AuthenticationException(401, 'Invalid Hash'); |
||
34 | } |
||
35 | |||
36 | return true; |
||
37 | } |
||
39 |