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
|
|
|
|