|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace lepiaf\SapientBundle\Service; |
|
5
|
|
|
|
|
6
|
|
|
use lepiaf\SapientBundle\Exception\{ |
|
7
|
|
|
RequesterHeaderMissingException, |
|
8
|
|
|
NoKeyFoundForRequesterException, |
|
9
|
|
|
SignerHeaderMissingException |
|
10
|
|
|
}; |
|
11
|
|
|
use Psr\Http\Message\RequestInterface; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
|
|
14
|
|
|
class PublicKeyGetter |
|
15
|
|
|
{ |
|
16
|
|
|
public const HEADER_SIGNER = 'Sapient-Signer'; |
|
17
|
|
|
public const HEADER_REQUESTER = 'Sapient-Requester'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $sealingPublicKeys; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $verifyingPublicKeys; |
|
28
|
|
|
|
|
29
|
13 |
|
public function __construct(array $sealingPublicKeys, array $verifyingPublicKeys) |
|
30
|
|
|
{ |
|
31
|
13 |
|
$this->sealingPublicKeys = $sealingPublicKeys; |
|
32
|
13 |
|
$this->verifyingPublicKeys = $verifyingPublicKeys; |
|
33
|
13 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param RequestInterface $request |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
* |
|
40
|
|
|
* @throws RequesterHeaderMissingException |
|
41
|
|
|
* @throws NoKeyFoundForRequesterException |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function getSealingKey(RequestInterface $request): string |
|
44
|
|
|
{ |
|
45
|
4 |
|
if (!$request->hasHeader(self::HEADER_REQUESTER) || 0 === \count($request->getHeader(self::HEADER_REQUESTER))) { |
|
46
|
2 |
|
throw new RequesterHeaderMissingException(sprintf('%s header is missing.', self::HEADER_REQUESTER)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
foreach ($this->sealingPublicKeys as $sealingPublicKey) { |
|
50
|
2 |
|
if ($request->getHeader(self::HEADER_REQUESTER)[0] === $sealingPublicKey['name']) { |
|
51
|
2 |
|
return $sealingPublicKey['key']; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
throw new NoKeyFoundForRequesterException('Sealing key not found.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param ResponseInterface $response |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
* |
|
63
|
|
|
* @throws SignerHeaderMissingException |
|
64
|
|
|
* @throws NoKeyFoundForRequesterException |
|
65
|
|
|
*/ |
|
66
|
6 |
|
public function getVerifyingKey(ResponseInterface $response): string |
|
67
|
|
|
{ |
|
68
|
6 |
|
if (!$response->hasHeader(self::HEADER_SIGNER) || 0 === \count($response->getHeader(self::HEADER_SIGNER))) { |
|
69
|
2 |
|
throw new SignerHeaderMissingException(sprintf('%s header is missing.', self::HEADER_SIGNER)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
foreach ($this->verifyingPublicKeys as $verifyingPublicKeys) { |
|
73
|
4 |
|
if ($response->getHeader(self::HEADER_SIGNER)[0] === $verifyingPublicKeys['name']) { |
|
74
|
4 |
|
return $verifyingPublicKeys['key']; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
throw new NoKeyFoundForRequesterException('Verifying key not found.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param ResponseInterface $response |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
* |
|
86
|
|
|
* @throws SignerHeaderMissingException |
|
87
|
|
|
* @throws NoKeyFoundForRequesterException |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function getRequestVerifyingKey(RequestInterface $request): string |
|
90
|
|
|
{ |
|
91
|
2 |
|
if (!$request->hasHeader(self::HEADER_REQUESTER) || 0 === \count($request->getHeader(self::HEADER_REQUESTER))) { |
|
92
|
|
|
throw new SignerHeaderMissingException(sprintf('%s header is missing.', self::HEADER_REQUESTER)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
foreach ($this->verifyingPublicKeys as $verifyingPublicKeys) { |
|
96
|
2 |
|
if ($request->getHeader(self::HEADER_REQUESTER)[0] === $verifyingPublicKeys['name']) { |
|
97
|
2 |
|
return $verifyingPublicKeys['key']; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
throw new NoKeyFoundForRequesterException('Verifying key not found for requester.'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|