@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | "JWK Set must have a 'keys' member."); |
66 | 66 | } |
67 | 67 | $jwks = array_map( |
68 | - function ($jwkdata) { |
|
68 | + function($jwkdata) { |
|
69 | 69 | return JWK::fromArray($jwkdata); |
70 | 70 | }, $members["keys"]); |
71 | 71 | unset($members["keys"]); |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | { |
199 | 199 | $data = $this->_additional; |
200 | 200 | $data["keys"] = array_map( |
201 | - function (JWK $jwk) { |
|
201 | + function(JWK $jwk) { |
|
202 | 202 | return $jwk->toArray(); |
203 | 203 | }, $this->_jwks); |
204 | 204 | return $data; |
@@ -13,227 +13,227 @@ |
||
13 | 13 | */ |
14 | 14 | class JWKSet implements \Countable, \IteratorAggregate |
15 | 15 | { |
16 | - /** |
|
17 | - * JWK objects. |
|
18 | - * |
|
19 | - * @var JWK[] $_jwks |
|
20 | - */ |
|
21 | - protected $_jwks; |
|
22 | - |
|
23 | - /** |
|
24 | - * Additional members. |
|
25 | - * |
|
26 | - * @var array $_additional |
|
27 | - */ |
|
28 | - protected $_additional; |
|
29 | - |
|
30 | - /** |
|
31 | - * JWK mappings. |
|
32 | - * |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - private $_mappings = array(); |
|
36 | - |
|
37 | - /** |
|
38 | - * Constructor. |
|
39 | - * |
|
40 | - * @param JWK ...$jwks |
|
41 | - */ |
|
42 | - public function __construct(JWK ...$jwks) |
|
43 | - { |
|
44 | - $this->_jwks = $jwks; |
|
45 | - $this->_additional = array(); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Reset internal cache variables on clone. |
|
50 | - */ |
|
51 | - public function __clone() |
|
52 | - { |
|
53 | - $this->_mappings = array(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Initialize from an array representing a JSON object. |
|
58 | - * |
|
59 | - * @param array $members |
|
60 | - * @throws \UnexpectedValueException |
|
61 | - * @return self |
|
62 | - */ |
|
63 | - public static function fromArray(array $members): self |
|
64 | - { |
|
65 | - if (!isset($members["keys"]) || !is_array($members["keys"])) { |
|
66 | - throw new \UnexpectedValueException( |
|
67 | - "JWK Set must have a 'keys' member."); |
|
68 | - } |
|
69 | - $jwks = array_map( |
|
70 | - function ($jwkdata) { |
|
71 | - return JWK::fromArray($jwkdata); |
|
72 | - }, $members["keys"]); |
|
73 | - unset($members["keys"]); |
|
74 | - $obj = new self(...$jwks); |
|
75 | - $obj->_additional = $members; |
|
76 | - return $obj; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Initialize from a JSON string. |
|
81 | - * |
|
82 | - * @param string $json |
|
83 | - * @throws \UnexpectedValueException |
|
84 | - * @return self |
|
85 | - */ |
|
86 | - public static function fromJSON(string $json): self |
|
87 | - { |
|
88 | - $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
89 | - if (!is_array($members)) { |
|
90 | - throw new \UnexpectedValueException("Invalid JSON."); |
|
91 | - } |
|
92 | - return self::fromArray($members); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Get self with keys added. |
|
97 | - * |
|
98 | - * @param JWK ...$keys JWK objects |
|
99 | - * @return self |
|
100 | - */ |
|
101 | - public function withKeys(JWK ...$keys): self |
|
102 | - { |
|
103 | - $obj = clone $this; |
|
104 | - $obj->_jwks = array_merge($obj->_jwks, $keys); |
|
105 | - return $obj; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Get all JWK's in a set. |
|
110 | - * |
|
111 | - * @return JWK[] |
|
112 | - */ |
|
113 | - public function keys(): array |
|
114 | - { |
|
115 | - return $this->_jwks; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Get the first JWK in the set. |
|
120 | - * |
|
121 | - * @throws \LogicException |
|
122 | - * @return JWK |
|
123 | - */ |
|
124 | - public function first(): JWK |
|
125 | - { |
|
126 | - if (!count($this->_jwks)) { |
|
127 | - throw new \LogicException("No keys."); |
|
128 | - } |
|
129 | - return $this->_jwks[0]; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Get JWK by key ID. |
|
134 | - * |
|
135 | - * @param string $id |
|
136 | - * @return JWK|null Null if not found |
|
137 | - */ |
|
138 | - protected function _getKeyByID(string $id) |
|
139 | - { |
|
140 | - $map = $this->_getMapping(JWKParameter::PARAM_KEY_ID); |
|
141 | - return isset($map[$id]) ? $map[$id] : null; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Check whether set has a JWK with a given key ID. |
|
146 | - * |
|
147 | - * @param string $id |
|
148 | - * @return bool |
|
149 | - */ |
|
150 | - public function hasKeyID(string $id): bool |
|
151 | - { |
|
152 | - return $this->_getKeyByID($id) !== null; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Get a JWK by a key ID. |
|
157 | - * |
|
158 | - * @param string $id |
|
159 | - * @throws \LogicException |
|
160 | - * @return JWK |
|
161 | - */ |
|
162 | - public function keyByID(string $id): JWK |
|
163 | - { |
|
164 | - $jwk = $this->_getKeyByID($id); |
|
165 | - if (!$jwk) { |
|
166 | - throw new \LogicException("No key ID $id."); |
|
167 | - } |
|
168 | - return $jwk; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get mapping from parameter values of given parameter name to JWK. |
|
173 | - * |
|
174 | - * Later duplicate value shall override earlier JWK. |
|
175 | - * |
|
176 | - * @param string $name Parameter name |
|
177 | - * @return array |
|
178 | - */ |
|
179 | - protected function _getMapping(string $name): array |
|
180 | - { |
|
181 | - if (!isset($this->_mappings[$name])) { |
|
182 | - $mapping = array(); |
|
183 | - foreach ($this->_jwks as $jwk) { |
|
184 | - if ($jwk->has($name)) { |
|
185 | - $key = (string) $jwk->get($name)->value(); |
|
186 | - $mapping[$key] = $jwk; |
|
187 | - } |
|
188 | - } |
|
189 | - $this->_mappings[$name] = $mapping; |
|
190 | - } |
|
191 | - return $this->_mappings[$name]; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Convert to array. |
|
196 | - * |
|
197 | - * @return array |
|
198 | - */ |
|
199 | - public function toArray(): array |
|
200 | - { |
|
201 | - $data = $this->_additional; |
|
202 | - $data["keys"] = array_map( |
|
203 | - function (JWK $jwk) { |
|
204 | - return $jwk->toArray(); |
|
205 | - }, $this->_jwks); |
|
206 | - return $data; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Convert to JSON. |
|
211 | - * |
|
212 | - * @return string |
|
213 | - */ |
|
214 | - public function toJSON(): string |
|
215 | - { |
|
216 | - return json_encode((object) $this->toArray(), JSON_UNESCAPED_SLASHES); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Get the number of keys. |
|
221 | - * |
|
222 | - * @see \Countable::count() |
|
223 | - */ |
|
224 | - public function count(): int |
|
225 | - { |
|
226 | - return count($this->_jwks); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Get iterator for JWK objects. |
|
231 | - * |
|
232 | - * @see \IteratorAggregate::getIterator() |
|
233 | - * @return \ArrayIterator |
|
234 | - */ |
|
235 | - public function getIterator(): \ArrayIterator |
|
236 | - { |
|
237 | - return new \ArrayIterator($this->_jwks); |
|
238 | - } |
|
16 | + /** |
|
17 | + * JWK objects. |
|
18 | + * |
|
19 | + * @var JWK[] $_jwks |
|
20 | + */ |
|
21 | + protected $_jwks; |
|
22 | + |
|
23 | + /** |
|
24 | + * Additional members. |
|
25 | + * |
|
26 | + * @var array $_additional |
|
27 | + */ |
|
28 | + protected $_additional; |
|
29 | + |
|
30 | + /** |
|
31 | + * JWK mappings. |
|
32 | + * |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + private $_mappings = array(); |
|
36 | + |
|
37 | + /** |
|
38 | + * Constructor. |
|
39 | + * |
|
40 | + * @param JWK ...$jwks |
|
41 | + */ |
|
42 | + public function __construct(JWK ...$jwks) |
|
43 | + { |
|
44 | + $this->_jwks = $jwks; |
|
45 | + $this->_additional = array(); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Reset internal cache variables on clone. |
|
50 | + */ |
|
51 | + public function __clone() |
|
52 | + { |
|
53 | + $this->_mappings = array(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Initialize from an array representing a JSON object. |
|
58 | + * |
|
59 | + * @param array $members |
|
60 | + * @throws \UnexpectedValueException |
|
61 | + * @return self |
|
62 | + */ |
|
63 | + public static function fromArray(array $members): self |
|
64 | + { |
|
65 | + if (!isset($members["keys"]) || !is_array($members["keys"])) { |
|
66 | + throw new \UnexpectedValueException( |
|
67 | + "JWK Set must have a 'keys' member."); |
|
68 | + } |
|
69 | + $jwks = array_map( |
|
70 | + function ($jwkdata) { |
|
71 | + return JWK::fromArray($jwkdata); |
|
72 | + }, $members["keys"]); |
|
73 | + unset($members["keys"]); |
|
74 | + $obj = new self(...$jwks); |
|
75 | + $obj->_additional = $members; |
|
76 | + return $obj; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Initialize from a JSON string. |
|
81 | + * |
|
82 | + * @param string $json |
|
83 | + * @throws \UnexpectedValueException |
|
84 | + * @return self |
|
85 | + */ |
|
86 | + public static function fromJSON(string $json): self |
|
87 | + { |
|
88 | + $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
89 | + if (!is_array($members)) { |
|
90 | + throw new \UnexpectedValueException("Invalid JSON."); |
|
91 | + } |
|
92 | + return self::fromArray($members); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Get self with keys added. |
|
97 | + * |
|
98 | + * @param JWK ...$keys JWK objects |
|
99 | + * @return self |
|
100 | + */ |
|
101 | + public function withKeys(JWK ...$keys): self |
|
102 | + { |
|
103 | + $obj = clone $this; |
|
104 | + $obj->_jwks = array_merge($obj->_jwks, $keys); |
|
105 | + return $obj; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Get all JWK's in a set. |
|
110 | + * |
|
111 | + * @return JWK[] |
|
112 | + */ |
|
113 | + public function keys(): array |
|
114 | + { |
|
115 | + return $this->_jwks; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Get the first JWK in the set. |
|
120 | + * |
|
121 | + * @throws \LogicException |
|
122 | + * @return JWK |
|
123 | + */ |
|
124 | + public function first(): JWK |
|
125 | + { |
|
126 | + if (!count($this->_jwks)) { |
|
127 | + throw new \LogicException("No keys."); |
|
128 | + } |
|
129 | + return $this->_jwks[0]; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Get JWK by key ID. |
|
134 | + * |
|
135 | + * @param string $id |
|
136 | + * @return JWK|null Null if not found |
|
137 | + */ |
|
138 | + protected function _getKeyByID(string $id) |
|
139 | + { |
|
140 | + $map = $this->_getMapping(JWKParameter::PARAM_KEY_ID); |
|
141 | + return isset($map[$id]) ? $map[$id] : null; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Check whether set has a JWK with a given key ID. |
|
146 | + * |
|
147 | + * @param string $id |
|
148 | + * @return bool |
|
149 | + */ |
|
150 | + public function hasKeyID(string $id): bool |
|
151 | + { |
|
152 | + return $this->_getKeyByID($id) !== null; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Get a JWK by a key ID. |
|
157 | + * |
|
158 | + * @param string $id |
|
159 | + * @throws \LogicException |
|
160 | + * @return JWK |
|
161 | + */ |
|
162 | + public function keyByID(string $id): JWK |
|
163 | + { |
|
164 | + $jwk = $this->_getKeyByID($id); |
|
165 | + if (!$jwk) { |
|
166 | + throw new \LogicException("No key ID $id."); |
|
167 | + } |
|
168 | + return $jwk; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get mapping from parameter values of given parameter name to JWK. |
|
173 | + * |
|
174 | + * Later duplicate value shall override earlier JWK. |
|
175 | + * |
|
176 | + * @param string $name Parameter name |
|
177 | + * @return array |
|
178 | + */ |
|
179 | + protected function _getMapping(string $name): array |
|
180 | + { |
|
181 | + if (!isset($this->_mappings[$name])) { |
|
182 | + $mapping = array(); |
|
183 | + foreach ($this->_jwks as $jwk) { |
|
184 | + if ($jwk->has($name)) { |
|
185 | + $key = (string) $jwk->get($name)->value(); |
|
186 | + $mapping[$key] = $jwk; |
|
187 | + } |
|
188 | + } |
|
189 | + $this->_mappings[$name] = $mapping; |
|
190 | + } |
|
191 | + return $this->_mappings[$name]; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Convert to array. |
|
196 | + * |
|
197 | + * @return array |
|
198 | + */ |
|
199 | + public function toArray(): array |
|
200 | + { |
|
201 | + $data = $this->_additional; |
|
202 | + $data["keys"] = array_map( |
|
203 | + function (JWK $jwk) { |
|
204 | + return $jwk->toArray(); |
|
205 | + }, $this->_jwks); |
|
206 | + return $data; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Convert to JSON. |
|
211 | + * |
|
212 | + * @return string |
|
213 | + */ |
|
214 | + public function toJSON(): string |
|
215 | + { |
|
216 | + return json_encode((object) $this->toArray(), JSON_UNESCAPED_SLASHES); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Get the number of keys. |
|
221 | + * |
|
222 | + * @see \Countable::count() |
|
223 | + */ |
|
224 | + public function count(): int |
|
225 | + { |
|
226 | + return count($this->_jwks); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Get iterator for JWK objects. |
|
231 | + * |
|
232 | + * @see \IteratorAggregate::getIterator() |
|
233 | + * @return \ArrayIterator |
|
234 | + */ |
|
235 | + public function getIterator(): \ArrayIterator |
|
236 | + { |
|
237 | + return new \ArrayIterator($this->_jwks); |
|
238 | + } |
|
239 | 239 | } |
@@ -12,15 +12,15 @@ |
||
12 | 12 | */ |
13 | 13 | class OtherPrimesInfoParameter extends JWKParameter |
14 | 14 | { |
15 | - use ArrayParameterValue; |
|
15 | + use ArrayParameterValue; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Constructor. |
|
19 | - * |
|
20 | - * @param array[] ...$primes |
|
21 | - */ |
|
22 | - public function __construct(...$primes) |
|
23 | - { |
|
24 | - parent::__construct(self::PARAM_OTHER_PRIMES_INFO, $primes); |
|
25 | - } |
|
17 | + /** |
|
18 | + * Constructor. |
|
19 | + * |
|
20 | + * @param array[] ...$primes |
|
21 | + */ |
|
22 | + public function __construct(...$primes) |
|
23 | + { |
|
24 | + parent::__construct(self::PARAM_OTHER_PRIMES_INFO, $primes); |
|
25 | + } |
|
26 | 26 | } |
@@ -66,14 +66,14 @@ |
||
66 | 66 | { |
67 | 67 | $this->_parts = explode(".", $token); |
68 | 68 | switch (count($this->_parts)) { |
69 | - case 3: |
|
70 | - $this->_type = self::TYPE_JWS; |
|
71 | - break; |
|
72 | - case 5: |
|
73 | - $this->_type = self::TYPE_JWE; |
|
74 | - break; |
|
75 | - default: |
|
76 | - throw new \UnexpectedValueException("Not a JWT token."); |
|
69 | + case 3: |
|
70 | + $this->_type = self::TYPE_JWS; |
|
71 | + break; |
|
72 | + case 5: |
|
73 | + $this->_type = self::TYPE_JWE; |
|
74 | + break; |
|
75 | + default: |
|
76 | + throw new \UnexpectedValueException("Not a JWT token."); |
|
77 | 77 | } |
78 | 78 | } |
79 | 79 |
@@ -26,418 +26,418 @@ |
||
26 | 26 | */ |
27 | 27 | class JWT |
28 | 28 | { |
29 | - /** |
|
30 | - * Type identifier for the signed JWT. |
|
31 | - * |
|
32 | - * @internal |
|
33 | - * |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - const TYPE_JWS = 0; |
|
29 | + /** |
|
30 | + * Type identifier for the signed JWT. |
|
31 | + * |
|
32 | + * @internal |
|
33 | + * |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + const TYPE_JWS = 0; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Type identifier for the encrypted JWT. |
|
40 | - * |
|
41 | - * @internal |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - const TYPE_JWE = 1; |
|
38 | + /** |
|
39 | + * Type identifier for the encrypted JWT. |
|
40 | + * |
|
41 | + * @internal |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + const TYPE_JWE = 1; |
|
46 | 46 | |
47 | - /** |
|
48 | - * JWT parts. |
|
49 | - * |
|
50 | - * @var string[] $_parts |
|
51 | - */ |
|
52 | - protected $_parts; |
|
47 | + /** |
|
48 | + * JWT parts. |
|
49 | + * |
|
50 | + * @var string[] $_parts |
|
51 | + */ |
|
52 | + protected $_parts; |
|
53 | 53 | |
54 | - /** |
|
55 | - * JWT type. |
|
56 | - * |
|
57 | - * @var int $_type |
|
58 | - */ |
|
59 | - protected $_type; |
|
54 | + /** |
|
55 | + * JWT type. |
|
56 | + * |
|
57 | + * @var int $_type |
|
58 | + */ |
|
59 | + protected $_type; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param string $token JWT string |
|
65 | - * @throws \UnexpectedValueException |
|
66 | - */ |
|
67 | - public function __construct(string $token) |
|
68 | - { |
|
69 | - $this->_parts = explode(".", $token); |
|
70 | - switch (count($this->_parts)) { |
|
71 | - case 3: |
|
72 | - $this->_type = self::TYPE_JWS; |
|
73 | - break; |
|
74 | - case 5: |
|
75 | - $this->_type = self::TYPE_JWE; |
|
76 | - break; |
|
77 | - default: |
|
78 | - throw new \UnexpectedValueException("Not a JWT token."); |
|
79 | - } |
|
80 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param string $token JWT string |
|
65 | + * @throws \UnexpectedValueException |
|
66 | + */ |
|
67 | + public function __construct(string $token) |
|
68 | + { |
|
69 | + $this->_parts = explode(".", $token); |
|
70 | + switch (count($this->_parts)) { |
|
71 | + case 3: |
|
72 | + $this->_type = self::TYPE_JWS; |
|
73 | + break; |
|
74 | + case 5: |
|
75 | + $this->_type = self::TYPE_JWE; |
|
76 | + break; |
|
77 | + default: |
|
78 | + throw new \UnexpectedValueException("Not a JWT token."); |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Convert claims set to an unsecured JWT. |
|
84 | - * |
|
85 | - * Unsecured JWT is not signed nor encrypted neither integrity protected, |
|
86 | - * and should thus be handled with care! |
|
87 | - * |
|
88 | - * @link https://tools.ietf.org/html/rfc7519#section-6 |
|
89 | - * @param Claims $claims Claims set |
|
90 | - * @param Header|null $header Optional header |
|
91 | - * @throws \RuntimeException For generic errors |
|
92 | - * @return self |
|
93 | - */ |
|
94 | - public static function unsecuredFromClaims(Claims $claims, |
|
95 | - Header $header = null): self |
|
96 | - { |
|
97 | - return self::signedFromClaims($claims, new NoneAlgorithm(), $header); |
|
98 | - } |
|
82 | + /** |
|
83 | + * Convert claims set to an unsecured JWT. |
|
84 | + * |
|
85 | + * Unsecured JWT is not signed nor encrypted neither integrity protected, |
|
86 | + * and should thus be handled with care! |
|
87 | + * |
|
88 | + * @link https://tools.ietf.org/html/rfc7519#section-6 |
|
89 | + * @param Claims $claims Claims set |
|
90 | + * @param Header|null $header Optional header |
|
91 | + * @throws \RuntimeException For generic errors |
|
92 | + * @return self |
|
93 | + */ |
|
94 | + public static function unsecuredFromClaims(Claims $claims, |
|
95 | + Header $header = null): self |
|
96 | + { |
|
97 | + return self::signedFromClaims($claims, new NoneAlgorithm(), $header); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Convert claims set to a signed JWS token. |
|
102 | - * |
|
103 | - * @param Claims $claims Claims set |
|
104 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
105 | - * @param Header|null $header Optional header |
|
106 | - * @throws \RuntimeException For generic errors |
|
107 | - * @return self |
|
108 | - */ |
|
109 | - public static function signedFromClaims(Claims $claims, |
|
110 | - SignatureAlgorithm $algo, Header $header = null): self |
|
111 | - { |
|
112 | - $payload = $claims->toJSON(); |
|
113 | - $jws = JWS::sign($payload, $algo, $header); |
|
114 | - return new self($jws->toCompact()); |
|
115 | - } |
|
100 | + /** |
|
101 | + * Convert claims set to a signed JWS token. |
|
102 | + * |
|
103 | + * @param Claims $claims Claims set |
|
104 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
105 | + * @param Header|null $header Optional header |
|
106 | + * @throws \RuntimeException For generic errors |
|
107 | + * @return self |
|
108 | + */ |
|
109 | + public static function signedFromClaims(Claims $claims, |
|
110 | + SignatureAlgorithm $algo, Header $header = null): self |
|
111 | + { |
|
112 | + $payload = $claims->toJSON(); |
|
113 | + $jws = JWS::sign($payload, $algo, $header); |
|
114 | + return new self($jws->toCompact()); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Convert claims set to an encrypted JWE token. |
|
119 | - * |
|
120 | - * @param Claims $claims Claims set |
|
121 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
122 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
123 | - * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
124 | - * @param Header|null $header Optional header |
|
125 | - * @throws \RuntimeException For generic errors |
|
126 | - * @return self |
|
127 | - */ |
|
128 | - public static function encryptedFromClaims(Claims $claims, |
|
129 | - KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
130 | - CompressionAlgorithm $zip_algo = null, Header $header = null): self |
|
131 | - { |
|
132 | - $payload = $claims->toJSON(); |
|
133 | - $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
134 | - return new self($jwe->toCompact()); |
|
135 | - } |
|
117 | + /** |
|
118 | + * Convert claims set to an encrypted JWE token. |
|
119 | + * |
|
120 | + * @param Claims $claims Claims set |
|
121 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
122 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
123 | + * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
124 | + * @param Header|null $header Optional header |
|
125 | + * @throws \RuntimeException For generic errors |
|
126 | + * @return self |
|
127 | + */ |
|
128 | + public static function encryptedFromClaims(Claims $claims, |
|
129 | + KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
130 | + CompressionAlgorithm $zip_algo = null, Header $header = null): self |
|
131 | + { |
|
132 | + $payload = $claims->toJSON(); |
|
133 | + $jwe = JWE::encrypt($payload, $key_algo, $enc_algo, $zip_algo, $header); |
|
134 | + return new self($jwe->toCompact()); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Get claims from the JWT. |
|
139 | - * |
|
140 | - * Claims shall be validated according to given validation context. |
|
141 | - * Validation context must contain all the necessary keys for the signature |
|
142 | - * validation and/or content decryption. |
|
143 | - * |
|
144 | - * If validation context contains only one key, it shall be used explicitly. |
|
145 | - * If multiple keys are provided, they must contain a JWK ID parameter for |
|
146 | - * the key identification. |
|
147 | - * |
|
148 | - * @param ValidationContext $ctx |
|
149 | - * @throws ValidationException If signature is invalid, or decryption fails, |
|
150 | - * or claims validation fails. |
|
151 | - * @throws \RuntimeException For generic errors |
|
152 | - * @return Claims |
|
153 | - */ |
|
154 | - public function claims(ValidationContext $ctx): Claims |
|
155 | - { |
|
156 | - // check signature or decrypt depending on the JWT type. |
|
157 | - if ($this->isJWS()) { |
|
158 | - $payload = self::_validatedPayloadFromJWS($this->JWS(), $ctx); |
|
159 | - } else { |
|
160 | - $payload = self::_validatedPayloadFromJWE($this->JWE(), $ctx); |
|
161 | - } |
|
162 | - // if JWT contains a nested token |
|
163 | - if ($this->isNested()) { |
|
164 | - return $this->_claimsFromNestedPayload($payload, $ctx); |
|
165 | - } |
|
166 | - // decode claims and validate |
|
167 | - $claims = Claims::fromJSON($payload); |
|
168 | - $ctx->validate($claims); |
|
169 | - return $claims; |
|
170 | - } |
|
137 | + /** |
|
138 | + * Get claims from the JWT. |
|
139 | + * |
|
140 | + * Claims shall be validated according to given validation context. |
|
141 | + * Validation context must contain all the necessary keys for the signature |
|
142 | + * validation and/or content decryption. |
|
143 | + * |
|
144 | + * If validation context contains only one key, it shall be used explicitly. |
|
145 | + * If multiple keys are provided, they must contain a JWK ID parameter for |
|
146 | + * the key identification. |
|
147 | + * |
|
148 | + * @param ValidationContext $ctx |
|
149 | + * @throws ValidationException If signature is invalid, or decryption fails, |
|
150 | + * or claims validation fails. |
|
151 | + * @throws \RuntimeException For generic errors |
|
152 | + * @return Claims |
|
153 | + */ |
|
154 | + public function claims(ValidationContext $ctx): Claims |
|
155 | + { |
|
156 | + // check signature or decrypt depending on the JWT type. |
|
157 | + if ($this->isJWS()) { |
|
158 | + $payload = self::_validatedPayloadFromJWS($this->JWS(), $ctx); |
|
159 | + } else { |
|
160 | + $payload = self::_validatedPayloadFromJWE($this->JWE(), $ctx); |
|
161 | + } |
|
162 | + // if JWT contains a nested token |
|
163 | + if ($this->isNested()) { |
|
164 | + return $this->_claimsFromNestedPayload($payload, $ctx); |
|
165 | + } |
|
166 | + // decode claims and validate |
|
167 | + $claims = Claims::fromJSON($payload); |
|
168 | + $ctx->validate($claims); |
|
169 | + return $claims; |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Sign self producing a nested JWT. |
|
174 | - * |
|
175 | - * Note that if JWT is to be signed and encrypted, it should be done in |
|
176 | - * sign-then-encrypt order. Please refer to links for security information. |
|
177 | - * |
|
178 | - * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
179 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
180 | - * @param Header|null $header Optional header |
|
181 | - * @throws \RuntimeException For generic errors |
|
182 | - * @return self |
|
183 | - */ |
|
184 | - public function signNested(SignatureAlgorithm $algo, Header $header = null): self |
|
185 | - { |
|
186 | - if (!isset($header)) { |
|
187 | - $header = new Header(); |
|
188 | - } |
|
189 | - // add JWT content type parameter |
|
190 | - $header = $header->withParameters( |
|
191 | - new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
192 | - $jws = JWS::sign($this->token(), $algo, $header); |
|
193 | - return new self($jws->toCompact()); |
|
194 | - } |
|
172 | + /** |
|
173 | + * Sign self producing a nested JWT. |
|
174 | + * |
|
175 | + * Note that if JWT is to be signed and encrypted, it should be done in |
|
176 | + * sign-then-encrypt order. Please refer to links for security information. |
|
177 | + * |
|
178 | + * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
179 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
180 | + * @param Header|null $header Optional header |
|
181 | + * @throws \RuntimeException For generic errors |
|
182 | + * @return self |
|
183 | + */ |
|
184 | + public function signNested(SignatureAlgorithm $algo, Header $header = null): self |
|
185 | + { |
|
186 | + if (!isset($header)) { |
|
187 | + $header = new Header(); |
|
188 | + } |
|
189 | + // add JWT content type parameter |
|
190 | + $header = $header->withParameters( |
|
191 | + new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
192 | + $jws = JWS::sign($this->token(), $algo, $header); |
|
193 | + return new self($jws->toCompact()); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Encrypt self producing a nested JWT. |
|
198 | - * |
|
199 | - * This JWT should be a JWS, that is, the order of nesting should be |
|
200 | - * sign-then-encrypt. |
|
201 | - * |
|
202 | - * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
203 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
204 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
205 | - * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
206 | - * @param Header|null $header Optional header |
|
207 | - * @throws \RuntimeException For generic errors |
|
208 | - * @return self |
|
209 | - */ |
|
210 | - public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
211 | - ContentEncryptionAlgorithm $enc_algo, |
|
212 | - CompressionAlgorithm $zip_algo = null, Header $header = null): self |
|
213 | - { |
|
214 | - if (!isset($header)) { |
|
215 | - $header = new Header(); |
|
216 | - } |
|
217 | - // add JWT content type parameter |
|
218 | - $header = $header->withParameters( |
|
219 | - new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
220 | - $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
221 | - $header); |
|
222 | - return new self($jwe->toCompact()); |
|
223 | - } |
|
196 | + /** |
|
197 | + * Encrypt self producing a nested JWT. |
|
198 | + * |
|
199 | + * This JWT should be a JWS, that is, the order of nesting should be |
|
200 | + * sign-then-encrypt. |
|
201 | + * |
|
202 | + * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
|
203 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
204 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
205 | + * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
206 | + * @param Header|null $header Optional header |
|
207 | + * @throws \RuntimeException For generic errors |
|
208 | + * @return self |
|
209 | + */ |
|
210 | + public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
211 | + ContentEncryptionAlgorithm $enc_algo, |
|
212 | + CompressionAlgorithm $zip_algo = null, Header $header = null): self |
|
213 | + { |
|
214 | + if (!isset($header)) { |
|
215 | + $header = new Header(); |
|
216 | + } |
|
217 | + // add JWT content type parameter |
|
218 | + $header = $header->withParameters( |
|
219 | + new ContentTypeParameter(ContentTypeParameter::TYPE_JWT)); |
|
220 | + $jwe = JWE::encrypt($this->token(), $key_algo, $enc_algo, $zip_algo, |
|
221 | + $header); |
|
222 | + return new self($jwe->toCompact()); |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * Whether JWT is a JWS. |
|
227 | - * |
|
228 | - * @return bool |
|
229 | - */ |
|
230 | - public function isJWS(): bool |
|
231 | - { |
|
232 | - return $this->_type == self::TYPE_JWS; |
|
233 | - } |
|
225 | + /** |
|
226 | + * Whether JWT is a JWS. |
|
227 | + * |
|
228 | + * @return bool |
|
229 | + */ |
|
230 | + public function isJWS(): bool |
|
231 | + { |
|
232 | + return $this->_type == self::TYPE_JWS; |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * Get JWT as a JWS. |
|
237 | - * |
|
238 | - * @throws \LogicException |
|
239 | - * @return JWS |
|
240 | - */ |
|
241 | - public function JWS(): JWS |
|
242 | - { |
|
243 | - if (!$this->isJWS()) { |
|
244 | - throw new \LogicException("Not a JWS."); |
|
245 | - } |
|
246 | - return JWS::fromParts($this->_parts); |
|
247 | - } |
|
235 | + /** |
|
236 | + * Get JWT as a JWS. |
|
237 | + * |
|
238 | + * @throws \LogicException |
|
239 | + * @return JWS |
|
240 | + */ |
|
241 | + public function JWS(): JWS |
|
242 | + { |
|
243 | + if (!$this->isJWS()) { |
|
244 | + throw new \LogicException("Not a JWS."); |
|
245 | + } |
|
246 | + return JWS::fromParts($this->_parts); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * Whether JWT is a JWE. |
|
251 | - * |
|
252 | - * @return bool |
|
253 | - */ |
|
254 | - public function isJWE(): bool |
|
255 | - { |
|
256 | - return $this->_type == self::TYPE_JWE; |
|
257 | - } |
|
249 | + /** |
|
250 | + * Whether JWT is a JWE. |
|
251 | + * |
|
252 | + * @return bool |
|
253 | + */ |
|
254 | + public function isJWE(): bool |
|
255 | + { |
|
256 | + return $this->_type == self::TYPE_JWE; |
|
257 | + } |
|
258 | 258 | |
259 | - /** |
|
260 | - * Get JWT as a JWE. |
|
261 | - * |
|
262 | - * @throws \LogicException |
|
263 | - * @return JWE |
|
264 | - */ |
|
265 | - public function JWE(): JWE |
|
266 | - { |
|
267 | - if (!$this->isJWE()) { |
|
268 | - throw new \LogicException("Not a JWE."); |
|
269 | - } |
|
270 | - return JWE::fromParts($this->_parts); |
|
271 | - } |
|
259 | + /** |
|
260 | + * Get JWT as a JWE. |
|
261 | + * |
|
262 | + * @throws \LogicException |
|
263 | + * @return JWE |
|
264 | + */ |
|
265 | + public function JWE(): JWE |
|
266 | + { |
|
267 | + if (!$this->isJWE()) { |
|
268 | + throw new \LogicException("Not a JWE."); |
|
269 | + } |
|
270 | + return JWE::fromParts($this->_parts); |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Check whether JWT contains another nested JWT. |
|
275 | - * |
|
276 | - * @return bool |
|
277 | - */ |
|
278 | - public function isNested(): bool |
|
279 | - { |
|
280 | - $header = $this->header(); |
|
281 | - if (!$header->hasContentType()) { |
|
282 | - return false; |
|
283 | - } |
|
284 | - $cty = $header->contentType()->value(); |
|
285 | - if ($cty != ContentTypeParameter::TYPE_JWT) { |
|
286 | - return false; |
|
287 | - } |
|
288 | - return true; |
|
289 | - } |
|
273 | + /** |
|
274 | + * Check whether JWT contains another nested JWT. |
|
275 | + * |
|
276 | + * @return bool |
|
277 | + */ |
|
278 | + public function isNested(): bool |
|
279 | + { |
|
280 | + $header = $this->header(); |
|
281 | + if (!$header->hasContentType()) { |
|
282 | + return false; |
|
283 | + } |
|
284 | + $cty = $header->contentType()->value(); |
|
285 | + if ($cty != ContentTypeParameter::TYPE_JWT) { |
|
286 | + return false; |
|
287 | + } |
|
288 | + return true; |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * Check whether JWT is unsecured, that is, it's neither integrity protected |
|
293 | - * nor encrypted. |
|
294 | - * |
|
295 | - * @return bool |
|
296 | - */ |
|
297 | - public function isUnsecured(): bool |
|
298 | - { |
|
299 | - // encrypted JWT shall be considered secure |
|
300 | - if ($this->isJWE()) { |
|
301 | - return false; |
|
302 | - } |
|
303 | - // check whether JWS is unsecured |
|
304 | - return $this->JWS()->isUnsecured(); |
|
305 | - } |
|
291 | + /** |
|
292 | + * Check whether JWT is unsecured, that is, it's neither integrity protected |
|
293 | + * nor encrypted. |
|
294 | + * |
|
295 | + * @return bool |
|
296 | + */ |
|
297 | + public function isUnsecured(): bool |
|
298 | + { |
|
299 | + // encrypted JWT shall be considered secure |
|
300 | + if ($this->isJWE()) { |
|
301 | + return false; |
|
302 | + } |
|
303 | + // check whether JWS is unsecured |
|
304 | + return $this->JWS()->isUnsecured(); |
|
305 | + } |
|
306 | 306 | |
307 | - /** |
|
308 | - * Get JWT header. |
|
309 | - * |
|
310 | - * @return JOSE |
|
311 | - */ |
|
312 | - public function header(): JOSE |
|
313 | - { |
|
314 | - $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
315 | - return new JOSE($header); |
|
316 | - } |
|
307 | + /** |
|
308 | + * Get JWT header. |
|
309 | + * |
|
310 | + * @return JOSE |
|
311 | + */ |
|
312 | + public function header(): JOSE |
|
313 | + { |
|
314 | + $header = Header::fromJSON(Base64::urlDecode($this->_parts[0])); |
|
315 | + return new JOSE($header); |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * Get JWT as a string. |
|
320 | - * |
|
321 | - * @return string |
|
322 | - */ |
|
323 | - public function token(): string |
|
324 | - { |
|
325 | - return implode(".", $this->_parts); |
|
326 | - } |
|
318 | + /** |
|
319 | + * Get JWT as a string. |
|
320 | + * |
|
321 | + * @return string |
|
322 | + */ |
|
323 | + public function token(): string |
|
324 | + { |
|
325 | + return implode(".", $this->_parts); |
|
326 | + } |
|
327 | 327 | |
328 | - /** |
|
329 | - * Get claims from a nested payload. |
|
330 | - * |
|
331 | - * @param string $payload JWT payload |
|
332 | - * @param ValidationContext $ctx Validation context |
|
333 | - * @return Claims |
|
334 | - */ |
|
335 | - private function _claimsFromNestedPayload(string $payload, |
|
336 | - ValidationContext $ctx): Claims |
|
337 | - { |
|
338 | - $jwt = new JWT($payload); |
|
339 | - // if this token secured, allow nested tokens to be unsecured. |
|
340 | - if (!$this->isUnsecured()) { |
|
341 | - $ctx = $ctx->withUnsecuredAllowed(true); |
|
342 | - } |
|
343 | - return $jwt->claims($ctx); |
|
344 | - } |
|
328 | + /** |
|
329 | + * Get claims from a nested payload. |
|
330 | + * |
|
331 | + * @param string $payload JWT payload |
|
332 | + * @param ValidationContext $ctx Validation context |
|
333 | + * @return Claims |
|
334 | + */ |
|
335 | + private function _claimsFromNestedPayload(string $payload, |
|
336 | + ValidationContext $ctx): Claims |
|
337 | + { |
|
338 | + $jwt = new JWT($payload); |
|
339 | + // if this token secured, allow nested tokens to be unsecured. |
|
340 | + if (!$this->isUnsecured()) { |
|
341 | + $ctx = $ctx->withUnsecuredAllowed(true); |
|
342 | + } |
|
343 | + return $jwt->claims($ctx); |
|
344 | + } |
|
345 | 345 | |
346 | - /** |
|
347 | - * Get validated payload from JWS. |
|
348 | - * |
|
349 | - * @param JWS $jws JWS |
|
350 | - * @param ValidationContext $ctx Validation context |
|
351 | - * @throws ValidationException If signature validation fails |
|
352 | - * @return string |
|
353 | - */ |
|
354 | - private static function _validatedPayloadFromJWS(JWS $jws, |
|
355 | - ValidationContext $ctx): string |
|
356 | - { |
|
357 | - // if JWS is unsecured |
|
358 | - if ($jws->isUnsecured()) { |
|
359 | - return self::_validatedPayloadFromUnsecuredJWS($jws, $ctx); |
|
360 | - } |
|
361 | - return self::_validatedPayloadFromSignedJWS($jws, $ctx->keys()); |
|
362 | - } |
|
346 | + /** |
|
347 | + * Get validated payload from JWS. |
|
348 | + * |
|
349 | + * @param JWS $jws JWS |
|
350 | + * @param ValidationContext $ctx Validation context |
|
351 | + * @throws ValidationException If signature validation fails |
|
352 | + * @return string |
|
353 | + */ |
|
354 | + private static function _validatedPayloadFromJWS(JWS $jws, |
|
355 | + ValidationContext $ctx): string |
|
356 | + { |
|
357 | + // if JWS is unsecured |
|
358 | + if ($jws->isUnsecured()) { |
|
359 | + return self::_validatedPayloadFromUnsecuredJWS($jws, $ctx); |
|
360 | + } |
|
361 | + return self::_validatedPayloadFromSignedJWS($jws, $ctx->keys()); |
|
362 | + } |
|
363 | 363 | |
364 | - /** |
|
365 | - * Get validated payload from an unsecured JWS. |
|
366 | - * |
|
367 | - * @param JWS $jws JWS |
|
368 | - * @param ValidationContext $ctx Validation context |
|
369 | - * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
|
370 | - * token is malformed |
|
371 | - * @return string |
|
372 | - */ |
|
373 | - private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
374 | - ValidationContext $ctx): string |
|
375 | - { |
|
376 | - if (!$ctx->isUnsecuredAllowed()) { |
|
377 | - throw new ValidationException("Unsecured JWS not allowed."); |
|
378 | - } |
|
379 | - if (!$jws->validate(new NoneAlgorithm())) { |
|
380 | - throw new ValidationException("Malformed unsecured token."); |
|
381 | - } |
|
382 | - return $jws->payload(); |
|
383 | - } |
|
364 | + /** |
|
365 | + * Get validated payload from an unsecured JWS. |
|
366 | + * |
|
367 | + * @param JWS $jws JWS |
|
368 | + * @param ValidationContext $ctx Validation context |
|
369 | + * @throws ValidationException If unsecured JWT's are not allowed, or JWS |
|
370 | + * token is malformed |
|
371 | + * @return string |
|
372 | + */ |
|
373 | + private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
374 | + ValidationContext $ctx): string |
|
375 | + { |
|
376 | + if (!$ctx->isUnsecuredAllowed()) { |
|
377 | + throw new ValidationException("Unsecured JWS not allowed."); |
|
378 | + } |
|
379 | + if (!$jws->validate(new NoneAlgorithm())) { |
|
380 | + throw new ValidationException("Malformed unsecured token."); |
|
381 | + } |
|
382 | + return $jws->payload(); |
|
383 | + } |
|
384 | 384 | |
385 | - /** |
|
386 | - * Get validated payload from a signed JWS. |
|
387 | - * |
|
388 | - * @param JWS $jws JWS |
|
389 | - * @param JWKSet $keys Set of allowed keys for the signature validation |
|
390 | - * @throws ValidationException If validation fails |
|
391 | - * @return string |
|
392 | - */ |
|
393 | - private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys): string |
|
394 | - { |
|
395 | - try { |
|
396 | - // explicitly defined key |
|
397 | - if (1 == count($keys)) { |
|
398 | - $valid = $jws->validateWithJWK($keys->first()); |
|
399 | - } else { |
|
400 | - $valid = $jws->validateWithJWKSet($keys); |
|
401 | - } |
|
402 | - } catch (\RuntimeException $e) { |
|
403 | - throw new ValidationException("JWS validation failed.", 0, $e); |
|
404 | - } |
|
405 | - if (!$valid) { |
|
406 | - throw new ValidationException("JWS signature is invalid."); |
|
407 | - } |
|
408 | - return $jws->payload(); |
|
409 | - } |
|
385 | + /** |
|
386 | + * Get validated payload from a signed JWS. |
|
387 | + * |
|
388 | + * @param JWS $jws JWS |
|
389 | + * @param JWKSet $keys Set of allowed keys for the signature validation |
|
390 | + * @throws ValidationException If validation fails |
|
391 | + * @return string |
|
392 | + */ |
|
393 | + private static function _validatedPayloadFromSignedJWS(JWS $jws, JWKSet $keys): string |
|
394 | + { |
|
395 | + try { |
|
396 | + // explicitly defined key |
|
397 | + if (1 == count($keys)) { |
|
398 | + $valid = $jws->validateWithJWK($keys->first()); |
|
399 | + } else { |
|
400 | + $valid = $jws->validateWithJWKSet($keys); |
|
401 | + } |
|
402 | + } catch (\RuntimeException $e) { |
|
403 | + throw new ValidationException("JWS validation failed.", 0, $e); |
|
404 | + } |
|
405 | + if (!$valid) { |
|
406 | + throw new ValidationException("JWS signature is invalid."); |
|
407 | + } |
|
408 | + return $jws->payload(); |
|
409 | + } |
|
410 | 410 | |
411 | - /** |
|
412 | - * Get validated payload from an encrypted JWE. |
|
413 | - * |
|
414 | - * @param JWE $jwe JWE |
|
415 | - * @param ValidationContext $ctx Validation context |
|
416 | - * @throws ValidationException If decryption fails |
|
417 | - * @return string |
|
418 | - */ |
|
419 | - private static function _validatedPayloadFromJWE(JWE $jwe, |
|
420 | - ValidationContext $ctx): string |
|
421 | - { |
|
422 | - try { |
|
423 | - $keys = $ctx->keys(); |
|
424 | - // explicitly defined key |
|
425 | - if (1 == count($keys)) { |
|
426 | - return $jwe->decryptWithJWK($keys->first()); |
|
427 | - } |
|
428 | - return $jwe->decryptWithJWKSet($keys); |
|
429 | - } catch (\RuntimeException $e) { |
|
430 | - throw new ValidationException("JWE validation failed.", 0, $e); |
|
431 | - } |
|
432 | - } |
|
411 | + /** |
|
412 | + * Get validated payload from an encrypted JWE. |
|
413 | + * |
|
414 | + * @param JWE $jwe JWE |
|
415 | + * @param ValidationContext $ctx Validation context |
|
416 | + * @throws ValidationException If decryption fails |
|
417 | + * @return string |
|
418 | + */ |
|
419 | + private static function _validatedPayloadFromJWE(JWE $jwe, |
|
420 | + ValidationContext $ctx): string |
|
421 | + { |
|
422 | + try { |
|
423 | + $keys = $ctx->keys(); |
|
424 | + // explicitly defined key |
|
425 | + if (1 == count($keys)) { |
|
426 | + return $jwe->decryptWithJWK($keys->first()); |
|
427 | + } |
|
428 | + return $jwe->decryptWithJWKSet($keys); |
|
429 | + } catch (\RuntimeException $e) { |
|
430 | + throw new ValidationException("JWE validation failed.", 0, $e); |
|
431 | + } |
|
432 | + } |
|
433 | 433 | |
434 | - /** |
|
435 | - * Convert JWT to string. |
|
436 | - * |
|
437 | - * @return string |
|
438 | - */ |
|
439 | - public function __toString() |
|
440 | - { |
|
441 | - return $this->token(); |
|
442 | - } |
|
434 | + /** |
|
435 | + * Convert JWT to string. |
|
436 | + * |
|
437 | + * @return string |
|
438 | + */ |
|
439 | + public function __toString() |
|
440 | + { |
|
441 | + return $this->token(); |
|
442 | + } |
|
443 | 443 | } |
@@ -13,18 +13,18 @@ |
||
13 | 13 | */ |
14 | 14 | class ExpirationTimeClaim extends RegisteredClaim |
15 | 15 | { |
16 | - use NumericDateClaim; |
|
17 | - use ReferenceTimeValidation; |
|
16 | + use NumericDateClaim; |
|
17 | + use ReferenceTimeValidation; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param int $exp_time Expiration time |
|
23 | - */ |
|
24 | - public function __construct($exp_time) |
|
25 | - { |
|
26 | - // validate that claim is after the constraint (reference time) |
|
27 | - parent::__construct(self::NAME_EXPIRATION_TIME, intval($exp_time), |
|
28 | - new GreaterValidator()); |
|
29 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param int $exp_time Expiration time |
|
23 | + */ |
|
24 | + public function __construct($exp_time) |
|
25 | + { |
|
26 | + // validate that claim is after the constraint (reference time) |
|
27 | + parent::__construct(self::NAME_EXPIRATION_TIME, intval($exp_time), |
|
28 | + new GreaterValidator()); |
|
29 | + } |
|
30 | 30 | } |
@@ -9,85 +9,85 @@ |
||
9 | 9 | */ |
10 | 10 | abstract class RegisteredClaim extends Claim |
11 | 11 | { |
12 | - // JWT claims |
|
13 | - const NAME_ISSUER = "iss"; |
|
14 | - const NAME_SUBJECT = "sub"; |
|
15 | - const NAME_AUDIENCE = "aud"; |
|
16 | - const NAME_EXPIRATION_TIME = "exp"; |
|
17 | - const NAME_NOT_BEFORE = "nbf"; |
|
18 | - const NAME_ISSUED_AT = "iat"; |
|
19 | - const NAME_JWT_ID = "jti"; |
|
12 | + // JWT claims |
|
13 | + const NAME_ISSUER = "iss"; |
|
14 | + const NAME_SUBJECT = "sub"; |
|
15 | + const NAME_AUDIENCE = "aud"; |
|
16 | + const NAME_EXPIRATION_TIME = "exp"; |
|
17 | + const NAME_NOT_BEFORE = "nbf"; |
|
18 | + const NAME_ISSUED_AT = "iat"; |
|
19 | + const NAME_JWT_ID = "jti"; |
|
20 | 20 | |
21 | - // OpenID claims |
|
22 | - const NAME_FULL_NAME = "name"; |
|
23 | - const NAME_GIVEN_NAME = "given_name"; |
|
24 | - const NAME_FAMILY_NAME = "family_name"; |
|
25 | - const NAME_MIDDLE_NAME = "middle_name"; |
|
26 | - const NAME_NICKNAME = "nickname"; |
|
27 | - const NAME_PREFERRED_USERNAME = "preferred_username"; |
|
28 | - const NAME_PROFILE_URL = "profile"; |
|
29 | - const NAME_PICTURE_URL = "picture"; |
|
30 | - const NAME_WEBSITE_URL = "website"; |
|
31 | - const NAME_EMAIL = "email"; |
|
32 | - const NAME_EMAIL_VERIFIED = "email_verified"; |
|
33 | - const NAME_GENDER = "gender"; |
|
34 | - const NAME_BIRTHDATE = "birthdate"; |
|
35 | - const NAME_TIMEZONE = "zoneinfo"; |
|
36 | - const NAME_LOCALE = "locale"; |
|
37 | - const NAME_PHONE_NUMBER = "phone_number"; |
|
38 | - const NAME_PHONE_NUMBER_VERIFIED = "phone_number_verified"; |
|
39 | - const NAME_ADDRESS = "address"; |
|
40 | - const NAME_UPDATED_AT = "updated_at"; |
|
41 | - const NAME_AUTHORIZED_PARTY = "azp"; |
|
42 | - const NAME_NONCE = "nonce"; |
|
43 | - const NAME_AUTH_TIME = "auth_time"; |
|
44 | - const NAME_ACCESS_TOKEN_HASH = "at_hash"; |
|
45 | - const NAME_CODE_HASH = "c_hash"; |
|
46 | - const NAME_ACR = "acr"; |
|
47 | - const NAME_AMR = "amr"; |
|
48 | - const NAME_SUB_JWK = "sub_jwk"; |
|
21 | + // OpenID claims |
|
22 | + const NAME_FULL_NAME = "name"; |
|
23 | + const NAME_GIVEN_NAME = "given_name"; |
|
24 | + const NAME_FAMILY_NAME = "family_name"; |
|
25 | + const NAME_MIDDLE_NAME = "middle_name"; |
|
26 | + const NAME_NICKNAME = "nickname"; |
|
27 | + const NAME_PREFERRED_USERNAME = "preferred_username"; |
|
28 | + const NAME_PROFILE_URL = "profile"; |
|
29 | + const NAME_PICTURE_URL = "picture"; |
|
30 | + const NAME_WEBSITE_URL = "website"; |
|
31 | + const NAME_EMAIL = "email"; |
|
32 | + const NAME_EMAIL_VERIFIED = "email_verified"; |
|
33 | + const NAME_GENDER = "gender"; |
|
34 | + const NAME_BIRTHDATE = "birthdate"; |
|
35 | + const NAME_TIMEZONE = "zoneinfo"; |
|
36 | + const NAME_LOCALE = "locale"; |
|
37 | + const NAME_PHONE_NUMBER = "phone_number"; |
|
38 | + const NAME_PHONE_NUMBER_VERIFIED = "phone_number_verified"; |
|
39 | + const NAME_ADDRESS = "address"; |
|
40 | + const NAME_UPDATED_AT = "updated_at"; |
|
41 | + const NAME_AUTHORIZED_PARTY = "azp"; |
|
42 | + const NAME_NONCE = "nonce"; |
|
43 | + const NAME_AUTH_TIME = "auth_time"; |
|
44 | + const NAME_ACCESS_TOKEN_HASH = "at_hash"; |
|
45 | + const NAME_CODE_HASH = "c_hash"; |
|
46 | + const NAME_ACR = "acr"; |
|
47 | + const NAME_AMR = "amr"; |
|
48 | + const NAME_SUB_JWK = "sub_jwk"; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Mapping from registered claim name to class name. |
|
52 | - * |
|
53 | - * @internal |
|
54 | - * |
|
55 | - * @var array |
|
56 | - */ |
|
57 | - const MAP_NAME_TO_CLASS = array( |
|
58 | - /* @formatter:off */ |
|
59 | - self::NAME_ISSUER => IssuerClaim::class, |
|
60 | - self::NAME_SUBJECT => SubjectClaim::class, |
|
61 | - self::NAME_AUDIENCE => AudienceClaim::class, |
|
62 | - self::NAME_EXPIRATION_TIME => ExpirationTimeClaim::class, |
|
63 | - self::NAME_NOT_BEFORE => NotBeforeClaim::class, |
|
64 | - self::NAME_ISSUED_AT => IssuedAtClaim::class, |
|
65 | - self::NAME_JWT_ID => JWTIDClaim::class |
|
66 | - /* @formatter:on */ |
|
67 | - ); |
|
50 | + /** |
|
51 | + * Mapping from registered claim name to class name. |
|
52 | + * |
|
53 | + * @internal |
|
54 | + * |
|
55 | + * @var array |
|
56 | + */ |
|
57 | + const MAP_NAME_TO_CLASS = array( |
|
58 | + /* @formatter:off */ |
|
59 | + self::NAME_ISSUER => IssuerClaim::class, |
|
60 | + self::NAME_SUBJECT => SubjectClaim::class, |
|
61 | + self::NAME_AUDIENCE => AudienceClaim::class, |
|
62 | + self::NAME_EXPIRATION_TIME => ExpirationTimeClaim::class, |
|
63 | + self::NAME_NOT_BEFORE => NotBeforeClaim::class, |
|
64 | + self::NAME_ISSUED_AT => IssuedAtClaim::class, |
|
65 | + self::NAME_JWT_ID => JWTIDClaim::class |
|
66 | + /* @formatter:on */ |
|
67 | + ); |
|
68 | 68 | |
69 | - /** |
|
70 | - * Constructor. |
|
71 | - * |
|
72 | - * Defined here for type strictness. Parameters are passed to the |
|
73 | - * superclass. |
|
74 | - * |
|
75 | - * @param mixed ...$args |
|
76 | - */ |
|
77 | - public function __construct(...$args) |
|
78 | - { |
|
79 | - parent::__construct((string) $args[0], $args[1], |
|
80 | - isset($args[2]) ? $args[2] : null); |
|
81 | - } |
|
69 | + /** |
|
70 | + * Constructor. |
|
71 | + * |
|
72 | + * Defined here for type strictness. Parameters are passed to the |
|
73 | + * superclass. |
|
74 | + * |
|
75 | + * @param mixed ...$args |
|
76 | + */ |
|
77 | + public function __construct(...$args) |
|
78 | + { |
|
79 | + parent::__construct((string) $args[0], $args[1], |
|
80 | + isset($args[2]) ? $args[2] : null); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Initialize concrete claim instance from a JSON value. |
|
85 | - * |
|
86 | - * @param mixed $value |
|
87 | - * @return RegisteredClaim |
|
88 | - */ |
|
89 | - public static function fromJSONValue($value) |
|
90 | - { |
|
91 | - return new static($value); |
|
92 | - } |
|
83 | + /** |
|
84 | + * Initialize concrete claim instance from a JSON value. |
|
85 | + * |
|
86 | + * @param mixed $value |
|
87 | + * @return RegisteredClaim |
|
88 | + */ |
|
89 | + public static function fromJSONValue($value) |
|
90 | + { |
|
91 | + return new static($value); |
|
92 | + } |
|
93 | 93 | } |
@@ -11,13 +11,13 @@ |
||
11 | 11 | */ |
12 | 12 | class ReplicatedClaimParameter extends JWTParameter |
13 | 13 | { |
14 | - /** |
|
15 | - * Constructor. |
|
16 | - * |
|
17 | - * @param Claim $claim |
|
18 | - */ |
|
19 | - public function __construct(Claim $claim) |
|
20 | - { |
|
21 | - parent::__construct($claim->name(), $claim->value()); |
|
22 | - } |
|
14 | + /** |
|
15 | + * Constructor. |
|
16 | + * |
|
17 | + * @param Claim $claim |
|
18 | + */ |
|
19 | + public function __construct(Claim $claim) |
|
20 | + { |
|
21 | + parent::__construct($claim->name(), $claim->value()); |
|
22 | + } |
|
23 | 23 | } |
@@ -70,9 +70,9 @@ |
||
70 | 70 | // time_mid |
71 | 71 | mt_rand(0, 0xffff), |
72 | 72 | // time_hi_and_version |
73 | - mt_rand(0, 0x0fff) | 0x4000, |
|
73 | + mt_rand(0, 0x0fff)|0x4000, |
|
74 | 74 | // clk_seq_hi_res |
75 | - mt_rand(0, 0x3f) | 0x80, |
|
75 | + mt_rand(0, 0x3f)|0x80, |
|
76 | 76 | // clk_seq_low |
77 | 77 | mt_rand(0, 0xff), |
78 | 78 | // node |
@@ -30,32 +30,32 @@ discard block |
||
30 | 30 | */ |
31 | 31 | class UUIDv4 |
32 | 32 | { |
33 | - /** |
|
34 | - * UUID. |
|
35 | - * |
|
36 | - * @var string $_uuid |
|
37 | - */ |
|
38 | - protected $_uuid; |
|
33 | + /** |
|
34 | + * UUID. |
|
35 | + * |
|
36 | + * @var string $_uuid |
|
37 | + */ |
|
38 | + protected $_uuid; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Constructor. |
|
42 | - * |
|
43 | - * @param string $uuid UUIDv4 in canonical hexadecimal format |
|
44 | - */ |
|
45 | - public function __construct(string $uuid) |
|
46 | - { |
|
47 | - // @todo Check that UUID is version 4 |
|
48 | - $this->_uuid = $uuid; |
|
49 | - } |
|
40 | + /** |
|
41 | + * Constructor. |
|
42 | + * |
|
43 | + * @param string $uuid UUIDv4 in canonical hexadecimal format |
|
44 | + */ |
|
45 | + public function __construct(string $uuid) |
|
46 | + { |
|
47 | + // @todo Check that UUID is version 4 |
|
48 | + $this->_uuid = $uuid; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Create new random UUIDv4. |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public static function createRandom(): self |
|
57 | - { |
|
58 | - /* |
|
51 | + /** |
|
52 | + * Create new random UUIDv4. |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public static function createRandom(): self |
|
57 | + { |
|
58 | + /* |
|
59 | 59 | 1. Set the two most significant bits (bits 6 and 7) of |
60 | 60 | the clock_seq_hi_and_reserved to zero and one, respectively. |
61 | 61 | |
@@ -66,38 +66,38 @@ discard block |
||
66 | 66 | 3. Set all the other bits to randomly (or pseudo-randomly) |
67 | 67 | chosen values. |
68 | 68 | */ |
69 | - $uuid = sprintf("%04x%04x-%04x-%04x-%02x%02x-%04x%04x%04x", |
|
70 | - // time_low |
|
71 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
72 | - // time_mid |
|
73 | - mt_rand(0, 0xffff), |
|
74 | - // time_hi_and_version |
|
75 | - mt_rand(0, 0x0fff) | 0x4000, |
|
76 | - // clk_seq_hi_res |
|
77 | - mt_rand(0, 0x3f) | 0x80, |
|
78 | - // clk_seq_low |
|
79 | - mt_rand(0, 0xff), |
|
80 | - // node |
|
81 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
82 | - return new self($uuid); |
|
83 | - } |
|
69 | + $uuid = sprintf("%04x%04x-%04x-%04x-%02x%02x-%04x%04x%04x", |
|
70 | + // time_low |
|
71 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
72 | + // time_mid |
|
73 | + mt_rand(0, 0xffff), |
|
74 | + // time_hi_and_version |
|
75 | + mt_rand(0, 0x0fff) | 0x4000, |
|
76 | + // clk_seq_hi_res |
|
77 | + mt_rand(0, 0x3f) | 0x80, |
|
78 | + // clk_seq_low |
|
79 | + mt_rand(0, 0xff), |
|
80 | + // node |
|
81 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
82 | + return new self($uuid); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Get UUIDv4 in canonical form. |
|
87 | - * |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function canonical(): string |
|
91 | - { |
|
92 | - return $this->_uuid; |
|
93 | - } |
|
85 | + /** |
|
86 | + * Get UUIDv4 in canonical form. |
|
87 | + * |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function canonical(): string |
|
91 | + { |
|
92 | + return $this->_uuid; |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * |
|
97 | - * @return string |
|
98 | - */ |
|
99 | - public function __toString() |
|
100 | - { |
|
101 | - return $this->canonical(); |
|
102 | - } |
|
95 | + /** |
|
96 | + * |
|
97 | + * @return string |
|
98 | + */ |
|
99 | + public function __toString() |
|
100 | + { |
|
101 | + return $this->canonical(); |
|
102 | + } |
|
103 | 103 | } |
@@ -31,17 +31,17 @@ |
||
31 | 31 | { |
32 | 32 | $data = strtr($data, "-_", "+/"); |
33 | 33 | switch (strlen($data) % 4) { |
34 | - case 0: |
|
35 | - break; |
|
36 | - case 2: |
|
37 | - $data .= "=="; |
|
38 | - break; |
|
39 | - case 3: |
|
40 | - $data .= "="; |
|
41 | - break; |
|
42 | - default: |
|
43 | - throw new \UnexpectedValueException( |
|
44 | - "Malformed base64url encoding."); |
|
34 | + case 0: |
|
35 | + break; |
|
36 | + case 2: |
|
37 | + $data .= "=="; |
|
38 | + break; |
|
39 | + case 3: |
|
40 | + $data .= "="; |
|
41 | + break; |
|
42 | + default: |
|
43 | + throw new \UnexpectedValueException( |
|
44 | + "Malformed base64url encoding."); |
|
45 | 45 | } |
46 | 46 | return self::decode($data); |
47 | 47 | } |
@@ -9,104 +9,104 @@ |
||
9 | 9 | */ |
10 | 10 | class Base64 |
11 | 11 | { |
12 | - /** |
|
13 | - * Encode a string using base64url variant. |
|
14 | - * |
|
15 | - * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
16 | - * @param string $data |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - public static function urlEncode(string $data): string |
|
20 | - { |
|
21 | - return strtr(rtrim(self::encode($data), "="), "+/", "-_"); |
|
22 | - } |
|
12 | + /** |
|
13 | + * Encode a string using base64url variant. |
|
14 | + * |
|
15 | + * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
16 | + * @param string $data |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + public static function urlEncode(string $data): string |
|
20 | + { |
|
21 | + return strtr(rtrim(self::encode($data), "="), "+/", "-_"); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * Decode a string using base64url variant. |
|
26 | - * |
|
27 | - * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
28 | - * @param string $data |
|
29 | - * @throws \UnexpectedValueException |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - public static function urlDecode(string $data): string |
|
33 | - { |
|
34 | - $data = strtr($data, "-_", "+/"); |
|
35 | - switch (strlen($data) % 4) { |
|
36 | - case 0: |
|
37 | - break; |
|
38 | - case 2: |
|
39 | - $data .= "=="; |
|
40 | - break; |
|
41 | - case 3: |
|
42 | - $data .= "="; |
|
43 | - break; |
|
44 | - default: |
|
45 | - throw new \UnexpectedValueException( |
|
46 | - "Malformed base64url encoding."); |
|
47 | - } |
|
48 | - return self::decode($data); |
|
49 | - } |
|
24 | + /** |
|
25 | + * Decode a string using base64url variant. |
|
26 | + * |
|
27 | + * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
28 | + * @param string $data |
|
29 | + * @throws \UnexpectedValueException |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + public static function urlDecode(string $data): string |
|
33 | + { |
|
34 | + $data = strtr($data, "-_", "+/"); |
|
35 | + switch (strlen($data) % 4) { |
|
36 | + case 0: |
|
37 | + break; |
|
38 | + case 2: |
|
39 | + $data .= "=="; |
|
40 | + break; |
|
41 | + case 3: |
|
42 | + $data .= "="; |
|
43 | + break; |
|
44 | + default: |
|
45 | + throw new \UnexpectedValueException( |
|
46 | + "Malformed base64url encoding."); |
|
47 | + } |
|
48 | + return self::decode($data); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Check whether string is validly base64url encoded. |
|
53 | - * |
|
54 | - * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
55 | - * @param string $data |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public static function isValidURLEncoding(string $data): bool |
|
59 | - { |
|
60 | - return preg_match('#^[A-Za-z0-9\-_]*$#', $data) == 1; |
|
61 | - } |
|
51 | + /** |
|
52 | + * Check whether string is validly base64url encoded. |
|
53 | + * |
|
54 | + * @link https://en.wikipedia.org/wiki/Base64#URL_applications |
|
55 | + * @param string $data |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public static function isValidURLEncoding(string $data): bool |
|
59 | + { |
|
60 | + return preg_match('#^[A-Za-z0-9\-_]*$#', $data) == 1; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Encode a string in base64. |
|
65 | - * |
|
66 | - * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
67 | - * @param string $data |
|
68 | - * @throws \RuntimeException If encoding fails |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public static function encode(string $data): string |
|
72 | - { |
|
73 | - $ret = @base64_encode($data); |
|
74 | - if (!is_string($ret)) { |
|
75 | - $err = error_get_last(); |
|
76 | - $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
77 | - throw new \RuntimeException($msg ?? "base64_encode() failed."); |
|
78 | - } |
|
79 | - return $ret; |
|
80 | - } |
|
63 | + /** |
|
64 | + * Encode a string in base64. |
|
65 | + * |
|
66 | + * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
67 | + * @param string $data |
|
68 | + * @throws \RuntimeException If encoding fails |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public static function encode(string $data): string |
|
72 | + { |
|
73 | + $ret = @base64_encode($data); |
|
74 | + if (!is_string($ret)) { |
|
75 | + $err = error_get_last(); |
|
76 | + $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
77 | + throw new \RuntimeException($msg ?? "base64_encode() failed."); |
|
78 | + } |
|
79 | + return $ret; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Decode a string from base64 encoding. |
|
84 | - * |
|
85 | - * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
86 | - * @param string $data |
|
87 | - * @throws \RuntimeException If decoding fails |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public static function decode(string $data): string |
|
91 | - { |
|
92 | - $ret = base64_decode($data, true); |
|
93 | - if (!is_string($ret)) { |
|
94 | - $err = error_get_last(); |
|
95 | - $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
96 | - throw new \RuntimeException($msg ?? "base64_decode() failed."); |
|
97 | - } |
|
98 | - return $ret; |
|
99 | - } |
|
82 | + /** |
|
83 | + * Decode a string from base64 encoding. |
|
84 | + * |
|
85 | + * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
86 | + * @param string $data |
|
87 | + * @throws \RuntimeException If decoding fails |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public static function decode(string $data): string |
|
91 | + { |
|
92 | + $ret = base64_decode($data, true); |
|
93 | + if (!is_string($ret)) { |
|
94 | + $err = error_get_last(); |
|
95 | + $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
96 | + throw new \RuntimeException($msg ?? "base64_decode() failed."); |
|
97 | + } |
|
98 | + return $ret; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Check whether string is validly base64 encoded. |
|
103 | - * |
|
104 | - * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
105 | - * @param string $data |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - public static function isValid(string $data): bool |
|
109 | - { |
|
110 | - return preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data) == 1; |
|
111 | - } |
|
101 | + /** |
|
102 | + * Check whether string is validly base64 encoded. |
|
103 | + * |
|
104 | + * @link https://tools.ietf.org/html/rfc4648#section-4 |
|
105 | + * @param string $data |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + public static function isValid(string $data): bool |
|
109 | + { |
|
110 | + return preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data) == 1; |
|
111 | + } |
|
112 | 112 | } |
@@ -7,25 +7,25 @@ |
||
7 | 7 | */ |
8 | 8 | trait ArrayParameterValue |
9 | 9 | { |
10 | - /** |
|
11 | - * Constructor. |
|
12 | - * |
|
13 | - * @param mixed ...$values |
|
14 | - */ |
|
15 | - abstract public function __construct(...$values); |
|
10 | + /** |
|
11 | + * Constructor. |
|
12 | + * |
|
13 | + * @param mixed ...$values |
|
14 | + */ |
|
15 | + abstract public function __construct(...$values); |
|
16 | 16 | |
17 | - /** |
|
18 | - * Initialize from a JSON value. |
|
19 | - * |
|
20 | - * @param array $value |
|
21 | - * @return static |
|
22 | - */ |
|
23 | - public static function fromJSONValue($value) |
|
24 | - { |
|
25 | - if (!is_array($value)) { |
|
26 | - throw new \UnexpectedValueException( |
|
27 | - get_called_class() . " expects an array parameter."); |
|
28 | - } |
|
29 | - return new static(...$value); |
|
30 | - } |
|
17 | + /** |
|
18 | + * Initialize from a JSON value. |
|
19 | + * |
|
20 | + * @param array $value |
|
21 | + * @return static |
|
22 | + */ |
|
23 | + public static function fromJSONValue($value) |
|
24 | + { |
|
25 | + if (!is_array($value)) { |
|
26 | + throw new \UnexpectedValueException( |
|
27 | + get_called_class() . " expects an array parameter."); |
|
28 | + } |
|
29 | + return new static(...$value); |
|
30 | + } |
|
31 | 31 | } |