Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Core/JWK.php

raw symbols SCIP
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 * @see \Jose\Tests\Component\Core\JWKTest
19 */
20 class JWK implements JsonSerializable
21 {
22 private array $values = [];
23
24 /**
25 * Creates a JWK object using the given values. The member "kty" is mandatory. Other members are NOT checked.
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 * Creates a JWK object using the given Json string.
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 * Returns the values to be serialized.
50 */
51 public function jsonSerialize(): array
52 {
53 return $this->values;
54 }
55
56 /**
57 * Get the value with a specific key.
58 *
59 * @param string $key The key
60 *
61 * @return mixed|null
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 * Returns true if the JWK has the value identified by.
74 *
75 * @param string $key The key
76 */
77 public function has(string $key): bool
78 {
79 return array_key_exists($key, $this->values);
80 }
81
82 /**
83 * Get all values stored in the JWK object.
84 *
85 * @return array Values of the JWK object
86 */
87 public function all(): array
88 {
89 return $this->values;
90 }
91
92 /**
93 * Returns the thumbprint of the key.
94 *
95 * @see https://tools.ietf.org/html/rfc7638
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 * Returns the associated public key.
114 * This method has no effect for:
115 * - public keys
116 * - shared keys
117 * - unknown keys.
118 *
119 * Known keys are "oct", "RSA", "EC" and "OKP".
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 }