1 | <?php |
||
26 | class ValidationContext |
||
27 | { |
||
28 | /** |
||
29 | * Reference time. |
||
30 | * |
||
31 | * @var int $_refTime |
||
32 | */ |
||
33 | protected $_refTime; |
||
34 | |||
35 | /** |
||
36 | * Leeway in seconds for the reference time constraints. |
||
37 | * |
||
38 | * @var int $_leeway |
||
39 | */ |
||
40 | protected $_leeway; |
||
41 | |||
42 | /** |
||
43 | * Validation constraints. |
||
44 | * |
||
45 | * @var array $_constraints |
||
46 | */ |
||
47 | protected $_constraints; |
||
48 | |||
49 | /** |
||
50 | * Explicitly defined validators for named claims. |
||
51 | * |
||
52 | * @var Validator[] $_validators |
||
53 | */ |
||
54 | protected $_validators; |
||
55 | |||
56 | /** |
||
57 | * Set of JSON Web Keys usable for the validation. |
||
58 | * |
||
59 | * @var JWKSet $_keys |
||
60 | */ |
||
61 | protected $_keys; |
||
62 | |||
63 | /** |
||
64 | * Whether to allow unsecured JWT's, that is, claims without integrity |
||
65 | * protection nor encryption. |
||
66 | * |
||
67 | * @var bool $_allowUnsecured |
||
68 | */ |
||
69 | protected $_allowUnsecured; |
||
70 | |||
71 | /** |
||
72 | * Constructor. |
||
73 | * |
||
74 | * @param array $constraints Optional array of constraints for the |
||
75 | * registered claims |
||
76 | * @param JWKSet $keys Optional set of JSON Web Keys used for signature |
||
77 | * validation and/or decryption |
||
78 | */ |
||
79 | 41 | public function __construct(array $constraints = null, JWKSet $keys = null) { |
|
87 | |||
88 | /** |
||
89 | * Initialize with a single JSON Web Key. |
||
90 | * |
||
91 | * @param JWK $key JSON Web Key |
||
92 | * @param array $constraints Optional constraints |
||
93 | * @return self |
||
94 | */ |
||
95 | 3 | public static function fromJWK(JWK $key, array $constraints = null) { |
|
98 | |||
99 | /** |
||
100 | * Get self with the reference time. |
||
101 | * |
||
102 | * @param int|null $ts Unix timestamp |
||
103 | * @return self |
||
104 | */ |
||
105 | 23 | public function withReferenceTime($ts) { |
|
110 | |||
111 | /** |
||
112 | * Check whether the reference time is set. |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | 20 | public function hasReferenceTime() { |
|
119 | |||
120 | /** |
||
121 | * Get the reference time. |
||
122 | * |
||
123 | * @throws \LogicException |
||
124 | * @return int |
||
125 | */ |
||
126 | 13 | public function referenceTime() { |
|
132 | |||
133 | /** |
||
134 | * Get self with the reference time leeway. |
||
135 | * |
||
136 | * @param int $seconds |
||
137 | * @return self |
||
138 | */ |
||
139 | 11 | public function withLeeway($seconds) { |
|
144 | |||
145 | /** |
||
146 | * Get the reference time leeway. |
||
147 | * |
||
148 | * @return int |
||
149 | */ |
||
150 | 12 | public function leeway() { |
|
153 | |||
154 | /** |
||
155 | * Get self with a validation constraint. |
||
156 | * |
||
157 | * If the claim does not provide its own validator, an explicit validator |
||
158 | * must be given. |
||
159 | * |
||
160 | * @param string $name Claim name |
||
161 | * @param mixed $constraint Value to check claim against |
||
162 | * @param Validator|null $validator Optional explicit validator |
||
163 | * @return self |
||
164 | */ |
||
165 | 16 | public function withConstraint($name, $constraint, |
|
174 | |||
175 | /** |
||
176 | * Get self with the issuer constraint. |
||
177 | * |
||
178 | * @param string $issuer Issuer name |
||
179 | * @return self |
||
180 | */ |
||
181 | 4 | public function withIssuer($issuer) { |
|
184 | |||
185 | /** |
||
186 | * Get self with the subject constraint. |
||
187 | * |
||
188 | * @param string $subject Subject name |
||
189 | * @return self |
||
190 | */ |
||
191 | 3 | public function withSubject($subject) { |
|
194 | |||
195 | /** |
||
196 | * Get self with the audience constraint. |
||
197 | * |
||
198 | * @param string $audience Audience name |
||
199 | * @return self |
||
200 | */ |
||
201 | 3 | public function withAudience($audience) { |
|
204 | |||
205 | /** |
||
206 | * Get self with the JWT ID constraint. |
||
207 | * |
||
208 | * @param string $id JWT ID |
||
209 | * @return self |
||
210 | */ |
||
211 | 3 | public function withID($id) { |
|
214 | |||
215 | /** |
||
216 | * Check whether a named constraint is present. |
||
217 | * |
||
218 | * @param string $name Claim name |
||
219 | * @return bool |
||
220 | */ |
||
221 | 35 | public function hasConstraint($name) { |
|
224 | |||
225 | /** |
||
226 | * Get a constraint value by the claim name. |
||
227 | * |
||
228 | * @param string $name Claim name |
||
229 | * @throws \LogicException If constraint is not set |
||
230 | * @return mixed Constraint value |
||
231 | */ |
||
232 | 22 | public function constraint($name) { |
|
238 | |||
239 | /** |
||
240 | * Check whether a validator is defined for the given claim name. |
||
241 | * |
||
242 | * @param string $name Claim name |
||
243 | * @return bool |
||
244 | */ |
||
245 | 17 | public function hasValidator($name) { |
|
248 | |||
249 | /** |
||
250 | * Get explicitly defined validator by the claim name. |
||
251 | * |
||
252 | * @param string $name Claim name |
||
253 | * @throws \LogicException If validator is not set |
||
254 | * @return Validator |
||
255 | */ |
||
256 | 3 | public function validator($name) { |
|
262 | |||
263 | /** |
||
264 | * Get a set of JSON Web Keys defined in this context. |
||
265 | * |
||
266 | * @return JWKSet |
||
267 | */ |
||
268 | 8 | public function keys() { |
|
271 | |||
272 | /** |
||
273 | * Get self with 'allow unsecured' flag set. |
||
274 | * |
||
275 | * If the unsecured JWT's are allowed, claims shall be considered valid even |
||
276 | * though they are not signed nor encrypted. |
||
277 | * |
||
278 | * @param bool $allow Whether to allow unsecured JWT's |
||
279 | * @return self |
||
280 | */ |
||
281 | 3 | public function withUnsecuredAllowed($allow) { |
|
286 | |||
287 | /** |
||
288 | * Check whether the unsecured JWT's are allowed. |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | 3 | public function isUnsecuredAllowed() { |
|
295 | |||
296 | /** |
||
297 | * Validate claims. |
||
298 | * |
||
299 | * @param Claims $claims |
||
300 | * @throws ValidationException If any of the claims is not valid |
||
301 | * @return self |
||
302 | */ |
||
303 | 25 | public function validate(Claims $claims) { |
|
312 | } |
||
313 |