Passed
Push — master ( 11687d...119443 )
by Pol
07:09 queued 04:54
created

JWKSKeyLoader::getPublicKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcPhp\ApiGwAuthenticationBundle\Service\KeyLoader;
6
7
use EcPhp\ApiGwAuthenticationBundle\Exception\ApiGwAuthenticationException;
8
use EcPhp\ApiGwAuthenticationBundle\Service\KeyConverter\KeyConverterInterface;
9
use Symfony\Contracts\HttpClient\HttpClientInterface;
10
use Throwable;
11
12
use function array_key_exists;
13
14
final class JWKSKeyLoader implements KeyLoaderInterface
15
{
16
    private HttpClientInterface $httpClient;
17
18
    private KeyConverterInterface $keyConverter;
19
20
    private KeyLoaderInterface $keyLoader;
21
22 8
    public function __construct(
23
        KeyLoaderInterface $keyLoader,
24
        HttpClientInterface $httpClient,
25
        KeyConverterInterface $keyConverter
26
    ) {
27 8
        $this->keyLoader = $keyLoader;
28 8
        $this->httpClient = $httpClient;
29 8
        $this->keyConverter = $keyConverter;
30 8
    }
31
32 1
    public function getPassphrase(): string
33
    {
34 1
        return $this->keyLoader->getPassphrase();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->keyLoader->getPassphrase() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
35
    }
36
37 1
    public function getPublicKey(): string
38
    {
39 1
        return $this->keyLoader->getPublicKey();
40
    }
41
42 1
    public function getSigningKey(): string
43
    {
44 1
        return $this->keyLoader->getSigningKey();
45
    }
46
47 7
    public function loadKey($type): string
48
    {
49
        // Todo: Implements for PRIVATE key as well.
50 7
        $key = $this->keyLoader->getPublicKey();
51
52
        try {
53 7
            $jwks = $this->httpClient->request('GET', $key);
54 2
        } catch (Throwable $e) {
55 2
            throw new ApiGwAuthenticationException(
56 2
                sprintf('Unable to request uri(%s) for %s key.', $key, $type),
57 2
                $e->getCode(),
58
                $e
59
            );
60
        }
61
62 5
        if (200 !== $statusCode = $jwks->getStatusCode()) {
63 2
            throw new ApiGwAuthenticationException(
64 2
                sprintf('Invalid code(%s) thrown while fetching the %s key at %s.', $statusCode, $type, $key)
65
            );
66
        }
67
68 3
        $jwksArray = $jwks->toArray();
69
70 3
        if (false === array_key_exists('keys', $jwksArray)) {
71
            throw new ApiGwAuthenticationException(
72
                sprintf('Invalid JWKS format of %s key at %s.', $type, $key)
73
            );
74
        }
75
76 3
        if ([] === $jwksArray['keys']) {
77
            throw new ApiGwAuthenticationException(
78
                sprintf('Invalid JWKS format of %s key at %s, keys array is empty.', $type, $key)
79
            );
80
        }
81
82 3
        return current($this->keyConverter->fromJWKStoPEMS($jwksArray['keys']));
83
    }
84
}
85