| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Core; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use Jose\Component\Core\Util\Base64UrlSafe; |
| 9 |
use JsonSerializable; |
| 10 |
use function array_key_exists; |
| 11 |
use function in_array; |
| 12 |
use function is_array; |
| 13 |
use const JSON_THROW_ON_ERROR; |
| 14 |
use const JSON_UNESCAPED_SLASHES; |
| 15 |
use const JSON_UNESCAPED_UNICODE; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class JWK implements JsonSerializable |
| 21 |
{ |
| 22 |
private array $values = []; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct(array $values) |
| 28 |
{ |
| 29 |
if (! isset($values['kty'])) { |
| 30 |
throw new InvalidArgumentException('The parameter "kty" is mandatory.'); |
| 31 |
} |
| 32 |
$this->values = $values; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static function createFromJson(string $json): self |
| 39 |
{ |
| 40 |
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); |
| 41 |
if (! is_array($data)) { |
| 42 |
throw new InvalidArgumentException('Invalid argument.'); |
| 43 |
} |
| 44 |
|
| 45 |
return new self($data); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function jsonSerialize(): array |
| 52 |
{ |
| 53 |
return $this->values; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function get(string $key) |
| 64 |
{ |
| 65 |
if (! $this->has($key)) { |
| 66 |
throw new InvalidArgumentException(sprintf('The value identified by "%s" does not exist.', $key)); |
| 67 |
} |
| 68 |
|
| 69 |
return $this->values[$key]; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function has(string $key): bool |
| 78 |
{ |
| 79 |
return array_key_exists($key, $this->values); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function all(): array |
| 88 |
{ |
| 89 |
return $this->values; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function thumbprint(string $hash_algorithm): string |
| 98 |
{ |
| 99 |
if (! in_array($hash_algorithm, hash_algos(), true)) { |
| 100 |
throw new InvalidArgumentException(sprintf('The hash algorithm "%s" is not supported.', $hash_algorithm)); |
| 101 |
} |
| 102 |
$values = array_intersect_key($this->values, array_flip(['kty', 'n', 'e', 'crv', 'x', 'y', 'k'])); |
| 103 |
ksort($values); |
| 104 |
$input = json_encode($values, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
| 105 |
if ($input === false) { |
| 106 |
throw new InvalidArgumentException('Unable to compute the key thumbprint'); |
| 107 |
} |
| 108 |
|
| 109 |
return Base64UrlSafe::encodeUnpadded(hash($hash_algorithm, $input, true)); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function toPublic(): self |
| 122 |
{ |
| 123 |
$values = array_diff_key($this->values, array_flip(['p', 'd', 'q', 'dp', 'dq', 'qi'])); |
| 124 |
|
| 125 |
return new self($values); |
| 126 |
} |
| 127 |
} |