Total Complexity | 6 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
16 | 1 | class HashAuthenticator implements IRequestAuthenticator |
|
17 | { |
||
18 | |||
19 | /** Auth token request header name */ |
||
20 | public const AUTH_HEADER = 'X-HTTP-AUTH-TOKEN'; |
||
21 | |||
22 | 1 | public function __construct( |
|
23 | protected IRequest $request, |
||
24 | protected IAuthTokenCalculator $calculator, |
||
25 | ) |
||
26 | { |
||
27 | 1 | } |
|
28 | |||
29 | /** |
||
30 | * @throws AuthenticationException |
||
31 | */ |
||
32 | public function authenticate(IInput $input): bool |
||
33 | { |
||
34 | 1 | $requested = $this->getRequestedHash(); |
|
35 | 1 | if (!$requested) { |
|
36 | 1 | throw new AuthenticationException('Authentication header not found.'); |
|
37 | } |
||
38 | |||
39 | 1 | $expected = $this->getExpectedHash($input); |
|
40 | 1 | if ($requested !== $expected) { |
|
41 | 1 | throw new AuthenticationException('Authentication tokens do not match.'); |
|
42 | } |
||
43 | 1 | return true; |
|
44 | } |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Get request hash |
||
49 | * @return string|null |
||
50 | */ |
||
51 | protected function getRequestedHash(): ?string |
||
52 | { |
||
53 | 1 | return $this->request->getHeader(self::AUTH_HEADER); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get expected hash |
||
58 | * @return string |
||
59 | */ |
||
60 | protected function getExpectedHash(IInput $input): string |
||
63 | } |
||
64 | } |
||
65 |