@@ -14,167 +14,167 @@ |
||
14 | 14 | */ |
15 | 15 | class Claims implements \Countable, \IteratorAggregate |
16 | 16 | { |
17 | - use TypedClaims; |
|
18 | - |
|
19 | - /** |
|
20 | - * Claims. |
|
21 | - * |
|
22 | - * @var Claim[] |
|
23 | - */ |
|
24 | - protected $_claims; |
|
25 | - |
|
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param Claim ...$claims Zero or more claims |
|
30 | - */ |
|
31 | - public function __construct(Claim ...$claims) |
|
32 | - { |
|
33 | - $this->_claims = []; |
|
34 | - foreach ($claims as $claim) { |
|
35 | - $this->_claims[$claim->name()] = $claim; |
|
36 | - } |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Convert to string. |
|
41 | - * |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - public function __toString(): string |
|
45 | - { |
|
46 | - return $this->toJSON(); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Initialize from a JSON string. |
|
51 | - * |
|
52 | - * @param string $json JSON |
|
53 | - * |
|
54 | - * @throws \UnexpectedValueException If JSON is malformed |
|
55 | - * |
|
56 | - * @return self |
|
57 | - */ |
|
58 | - public static function fromJSON(string $json): self |
|
59 | - { |
|
60 | - $claims = []; |
|
61 | - $fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
62 | - if (!is_array($fields)) { |
|
63 | - throw new \UnexpectedValueException('Invalid JSON.'); |
|
64 | - } |
|
65 | - foreach ($fields as $name => $value) { |
|
66 | - $claims[] = Claim::fromNameAndValue($name, $value); |
|
67 | - } |
|
68 | - return new self(...$claims); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Get self with Claim objects added. |
|
73 | - * |
|
74 | - * @param Claim ...$claims One or more Claim objects |
|
75 | - * |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - public function withClaims(Claim ...$claims): self |
|
79 | - { |
|
80 | - $obj = clone $this; |
|
81 | - foreach ($claims as $claim) { |
|
82 | - $obj->_claims[$claim->name()] = $claim; |
|
83 | - } |
|
84 | - return $obj; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Get all claims. |
|
89 | - * |
|
90 | - * @return Claim[] |
|
91 | - */ |
|
92 | - public function all(): array |
|
93 | - { |
|
94 | - return array_values($this->_claims); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Check whether claim is present. |
|
99 | - * |
|
100 | - * @param string $name Claim name |
|
101 | - * |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public function has(string $name): bool |
|
105 | - { |
|
106 | - return isset($this->_claims[$name]); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Get claim by name. |
|
111 | - * |
|
112 | - * @param string $name Claim name |
|
113 | - * |
|
114 | - * @throws \LogicException If claim is not present |
|
115 | - * |
|
116 | - * @return Claim |
|
117 | - */ |
|
118 | - public function get(string $name): Claim |
|
119 | - { |
|
120 | - if (!isset($this->_claims[$name])) { |
|
121 | - throw new \LogicException("Claim {$name} not set."); |
|
122 | - } |
|
123 | - return $this->_claims[$name]; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Convert to a JSON. |
|
128 | - * |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public function toJSON(): string |
|
132 | - { |
|
133 | - $data = []; |
|
134 | - foreach ($this->_claims as $claim) { |
|
135 | - $data[$claim->name()] = $claim->value(); |
|
136 | - } |
|
137 | - return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Check whether a claims set is valid in the given context. |
|
142 | - * |
|
143 | - * @param ValidationContext $ctx Validation context |
|
144 | - * |
|
145 | - * @return bool |
|
146 | - */ |
|
147 | - public function isValid(ValidationContext $ctx): bool |
|
148 | - { |
|
149 | - try { |
|
150 | - $ctx->validate($this); |
|
151 | - } catch (\RuntimeException $e) { |
|
152 | - return false; |
|
153 | - } |
|
154 | - return true; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get the number of claims. |
|
159 | - * |
|
160 | - * @see \Countable::count() |
|
161 | - * |
|
162 | - * @return int |
|
163 | - */ |
|
164 | - public function count(): int |
|
165 | - { |
|
166 | - return count($this->_claims); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get iterator for Claim objects keyed by claim name. |
|
171 | - * |
|
172 | - * @see \IteratorAggregate::getIterator() |
|
173 | - * |
|
174 | - * @return \ArrayIterator |
|
175 | - */ |
|
176 | - public function getIterator(): \ArrayIterator |
|
177 | - { |
|
178 | - return new \ArrayIterator($this->_claims); |
|
179 | - } |
|
17 | + use TypedClaims; |
|
18 | + |
|
19 | + /** |
|
20 | + * Claims. |
|
21 | + * |
|
22 | + * @var Claim[] |
|
23 | + */ |
|
24 | + protected $_claims; |
|
25 | + |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param Claim ...$claims Zero or more claims |
|
30 | + */ |
|
31 | + public function __construct(Claim ...$claims) |
|
32 | + { |
|
33 | + $this->_claims = []; |
|
34 | + foreach ($claims as $claim) { |
|
35 | + $this->_claims[$claim->name()] = $claim; |
|
36 | + } |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Convert to string. |
|
41 | + * |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + public function __toString(): string |
|
45 | + { |
|
46 | + return $this->toJSON(); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Initialize from a JSON string. |
|
51 | + * |
|
52 | + * @param string $json JSON |
|
53 | + * |
|
54 | + * @throws \UnexpectedValueException If JSON is malformed |
|
55 | + * |
|
56 | + * @return self |
|
57 | + */ |
|
58 | + public static function fromJSON(string $json): self |
|
59 | + { |
|
60 | + $claims = []; |
|
61 | + $fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
62 | + if (!is_array($fields)) { |
|
63 | + throw new \UnexpectedValueException('Invalid JSON.'); |
|
64 | + } |
|
65 | + foreach ($fields as $name => $value) { |
|
66 | + $claims[] = Claim::fromNameAndValue($name, $value); |
|
67 | + } |
|
68 | + return new self(...$claims); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Get self with Claim objects added. |
|
73 | + * |
|
74 | + * @param Claim ...$claims One or more Claim objects |
|
75 | + * |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + public function withClaims(Claim ...$claims): self |
|
79 | + { |
|
80 | + $obj = clone $this; |
|
81 | + foreach ($claims as $claim) { |
|
82 | + $obj->_claims[$claim->name()] = $claim; |
|
83 | + } |
|
84 | + return $obj; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Get all claims. |
|
89 | + * |
|
90 | + * @return Claim[] |
|
91 | + */ |
|
92 | + public function all(): array |
|
93 | + { |
|
94 | + return array_values($this->_claims); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Check whether claim is present. |
|
99 | + * |
|
100 | + * @param string $name Claim name |
|
101 | + * |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public function has(string $name): bool |
|
105 | + { |
|
106 | + return isset($this->_claims[$name]); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Get claim by name. |
|
111 | + * |
|
112 | + * @param string $name Claim name |
|
113 | + * |
|
114 | + * @throws \LogicException If claim is not present |
|
115 | + * |
|
116 | + * @return Claim |
|
117 | + */ |
|
118 | + public function get(string $name): Claim |
|
119 | + { |
|
120 | + if (!isset($this->_claims[$name])) { |
|
121 | + throw new \LogicException("Claim {$name} not set."); |
|
122 | + } |
|
123 | + return $this->_claims[$name]; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Convert to a JSON. |
|
128 | + * |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public function toJSON(): string |
|
132 | + { |
|
133 | + $data = []; |
|
134 | + foreach ($this->_claims as $claim) { |
|
135 | + $data[$claim->name()] = $claim->value(); |
|
136 | + } |
|
137 | + return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Check whether a claims set is valid in the given context. |
|
142 | + * |
|
143 | + * @param ValidationContext $ctx Validation context |
|
144 | + * |
|
145 | + * @return bool |
|
146 | + */ |
|
147 | + public function isValid(ValidationContext $ctx): bool |
|
148 | + { |
|
149 | + try { |
|
150 | + $ctx->validate($this); |
|
151 | + } catch (\RuntimeException $e) { |
|
152 | + return false; |
|
153 | + } |
|
154 | + return true; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get the number of claims. |
|
159 | + * |
|
160 | + * @see \Countable::count() |
|
161 | + * |
|
162 | + * @return int |
|
163 | + */ |
|
164 | + public function count(): int |
|
165 | + { |
|
166 | + return count($this->_claims); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get iterator for Claim objects keyed by claim name. |
|
171 | + * |
|
172 | + * @see \IteratorAggregate::getIterator() |
|
173 | + * |
|
174 | + * @return \ArrayIterator |
|
175 | + */ |
|
176 | + public function getIterator(): \ArrayIterator |
|
177 | + { |
|
178 | + return new \ArrayIterator($this->_claims); |
|
179 | + } |
|
180 | 180 | } |
@@ -29,339 +29,339 @@ |
||
29 | 29 | */ |
30 | 30 | class ValidationContext |
31 | 31 | { |
32 | - /** |
|
33 | - * Reference time. |
|
34 | - * |
|
35 | - * @var int |
|
36 | - */ |
|
37 | - protected $_refTime; |
|
32 | + /** |
|
33 | + * Reference time. |
|
34 | + * |
|
35 | + * @var int |
|
36 | + */ |
|
37 | + protected $_refTime; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Leeway in seconds for the reference time constraints. |
|
41 | - * |
|
42 | - * @var int |
|
43 | - */ |
|
44 | - protected $_leeway; |
|
39 | + /** |
|
40 | + * Leeway in seconds for the reference time constraints. |
|
41 | + * |
|
42 | + * @var int |
|
43 | + */ |
|
44 | + protected $_leeway; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Validation constraints. |
|
48 | - * |
|
49 | - * @var array |
|
50 | - */ |
|
51 | - protected $_constraints; |
|
46 | + /** |
|
47 | + * Validation constraints. |
|
48 | + * |
|
49 | + * @var array |
|
50 | + */ |
|
51 | + protected $_constraints; |
|
52 | 52 | |
53 | - /** |
|
54 | - * Explicitly defined validators for named claims. |
|
55 | - * |
|
56 | - * @var Validator[] |
|
57 | - */ |
|
58 | - protected $_validators; |
|
53 | + /** |
|
54 | + * Explicitly defined validators for named claims. |
|
55 | + * |
|
56 | + * @var Validator[] |
|
57 | + */ |
|
58 | + protected $_validators; |
|
59 | 59 | |
60 | - /** |
|
61 | - * Set of JSON Web Keys usable for the validation. |
|
62 | - * |
|
63 | - * @var JWKSet |
|
64 | - */ |
|
65 | - protected $_keys; |
|
60 | + /** |
|
61 | + * Set of JSON Web Keys usable for the validation. |
|
62 | + * |
|
63 | + * @var JWKSet |
|
64 | + */ |
|
65 | + protected $_keys; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Whether to allow unsecured JWT's, that is, claims without integrity |
|
69 | - * protection nor encryption. |
|
70 | - * |
|
71 | - * @var bool |
|
72 | - */ |
|
73 | - protected $_allowUnsecured; |
|
67 | + /** |
|
68 | + * Whether to allow unsecured JWT's, that is, claims without integrity |
|
69 | + * protection nor encryption. |
|
70 | + * |
|
71 | + * @var bool |
|
72 | + */ |
|
73 | + protected $_allowUnsecured; |
|
74 | 74 | |
75 | - /** |
|
76 | - * Constructor. |
|
77 | - * |
|
78 | - * @param null|array $constraints Optional array of constraints for the |
|
79 | - * registered claims |
|
80 | - * @param null|JWKSet $keys Optional set of JSON Web Keys used for |
|
81 | - * signature validation and/or decryption |
|
82 | - */ |
|
83 | - public function __construct(?array $constraints = null, ?JWKSet $keys = null) |
|
84 | - { |
|
85 | - $this->_refTime = time(); |
|
86 | - $this->_leeway = 60; |
|
87 | - $this->_constraints = $constraints ? $constraints : []; |
|
88 | - $this->_validators = []; |
|
89 | - $this->_keys = $keys ? $keys : new JWKSet(); |
|
90 | - $this->_allowUnsecured = false; |
|
91 | - } |
|
75 | + /** |
|
76 | + * Constructor. |
|
77 | + * |
|
78 | + * @param null|array $constraints Optional array of constraints for the |
|
79 | + * registered claims |
|
80 | + * @param null|JWKSet $keys Optional set of JSON Web Keys used for |
|
81 | + * signature validation and/or decryption |
|
82 | + */ |
|
83 | + public function __construct(?array $constraints = null, ?JWKSet $keys = null) |
|
84 | + { |
|
85 | + $this->_refTime = time(); |
|
86 | + $this->_leeway = 60; |
|
87 | + $this->_constraints = $constraints ? $constraints : []; |
|
88 | + $this->_validators = []; |
|
89 | + $this->_keys = $keys ? $keys : new JWKSet(); |
|
90 | + $this->_allowUnsecured = false; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Initialize with a single JSON Web Key. |
|
95 | - * |
|
96 | - * @param JWK $key JSON Web Key |
|
97 | - * @param null|array $constraints Optional constraints |
|
98 | - * |
|
99 | - * @return self |
|
100 | - */ |
|
101 | - public static function fromJWK(JWK $key, ?array $constraints = null): self |
|
102 | - { |
|
103 | - return new self($constraints, new JWKSet($key)); |
|
104 | - } |
|
93 | + /** |
|
94 | + * Initialize with a single JSON Web Key. |
|
95 | + * |
|
96 | + * @param JWK $key JSON Web Key |
|
97 | + * @param null|array $constraints Optional constraints |
|
98 | + * |
|
99 | + * @return self |
|
100 | + */ |
|
101 | + public static function fromJWK(JWK $key, ?array $constraints = null): self |
|
102 | + { |
|
103 | + return new self($constraints, new JWKSet($key)); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Get self with the reference time. |
|
108 | - * |
|
109 | - * @param null|int $ts Unix timestamp |
|
110 | - * |
|
111 | - * @return self |
|
112 | - */ |
|
113 | - public function withReferenceTime(?int $ts): self |
|
114 | - { |
|
115 | - $obj = clone $this; |
|
116 | - $obj->_refTime = $ts; |
|
117 | - return $obj; |
|
118 | - } |
|
106 | + /** |
|
107 | + * Get self with the reference time. |
|
108 | + * |
|
109 | + * @param null|int $ts Unix timestamp |
|
110 | + * |
|
111 | + * @return self |
|
112 | + */ |
|
113 | + public function withReferenceTime(?int $ts): self |
|
114 | + { |
|
115 | + $obj = clone $this; |
|
116 | + $obj->_refTime = $ts; |
|
117 | + return $obj; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * Check whether the reference time is set. |
|
122 | - * |
|
123 | - * @return bool |
|
124 | - */ |
|
125 | - public function hasReferenceTime(): bool |
|
126 | - { |
|
127 | - return isset($this->_refTime); |
|
128 | - } |
|
120 | + /** |
|
121 | + * Check whether the reference time is set. |
|
122 | + * |
|
123 | + * @return bool |
|
124 | + */ |
|
125 | + public function hasReferenceTime(): bool |
|
126 | + { |
|
127 | + return isset($this->_refTime); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Get the reference time. |
|
132 | - * |
|
133 | - * @throws \LogicException |
|
134 | - * |
|
135 | - * @return int |
|
136 | - */ |
|
137 | - public function referenceTime(): int |
|
138 | - { |
|
139 | - if (!$this->hasReferenceTime()) { |
|
140 | - throw new \LogicException('Reference time not set.'); |
|
141 | - } |
|
142 | - return $this->_refTime; |
|
143 | - } |
|
130 | + /** |
|
131 | + * Get the reference time. |
|
132 | + * |
|
133 | + * @throws \LogicException |
|
134 | + * |
|
135 | + * @return int |
|
136 | + */ |
|
137 | + public function referenceTime(): int |
|
138 | + { |
|
139 | + if (!$this->hasReferenceTime()) { |
|
140 | + throw new \LogicException('Reference time not set.'); |
|
141 | + } |
|
142 | + return $this->_refTime; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * Get self with the reference time leeway. |
|
147 | - * |
|
148 | - * @param int $seconds |
|
149 | - * |
|
150 | - * @return self |
|
151 | - */ |
|
152 | - public function withLeeway(int $seconds): self |
|
153 | - { |
|
154 | - $obj = clone $this; |
|
155 | - $obj->_leeway = $seconds; |
|
156 | - return $obj; |
|
157 | - } |
|
145 | + /** |
|
146 | + * Get self with the reference time leeway. |
|
147 | + * |
|
148 | + * @param int $seconds |
|
149 | + * |
|
150 | + * @return self |
|
151 | + */ |
|
152 | + public function withLeeway(int $seconds): self |
|
153 | + { |
|
154 | + $obj = clone $this; |
|
155 | + $obj->_leeway = $seconds; |
|
156 | + return $obj; |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Get the reference time leeway. |
|
161 | - * |
|
162 | - * @return int |
|
163 | - */ |
|
164 | - public function leeway(): int |
|
165 | - { |
|
166 | - return $this->_leeway; |
|
167 | - } |
|
159 | + /** |
|
160 | + * Get the reference time leeway. |
|
161 | + * |
|
162 | + * @return int |
|
163 | + */ |
|
164 | + public function leeway(): int |
|
165 | + { |
|
166 | + return $this->_leeway; |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Get self with a validation constraint. |
|
171 | - * |
|
172 | - * If the claim does not provide its own validator, an explicit validator |
|
173 | - * must be given. |
|
174 | - * |
|
175 | - * @param string $name Claim name |
|
176 | - * @param mixed $constraint Value to check claim against |
|
177 | - * @param null|Validator $validator Optional explicit validator |
|
178 | - * |
|
179 | - * @return self |
|
180 | - */ |
|
181 | - public function withConstraint(string $name, $constraint, |
|
182 | - ?Validator $validator = null): self |
|
183 | - { |
|
184 | - $obj = clone $this; |
|
185 | - $obj->_constraints[$name] = $constraint; |
|
186 | - if ($validator) { |
|
187 | - $obj->_validators[$name] = $validator; |
|
188 | - } |
|
189 | - return $obj; |
|
190 | - } |
|
169 | + /** |
|
170 | + * Get self with a validation constraint. |
|
171 | + * |
|
172 | + * If the claim does not provide its own validator, an explicit validator |
|
173 | + * must be given. |
|
174 | + * |
|
175 | + * @param string $name Claim name |
|
176 | + * @param mixed $constraint Value to check claim against |
|
177 | + * @param null|Validator $validator Optional explicit validator |
|
178 | + * |
|
179 | + * @return self |
|
180 | + */ |
|
181 | + public function withConstraint(string $name, $constraint, |
|
182 | + ?Validator $validator = null): self |
|
183 | + { |
|
184 | + $obj = clone $this; |
|
185 | + $obj->_constraints[$name] = $constraint; |
|
186 | + if ($validator) { |
|
187 | + $obj->_validators[$name] = $validator; |
|
188 | + } |
|
189 | + return $obj; |
|
190 | + } |
|
191 | 191 | |
192 | - /** |
|
193 | - * Get self with the issuer constraint. |
|
194 | - * |
|
195 | - * @param string $issuer Issuer name |
|
196 | - * |
|
197 | - * @return self |
|
198 | - */ |
|
199 | - public function withIssuer(string $issuer): self |
|
200 | - { |
|
201 | - return $this->withConstraint(RegisteredClaim::NAME_ISSUER, $issuer); |
|
202 | - } |
|
192 | + /** |
|
193 | + * Get self with the issuer constraint. |
|
194 | + * |
|
195 | + * @param string $issuer Issuer name |
|
196 | + * |
|
197 | + * @return self |
|
198 | + */ |
|
199 | + public function withIssuer(string $issuer): self |
|
200 | + { |
|
201 | + return $this->withConstraint(RegisteredClaim::NAME_ISSUER, $issuer); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * Get self with the subject constraint. |
|
206 | - * |
|
207 | - * @param string $subject Subject name |
|
208 | - * |
|
209 | - * @return self |
|
210 | - */ |
|
211 | - public function withSubject(string $subject): self |
|
212 | - { |
|
213 | - return $this->withConstraint(RegisteredClaim::NAME_SUBJECT, $subject); |
|
214 | - } |
|
204 | + /** |
|
205 | + * Get self with the subject constraint. |
|
206 | + * |
|
207 | + * @param string $subject Subject name |
|
208 | + * |
|
209 | + * @return self |
|
210 | + */ |
|
211 | + public function withSubject(string $subject): self |
|
212 | + { |
|
213 | + return $this->withConstraint(RegisteredClaim::NAME_SUBJECT, $subject); |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * Get self with the audience constraint. |
|
218 | - * |
|
219 | - * @param string $audience Audience name |
|
220 | - * |
|
221 | - * @return self |
|
222 | - */ |
|
223 | - public function withAudience(string $audience): self |
|
224 | - { |
|
225 | - return $this->withConstraint(RegisteredClaim::NAME_AUDIENCE, $audience); |
|
226 | - } |
|
216 | + /** |
|
217 | + * Get self with the audience constraint. |
|
218 | + * |
|
219 | + * @param string $audience Audience name |
|
220 | + * |
|
221 | + * @return self |
|
222 | + */ |
|
223 | + public function withAudience(string $audience): self |
|
224 | + { |
|
225 | + return $this->withConstraint(RegisteredClaim::NAME_AUDIENCE, $audience); |
|
226 | + } |
|
227 | 227 | |
228 | - /** |
|
229 | - * Get self with the JWT ID constraint. |
|
230 | - * |
|
231 | - * @param string $id JWT ID |
|
232 | - * |
|
233 | - * @return self |
|
234 | - */ |
|
235 | - public function withID(string $id): self |
|
236 | - { |
|
237 | - return $this->withConstraint(RegisteredClaim::NAME_JWT_ID, $id); |
|
238 | - } |
|
228 | + /** |
|
229 | + * Get self with the JWT ID constraint. |
|
230 | + * |
|
231 | + * @param string $id JWT ID |
|
232 | + * |
|
233 | + * @return self |
|
234 | + */ |
|
235 | + public function withID(string $id): self |
|
236 | + { |
|
237 | + return $this->withConstraint(RegisteredClaim::NAME_JWT_ID, $id); |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * Check whether a named constraint is present. |
|
242 | - * |
|
243 | - * @param string $name Claim name |
|
244 | - * |
|
245 | - * @return bool |
|
246 | - */ |
|
247 | - public function hasConstraint(string $name): bool |
|
248 | - { |
|
249 | - return isset($this->_constraints[$name]); |
|
250 | - } |
|
240 | + /** |
|
241 | + * Check whether a named constraint is present. |
|
242 | + * |
|
243 | + * @param string $name Claim name |
|
244 | + * |
|
245 | + * @return bool |
|
246 | + */ |
|
247 | + public function hasConstraint(string $name): bool |
|
248 | + { |
|
249 | + return isset($this->_constraints[$name]); |
|
250 | + } |
|
251 | 251 | |
252 | - /** |
|
253 | - * Get a constraint value by the claim name. |
|
254 | - * |
|
255 | - * @param string $name Claim name |
|
256 | - * |
|
257 | - * @throws \LogicException If constraint is not set |
|
258 | - * |
|
259 | - * @return mixed Constraint value |
|
260 | - */ |
|
261 | - public function constraint(string $name) |
|
262 | - { |
|
263 | - if (!$this->hasConstraint($name)) { |
|
264 | - throw new \LogicException("Constraint {$name} not set."); |
|
265 | - } |
|
266 | - return $this->_constraints[$name]; |
|
267 | - } |
|
252 | + /** |
|
253 | + * Get a constraint value by the claim name. |
|
254 | + * |
|
255 | + * @param string $name Claim name |
|
256 | + * |
|
257 | + * @throws \LogicException If constraint is not set |
|
258 | + * |
|
259 | + * @return mixed Constraint value |
|
260 | + */ |
|
261 | + public function constraint(string $name) |
|
262 | + { |
|
263 | + if (!$this->hasConstraint($name)) { |
|
264 | + throw new \LogicException("Constraint {$name} not set."); |
|
265 | + } |
|
266 | + return $this->_constraints[$name]; |
|
267 | + } |
|
268 | 268 | |
269 | - /** |
|
270 | - * Check whether a validator is defined for the given claim name. |
|
271 | - * |
|
272 | - * @param string $name Claim name |
|
273 | - * |
|
274 | - * @return bool |
|
275 | - */ |
|
276 | - public function hasValidator(string $name): bool |
|
277 | - { |
|
278 | - return isset($this->_validators[$name]); |
|
279 | - } |
|
269 | + /** |
|
270 | + * Check whether a validator is defined for the given claim name. |
|
271 | + * |
|
272 | + * @param string $name Claim name |
|
273 | + * |
|
274 | + * @return bool |
|
275 | + */ |
|
276 | + public function hasValidator(string $name): bool |
|
277 | + { |
|
278 | + return isset($this->_validators[$name]); |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * Get explicitly defined validator by the claim name. |
|
283 | - * |
|
284 | - * @param string $name Claim name |
|
285 | - * |
|
286 | - * @throws \LogicException If validator is not set |
|
287 | - * |
|
288 | - * @return Validator |
|
289 | - */ |
|
290 | - public function validator(string $name): Validator |
|
291 | - { |
|
292 | - if (!$this->hasValidator($name)) { |
|
293 | - throw new \LogicException("Validator {$name} not set."); |
|
294 | - } |
|
295 | - return $this->_validators[$name]; |
|
296 | - } |
|
281 | + /** |
|
282 | + * Get explicitly defined validator by the claim name. |
|
283 | + * |
|
284 | + * @param string $name Claim name |
|
285 | + * |
|
286 | + * @throws \LogicException If validator is not set |
|
287 | + * |
|
288 | + * @return Validator |
|
289 | + */ |
|
290 | + public function validator(string $name): Validator |
|
291 | + { |
|
292 | + if (!$this->hasValidator($name)) { |
|
293 | + throw new \LogicException("Validator {$name} not set."); |
|
294 | + } |
|
295 | + return $this->_validators[$name]; |
|
296 | + } |
|
297 | 297 | |
298 | - /** |
|
299 | - * Get a set of JSON Web Keys defined in this context. |
|
300 | - * |
|
301 | - * @return JWKSet |
|
302 | - */ |
|
303 | - public function keys(): JWKSet |
|
304 | - { |
|
305 | - return $this->_keys; |
|
306 | - } |
|
298 | + /** |
|
299 | + * Get a set of JSON Web Keys defined in this context. |
|
300 | + * |
|
301 | + * @return JWKSet |
|
302 | + */ |
|
303 | + public function keys(): JWKSet |
|
304 | + { |
|
305 | + return $this->_keys; |
|
306 | + } |
|
307 | 307 | |
308 | - /** |
|
309 | - * Get self with 'allow unsecured' flag set. |
|
310 | - * |
|
311 | - * If the unsecured JWT's are allowed, claims shall be considered valid even |
|
312 | - * though they are not signed nor encrypted. |
|
313 | - * |
|
314 | - * @param bool $allow Whether to allow unsecured JWT's |
|
315 | - * |
|
316 | - * @return self |
|
317 | - */ |
|
318 | - public function withUnsecuredAllowed(bool $allow): self |
|
319 | - { |
|
320 | - $obj = clone $this; |
|
321 | - $obj->_allowUnsecured = $allow; |
|
322 | - return $obj; |
|
323 | - } |
|
308 | + /** |
|
309 | + * Get self with 'allow unsecured' flag set. |
|
310 | + * |
|
311 | + * If the unsecured JWT's are allowed, claims shall be considered valid even |
|
312 | + * though they are not signed nor encrypted. |
|
313 | + * |
|
314 | + * @param bool $allow Whether to allow unsecured JWT's |
|
315 | + * |
|
316 | + * @return self |
|
317 | + */ |
|
318 | + public function withUnsecuredAllowed(bool $allow): self |
|
319 | + { |
|
320 | + $obj = clone $this; |
|
321 | + $obj->_allowUnsecured = $allow; |
|
322 | + return $obj; |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * Check whether the unsecured JWT's are allowed. |
|
327 | - * |
|
328 | - * @return bool |
|
329 | - */ |
|
330 | - public function isUnsecuredAllowed(): bool |
|
331 | - { |
|
332 | - return $this->_allowUnsecured; |
|
333 | - } |
|
325 | + /** |
|
326 | + * Check whether the unsecured JWT's are allowed. |
|
327 | + * |
|
328 | + * @return bool |
|
329 | + */ |
|
330 | + public function isUnsecuredAllowed(): bool |
|
331 | + { |
|
332 | + return $this->_allowUnsecured; |
|
333 | + } |
|
334 | 334 | |
335 | - /** |
|
336 | - * Validate claims. |
|
337 | - * |
|
338 | - * @param Claims $claims |
|
339 | - * |
|
340 | - * @throws ValidationException If any of the claims is not valid |
|
341 | - * |
|
342 | - * @return self |
|
343 | - */ |
|
344 | - public function validate(Claims $claims): self |
|
345 | - { |
|
346 | - $claimset = iterator_to_array($claims); |
|
347 | - // validate required constraints |
|
348 | - foreach (array_keys($this->_constraints) as $name) { |
|
349 | - if (!isset($claimset[$name])) { |
|
350 | - throw new ValidationException("Claim '{$name}' is required."); |
|
351 | - } |
|
352 | - if (!$claimset[$name]->validateWithContext($this)) { |
|
353 | - throw new ValidationException( |
|
354 | - "Validation of claim '{$name}' failed."); |
|
355 | - } |
|
356 | - unset($claimset[$name]); |
|
357 | - } |
|
358 | - // validate remaining claims using default validators |
|
359 | - foreach ($claimset as $name => $claim) { |
|
360 | - if (!$claim->validateWithContext($this)) { |
|
361 | - throw new ValidationException( |
|
362 | - "Validation of claim '{$name}' failed."); |
|
363 | - } |
|
364 | - } |
|
365 | - return $this; |
|
366 | - } |
|
335 | + /** |
|
336 | + * Validate claims. |
|
337 | + * |
|
338 | + * @param Claims $claims |
|
339 | + * |
|
340 | + * @throws ValidationException If any of the claims is not valid |
|
341 | + * |
|
342 | + * @return self |
|
343 | + */ |
|
344 | + public function validate(Claims $claims): self |
|
345 | + { |
|
346 | + $claimset = iterator_to_array($claims); |
|
347 | + // validate required constraints |
|
348 | + foreach (array_keys($this->_constraints) as $name) { |
|
349 | + if (!isset($claimset[$name])) { |
|
350 | + throw new ValidationException("Claim '{$name}' is required."); |
|
351 | + } |
|
352 | + if (!$claimset[$name]->validateWithContext($this)) { |
|
353 | + throw new ValidationException( |
|
354 | + "Validation of claim '{$name}' failed."); |
|
355 | + } |
|
356 | + unset($claimset[$name]); |
|
357 | + } |
|
358 | + // validate remaining claims using default validators |
|
359 | + foreach ($claimset as $name => $claim) { |
|
360 | + if (!$claim->validateWithContext($this)) { |
|
361 | + throw new ValidationException( |
|
362 | + "Validation of claim '{$name}' failed."); |
|
363 | + } |
|
364 | + } |
|
365 | + return $this; |
|
366 | + } |
|
367 | 367 | } |
@@ -14,118 +14,118 @@ |
||
14 | 14 | */ |
15 | 15 | class Claim |
16 | 16 | { |
17 | - /** |
|
18 | - * Claim name. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $_name; |
|
17 | + /** |
|
18 | + * Claim name. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $_name; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Claim value. |
|
26 | - * |
|
27 | - * @var mixed |
|
28 | - */ |
|
29 | - protected $_value; |
|
24 | + /** |
|
25 | + * Claim value. |
|
26 | + * |
|
27 | + * @var mixed |
|
28 | + */ |
|
29 | + protected $_value; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Claim validator. |
|
33 | - * |
|
34 | - * @var null|Validator |
|
35 | - */ |
|
36 | - protected $_validator; |
|
31 | + /** |
|
32 | + * Claim validator. |
|
33 | + * |
|
34 | + * @var null|Validator |
|
35 | + */ |
|
36 | + protected $_validator; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Constructor. |
|
40 | - * |
|
41 | - * @param string $name Claim name |
|
42 | - * @param mixed $value Claim value |
|
43 | - * @param null|Validator $validator Claim validator or null if claim doesn't |
|
44 | - * provide validation |
|
45 | - */ |
|
46 | - public function __construct(string $name, $value, ?Validator $validator = null) |
|
47 | - { |
|
48 | - $this->_name = $name; |
|
49 | - $this->_value = $value; |
|
50 | - $this->_validator = $validator; |
|
51 | - } |
|
38 | + /** |
|
39 | + * Constructor. |
|
40 | + * |
|
41 | + * @param string $name Claim name |
|
42 | + * @param mixed $value Claim value |
|
43 | + * @param null|Validator $validator Claim validator or null if claim doesn't |
|
44 | + * provide validation |
|
45 | + */ |
|
46 | + public function __construct(string $name, $value, ?Validator $validator = null) |
|
47 | + { |
|
48 | + $this->_name = $name; |
|
49 | + $this->_value = $value; |
|
50 | + $this->_validator = $validator; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Initialize from a name and a value. |
|
55 | - * |
|
56 | - * Returns a specific claim object if applicable. |
|
57 | - * |
|
58 | - * @param string $name Claim name |
|
59 | - * @param mixed $value Claim value |
|
60 | - * |
|
61 | - * @return Claim |
|
62 | - */ |
|
63 | - public static function fromNameAndValue(string $name, $value): Claim |
|
64 | - { |
|
65 | - if (array_key_exists($name, RegisteredClaim::MAP_NAME_TO_CLASS)) { |
|
66 | - $cls = RegisteredClaim::MAP_NAME_TO_CLASS[$name]; |
|
67 | - return $cls::fromJSONValue($value); |
|
68 | - } |
|
69 | - return new self($name, $value); |
|
70 | - } |
|
53 | + /** |
|
54 | + * Initialize from a name and a value. |
|
55 | + * |
|
56 | + * Returns a specific claim object if applicable. |
|
57 | + * |
|
58 | + * @param string $name Claim name |
|
59 | + * @param mixed $value Claim value |
|
60 | + * |
|
61 | + * @return Claim |
|
62 | + */ |
|
63 | + public static function fromNameAndValue(string $name, $value): Claim |
|
64 | + { |
|
65 | + if (array_key_exists($name, RegisteredClaim::MAP_NAME_TO_CLASS)) { |
|
66 | + $cls = RegisteredClaim::MAP_NAME_TO_CLASS[$name]; |
|
67 | + return $cls::fromJSONValue($value); |
|
68 | + } |
|
69 | + return new self($name, $value); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Get the claim name. |
|
74 | - * |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function name(): string |
|
78 | - { |
|
79 | - return $this->_name; |
|
80 | - } |
|
72 | + /** |
|
73 | + * Get the claim name. |
|
74 | + * |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function name(): string |
|
78 | + { |
|
79 | + return $this->_name; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Get the claim value. |
|
84 | - * |
|
85 | - * @return mixed |
|
86 | - */ |
|
87 | - public function value() |
|
88 | - { |
|
89 | - return $this->_value; |
|
90 | - } |
|
82 | + /** |
|
83 | + * Get the claim value. |
|
84 | + * |
|
85 | + * @return mixed |
|
86 | + */ |
|
87 | + public function value() |
|
88 | + { |
|
89 | + return $this->_value; |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Validate the claim against a given constraint. |
|
94 | - * |
|
95 | - * @param mixed $constraint Constraint value |
|
96 | - * |
|
97 | - * @return bool True if the claim is valid |
|
98 | - */ |
|
99 | - public function validate($constraint): bool |
|
100 | - { |
|
101 | - // if claim has no validator, consider validation failed |
|
102 | - if (!isset($this->_validator)) { |
|
103 | - return false; |
|
104 | - } |
|
105 | - return $this->_validator->validate($this->_value, $constraint); |
|
106 | - } |
|
92 | + /** |
|
93 | + * Validate the claim against a given constraint. |
|
94 | + * |
|
95 | + * @param mixed $constraint Constraint value |
|
96 | + * |
|
97 | + * @return bool True if the claim is valid |
|
98 | + */ |
|
99 | + public function validate($constraint): bool |
|
100 | + { |
|
101 | + // if claim has no validator, consider validation failed |
|
102 | + if (!isset($this->_validator)) { |
|
103 | + return false; |
|
104 | + } |
|
105 | + return $this->_validator->validate($this->_value, $constraint); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Validate the claim in a given context. |
|
110 | - * |
|
111 | - * Overridden in specific claims that provide default validation. |
|
112 | - * |
|
113 | - * @param ValidationContext $ctx |
|
114 | - * |
|
115 | - * @return bool True if claim is valid |
|
116 | - */ |
|
117 | - public function validateWithContext(ValidationContext $ctx): bool |
|
118 | - { |
|
119 | - // if validator has no constraint for the claim |
|
120 | - if (!$ctx->hasConstraint($this->_name)) { |
|
121 | - return true; |
|
122 | - } |
|
123 | - // if validation context has an explicitly defined validator for the claim |
|
124 | - if ($ctx->hasValidator($this->_name)) { |
|
125 | - return $ctx->validator($this->_name) |
|
126 | - ->validate($this->_value, $ctx->constraint($this->_name)); |
|
127 | - } |
|
128 | - // validate using claim's default validator |
|
129 | - return $this->validate($ctx->constraint($this->_name)); |
|
130 | - } |
|
108 | + /** |
|
109 | + * Validate the claim in a given context. |
|
110 | + * |
|
111 | + * Overridden in specific claims that provide default validation. |
|
112 | + * |
|
113 | + * @param ValidationContext $ctx |
|
114 | + * |
|
115 | + * @return bool True if claim is valid |
|
116 | + */ |
|
117 | + public function validateWithContext(ValidationContext $ctx): bool |
|
118 | + { |
|
119 | + // if validator has no constraint for the claim |
|
120 | + if (!$ctx->hasConstraint($this->_name)) { |
|
121 | + return true; |
|
122 | + } |
|
123 | + // if validation context has an explicitly defined validator for the claim |
|
124 | + if ($ctx->hasValidator($this->_name)) { |
|
125 | + return $ctx->validator($this->_name) |
|
126 | + ->validate($this->_value, $ctx->constraint($this->_name)); |
|
127 | + } |
|
128 | + // validate using claim's default validator |
|
129 | + return $this->validate($ctx->constraint($this->_name)); |
|
130 | + } |
|
131 | 131 | } |