PublicKeyGetter::getVerifyingKey()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

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