Failed Conditions
Push — master ( 6560de...8483ce )
by Florent
05:17
created

X5UFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\KeyManagement;
15
16
use Http\Client\HttpClient;
17
use Http\Message\MessageFactory;
18
use Jose\Component\Core\Converter\JsonConverter;
19
use Jose\Component\Core\JWK;
20
use Jose\Component\Core\JWKSet;
21
use Jose\Component\KeyManagement\KeyConverter\KeyConverter;
22
23
/**
24
 * Class X5UFactory.
25
 */
26
final class X5UFactory extends UrlKeySetFactory
27
{
28
    private $jsonConverter;
29
30
    /**
31
     * X5UFactory constructor.
32
     *
33
     * @param JsonConverter  $jsonConverter
34
     * @param HttpClient     $client
35
     * @param MessageFactory $messageFactory
36
     */
37
    public function __construct(JsonConverter $jsonConverter, HttpClient $client, MessageFactory $messageFactory)
38
    {
39
        $this->jsonConverter = $jsonConverter;
40
        parent::__construct($client, $messageFactory);
41
    }
42
43
    /**
44
     * @param string $url
45
     * @param array  $headers
46
     *
47
     * @throws \InvalidArgumentException
48
     *
49
     * @return JWKSet
50
     */
51
    public function loadFromUrl(string $url, array $headers = []): JWKSet
52
    {
53
        $content = $this->getContent($url, $headers);
54
        $data = $this->jsonConverter->decode($content);
55
        if (!is_array($data)) {
56
            throw new \RuntimeException('Invalid content.');
57
        }
58
59
        $keys = [];
60
        foreach ($data as $kid => $cert) {
61
            $cert = '-----BEGIN CERTIFICATE-----'.PHP_EOL.$cert.PHP_EOL.'-----END CERTIFICATE-----';
62
            $jwk = KeyConverter::loadKeyFromCertificate($cert);
63
            if (is_string($kid)) {
64
                $jwk['kid'] = $kid;
65
                $keys[$kid] = JWK::create($jwk);
66
            } else {
67
                $keys[] = JWK::create($jwk);
68
            }
69
        }
70
71
        return JWKSet::createFromKeys($keys);
72
    }
73
}
74