| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\KeyManagement; |
| 6 |
|
| 7 |
use Jose\Component\Core\JWK; |
| 8 |
use Jose\Component\Core\JWKSet; |
| 9 |
use Jose\Component\Core\Util\JsonConverter; |
| 10 |
use Jose\Component\KeyManagement\KeyConverter\KeyConverter; |
| 11 |
use RuntimeException; |
| 12 |
use function is_array; |
| 13 |
use function is_string; |
| 14 |
|
| 15 |
class X5UFactory extends UrlKeySetFactory |
| 16 |
{ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function loadFromUrl(string $url, array $header = []): JWKSet |
| 21 |
{ |
| 22 |
$content = $this->getContent($url, $header); |
| 23 |
$data = JsonConverter::decode($content); |
| 24 |
if (! is_array($data)) { |
| 25 |
throw new RuntimeException('Invalid content.'); |
| 26 |
} |
| 27 |
|
| 28 |
$keys = []; |
| 29 |
foreach ($data as $kid => $cert) { |
| 30 |
if (mb_strpos((string) $cert, '-----BEGIN CERTIFICATE-----') === false) { |
| 31 |
$cert = '-----BEGIN CERTIFICATE-----' . "\n" . $cert . "\n" . '-----END CERTIFICATE-----'; |
| 32 |
} |
| 33 |
$jwk = KeyConverter::loadKeyFromCertificate($cert); |
| 34 |
if (is_string($kid)) { |
| 35 |
$jwk['kid'] = $kid; |
| 36 |
$keys[$kid] = new JWK($jwk); |
| 37 |
} else { |
| 38 |
$keys[] = new JWK($jwk); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return new JWKSet($keys); |
| 43 |
} |
| 44 |
} |