| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* The MIT License (MIT) |
| 7 |
* |
| 8 |
* Copyright (c) 2014-2020 Spomky-Labs |
| 9 |
* |
| 10 |
* This software may be modified and distributed under the terms |
| 11 |
* of the MIT license. See the LICENSE file for details. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Jose\Component\Core; |
| 15 |
|
| 16 |
use function array_key_exists; |
| 17 |
use Base64Url\Base64Url; |
| 18 |
use function in_array; |
| 19 |
use InvalidArgumentException; |
| 20 |
use function is_array; |
| 21 |
use JsonSerializable; |
| 22 |
|
| 23 |
class JWK implements JsonSerializable |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
private $values = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* Creates a JWK object using the given values. |
| 32 |
* The member "kty" is mandatory. Other members are NOT checked. |
| 33 |
* |
| 34 |
* @throws InvalidArgumentException if the key parameter "kty" is missing |
| 35 |
*/ |
| 36 |
public function __construct(array $values) |
| 37 |
{ |
| 38 |
if (!isset($values['kty'])) { |
| 39 |
throw new InvalidArgumentException('The parameter "kty" is mandatory.'); |
| 40 |
} |
| 41 |
$this->values = $values; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Creates a JWK object using the given Json string. |
| 46 |
* |
| 47 |
* @throws InvalidArgumentException if the data is not valid |
| 48 |
* |
| 49 |
* @return JWK |
| 50 |
*/ |
| 51 |
public static function createFromJson(string $json): self |
| 52 |
{ |
| 53 |
$data = json_decode($json, true); |
| 54 |
if (!is_array($data)) { |
| 55 |
throw new InvalidArgumentException('Invalid argument.'); |
| 56 |
} |
| 57 |
|
| 58 |
return new self($data); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the values to be serialized. |
| 63 |
*/ |
| 64 |
public function jsonSerialize(): array |
| 65 |
{ |
| 66 |
return $this->values; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the value with a specific key. |
| 71 |
* |
| 72 |
* @param string $key The key |
| 73 |
* |
| 74 |
* @throws InvalidArgumentException if the key does not exist |
| 75 |
* |
| 76 |
* @return null|mixed |
| 77 |
*/ |
| 78 |
public function get(string $key) |
| 79 |
{ |
| 80 |
if (!$this->has($key)) { |
| 81 |
throw new InvalidArgumentException(sprintf('The value identified by "%s" does not exist.', $key)); |
| 82 |
} |
| 83 |
|
| 84 |
return $this->values[$key]; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns true if the JWK has the value identified by. |
| 89 |
* |
| 90 |
* @param string $key The key |
| 91 |
*/ |
| 92 |
public function has(string $key): bool |
| 93 |
{ |
| 94 |
return array_key_exists($key, $this->values); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get all values stored in the JWK object. |
| 99 |
* |
| 100 |
* @return array Values of the JWK object |
| 101 |
*/ |
| 102 |
public function all(): array |
| 103 |
{ |
| 104 |
return $this->values; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns the thumbprint of the key. |
| 109 |
* |
| 110 |
* @see https://tools.ietf.org/html/rfc7638 |
| 111 |
* |
| 112 |
* @throws InvalidArgumentException if the hashing function is not supported |
| 113 |
*/ |
| 114 |
public function thumbprint(string $hash_algorithm): string |
| 115 |
{ |
| 116 |
if (!in_array($hash_algorithm, hash_algos(), true)) { |
| 117 |
throw new InvalidArgumentException(sprintf('The hash algorithm "%s" is not supported.', $hash_algorithm)); |
| 118 |
} |
| 119 |
$values = array_intersect_key($this->values, array_flip(['kty', 'n', 'e', 'crv', 'x', 'y', 'k'])); |
| 120 |
ksort($values); |
| 121 |
$input = json_encode($values, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
| 122 |
if (false === $input) { |
| 123 |
throw new InvalidArgumentException('Unable to compute the key thumbprint'); |
| 124 |
} |
| 125 |
|
| 126 |
return Base64Url::encode(hash($hash_algorithm, $input, true)); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns the associated public key. |
| 131 |
* This method has no effect for: |
| 132 |
* - public keys |
| 133 |
* - shared keys |
| 134 |
* - unknown keys. |
| 135 |
* |
| 136 |
* Known keys are "oct", "RSA", "EC" and "OKP". |
| 137 |
* |
| 138 |
* @return JWK |
| 139 |
*/ |
| 140 |
public function toPublic(): self |
| 141 |
{ |
| 142 |
$values = array_diff_key($this->values, array_flip(['p', 'd', 'q', 'dp', 'dq', 'qi'])); |
| 143 |
|
| 144 |
return new self($values); |
| 145 |
} |
| 146 |
} |
| 147 |
|