<?php
 declare(strict_types=1); namespace Jose\Component\Core; use InvalidArgumentException; use Jose\Component\Core\Util\Base64UrlSafe; use JsonSerializable; use Override; use function array_key_exists; use function in_array; use function is_array; use function sprintf; use const JSON_THROW_ON_ERROR; use const JSON_UNESCAPED_SLASHES; use const JSON_UNESCAPED_UNICODE; class JWK implements JsonSerializable { private array $values = []; public function __construct(array $values) { if (! isset($values['kty'])) { throw new InvalidArgumentException('The parameter "kty" is mandatory.'); } $this->values = $values; } public static function createFromJson(string $json): self { $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); if (! is_array($data)) { throw new InvalidArgumentException('Invalid argument.'); } return new self($data); } #[Override] public function jsonSerialize(): array { return $this->values; } public function get(string $key) { if (! $this->has($key)) { throw new InvalidArgumentException(sprintf('The value identified by "%s" does not exist.', $key)); } return $this->values[$key]; } public function has(string $key): bool { return array_key_exists($key, $this->values); } public function all(): array { return $this->values; } public function thumbprint(string $hash_algorithm): string { if (! in_array($hash_algorithm, hash_algos(), true)) { throw new InvalidArgumentException(sprintf('The hash algorithm "%s" is not supported.', $hash_algorithm)); } $values = array_intersect_key($this->values, array_flip(['kty', 'n', 'e', 'crv', 'x', 'y', 'k'])); ksort($values); $input = json_encode($values, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); if ($input === false) { throw new InvalidArgumentException('Unable to compute the key thumbprint'); } return Base64UrlSafe::encodeUnpadded(hash($hash_algorithm, $input, true)); } public function toPublic(): self { $values = array_diff_key($this->values, array_flip(['p', 'd', 'q', 'dp', 'dq', 'qi'])); return new self($values); } } 