vendor/web-token/jwt-library/Core/JWK.php line 20

  1. <?php
  2. declare(strict_types=1);
  3. namespace Jose\Component\Core;
  4. use InvalidArgumentException;
  5. use Jose\Component\Core\Util\Base64UrlSafe;
  6. use JsonSerializable;
  7. use function array_key_exists;
  8. use function in_array;
  9. use function is_array;
  10. use const JSON_THROW_ON_ERROR;
  11. use const JSON_UNESCAPED_SLASHES;
  12. use const JSON_UNESCAPED_UNICODE;
  13. /**
  14.  * @see \Jose\Tests\Component\Core\JWKTest
  15.  */
  16. class JWK implements JsonSerializable
  17. {
  18.     private array $values = [];
  19.     /**
  20.      * Creates a JWK object using the given values. The member "kty" is mandatory. Other members are NOT checked.
  21.      */
  22.     public function __construct(array $values)
  23.     {
  24.         if (! isset($values['kty'])) {
  25.             throw new InvalidArgumentException('The parameter "kty" is mandatory.');
  26.         }
  27.         $this->values $values;
  28.     }
  29.     /**
  30.      * Creates a JWK object using the given Json string.
  31.      */
  32.     public static function createFromJson(string $json): self
  33.     {
  34.         $data json_decode($jsontrue512JSON_THROW_ON_ERROR);
  35.         if (! is_array($data)) {
  36.             throw new InvalidArgumentException('Invalid argument.');
  37.         }
  38.         return new self($data);
  39.     }
  40.     /**
  41.      * Returns the values to be serialized.
  42.      */
  43.     public function jsonSerialize(): array
  44.     {
  45.         return $this->values;
  46.     }
  47.     /**
  48.      * Get the value with a specific key.
  49.      *
  50.      * @param string $key The key
  51.      *
  52.      * @return mixed|null
  53.      */
  54.     public function get(string $key)
  55.     {
  56.         if (! $this->has($key)) {
  57.             throw new InvalidArgumentException(sprintf('The value identified by "%s" does not exist.'$key));
  58.         }
  59.         return $this->values[$key];
  60.     }
  61.     /**
  62.      * Returns true if the JWK has the value identified by.
  63.      *
  64.      * @param string $key The key
  65.      */
  66.     public function has(string $key): bool
  67.     {
  68.         return array_key_exists($key$this->values);
  69.     }
  70.     /**
  71.      * Get all values stored in the JWK object.
  72.      *
  73.      * @return array Values of the JWK object
  74.      */
  75.     public function all(): array
  76.     {
  77.         return $this->values;
  78.     }
  79.     /**
  80.      * Returns the thumbprint of the key.
  81.      *
  82.      * @see https://tools.ietf.org/html/rfc7638
  83.      */
  84.     public function thumbprint(string $hash_algorithm): string
  85.     {
  86.         if (! in_array($hash_algorithmhash_algos(), true)) {
  87.             throw new InvalidArgumentException(sprintf('The hash algorithm "%s" is not supported.'$hash_algorithm));
  88.         }
  89.         $values array_intersect_key($this->valuesarray_flip(['kty''n''e''crv''x''y''k']));
  90.         ksort($values);
  91.         $input json_encode($valuesJSON_UNESCAPED_SLASHES JSON_UNESCAPED_UNICODE);
  92.         if ($input === false) {
  93.             throw new InvalidArgumentException('Unable to compute the key thumbprint');
  94.         }
  95.         return Base64UrlSafe::encodeUnpadded(hash($hash_algorithm$inputtrue));
  96.     }
  97.     /**
  98.      * Returns the associated public key.
  99.      * This method has no effect for:
  100.      * - public keys
  101.      * - shared keys
  102.      * - unknown keys.
  103.      *
  104.      * Known keys are "oct", "RSA", "EC" and "OKP".
  105.      */
  106.     public function toPublic(): self
  107.     {
  108.         $values array_diff_key($this->valuesarray_flip(['p''d''q''dp''dq''qi']));
  109.         return new self($values);
  110.     }
  111. }