forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.zep
More file actions
319 lines (270 loc) · 7.13 KB
/
Validator.zep
File metadata and controls
319 lines (270 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Phalcon\Encryption\Security\JWT;
use Phalcon\Encryption\Security\JWT\Exceptions\ValidatorException;
use Phalcon\Encryption\Security\JWT\Signer\SignerInterface;
use Phalcon\Encryption\Security\JWT\Token\Enum;
use Phalcon\Encryption\Security\JWT\Token\Token;
/**
* Class Validator
*/
class Validator
{
/**
* @var array
*/
private claims = [];
/**
* @var array
*/
private errors = [];
/**
* @var int
*/
private timeShift = 0;
/**
* @var Token
*/
private token;
/**
* Validator constructor.
*
* @param Token $token
* @param int $timeShift
*/
public function __construct(<Token> token, int timeShift = 0)
{
var now;
let now = time(),
this->token = token,
this->timeShift = timeShift,
this->claims = [
Enum::AUDIENCE : null,
Enum::EXPIRATION_TIME : now,
Enum::ID : null,
Enum::ISSUED_AT : now,
Enum::ISSUER : null,
Enum::NOT_BEFORE : now,
Enum::SUBJECT : null
];
}
/**
* Return an array with validation errors (if any)
*
* @return array
*/
public function getErrors() -> array
{
return this->errors;
}
/**
* Return the value of a claim
*
* @param string $claim
*
* @return mixed
*/
public function get(string claim) -> mixed | null
{
if isset this->claims[claim] {
return this->claims[claim];
}
return null;
}
/**
* Set the value of a claim, for comparison with the token values
*
* @param string $claim
* @param mixed $value
*
* @return Validator
*/
public function set(string claim, var value) -> <Validator>
{
let this->claims[claim] = value;
return this;
}
/**
* Set the token to be validated
*
* @param Token $token
*
* @return Validator
*/
public function setToken(<Token> token) -> <Validator>
{
let this->token = token;
return this;
}
/**
* Validate a claim
*
* @param string $name
* @param bool|int|string $value
*
* @return Validator
*/
public function validateClaim(string name, var value) -> <Validator>
{
var claimValue;
let claimValue = this->token->getClaims()->get(name);
if value !== claimValue {
let this->errors[] = "Validation: incorrect " . name;
}
return this;
}
/**
* Validate the audience
*
* @param string|array $audience
*
* @return Validator
* @throws ValidatorException
*/
public function validateAudience(var audience) -> <Validator>
{
var item, tokenAudience;
if (typeof audience !== "string" && typeof audience !== "array") {
throw new ValidatorException(
"Audience must be a string or an array"
);
}
if (typeof audience === "string") {
let audience = [audience];
}
let tokenAudience = this->token->getClaims()->get(Enum::AUDIENCE, []);
for item in audience {
if (true !== in_array(item, tokenAudience)) {
let this->errors[] = "Validation: audience not allowed";
}
}
return this;
}
/**
* Validate the expiration time of the token
*
* @param int $timestamp
*
* @return Validator
* @throws ValidatorException
*/
public function validateExpiration(int timestamp) -> <Validator>
{
var tokenExpirationTime;
let tokenExpirationTime = (int) this->token->getClaims()->get(Enum::EXPIRATION_TIME);
if (
this->token->getClaims()->has(Enum::EXPIRATION_TIME) &&
this->getTimestamp(timestamp) > $tokenExpirationTime
) {
let this->errors[] = "Validation: the token has expired";
}
return this;
}
/**
* Validate the id of the token
*
* @param string $id
*
* @return Validator
* @throws ValidatorException
*/
public function validateId(string id) -> <Validator>
{
var tokenId;
let tokenId = (string) this->token->getClaims()->get(Enum::ID);
if ($id !== tokenId) {
let this->errors[] = "Validation: incorrect Id";
}
return this;
}
/**
* Validate the issued at (iat) of the token
*
* @param int $timestamp
*
* @return Validator
* @throws ValidatorException
*/
public function validateIssuedAt(int timestamp) -> <Validator>
{
var tokenIssuedAt;
let tokenIssuedAt = (int) this->token->getClaims()->get(Enum::ISSUED_AT);
if (this->getTimestamp($timestamp) <= tokenIssuedAt) {
let this->errors[] = "Validation: the token cannot be used yet (future)";
}
return this;
}
/**
* Validate the issuer of the token
*
* @param string $issuer
*
* @return Validator
* @throws ValidatorException
*/
public function validateIssuer(string! issuer) -> <Validator>
{
var tokenIssuer;
let tokenIssuer = (string) this->token->getClaims()->get(Enum::ISSUER);
if (issuer !== tokenIssuer) {
let this->errors[] = "Validation: incorrect issuer";
}
return this;
}
/**
* Validate the notbefore (nbf) of the token
*
* @param int $timestamp
*
* @return Validator
* @throws ValidatorException
*/
public function validateNotBefore(int timestamp) -> <Validator>
{
var tokenNotBefore;
let tokenNotBefore = (int) this->token->getClaims()->get(Enum::NOT_BEFORE);
if (this->getTimestamp($timestamp) <= tokenNotBefore) {
let this->errors[] = "Validation: the token cannot be used yet (not before)";
}
return this;
}
/**
* Validate the signature of the token
*
* @param SignerInterface $signer
* @param string $passphrase
*
* @return Validator
* @throws ValidatorException
*/
public function validateSignature(
<SignerInterface> signer,
string passphrase
) -> <Validator> {
if (
true !== signer->verify(
this->token->getSignature()->getHash(),
this->token->getPayload(),
passphrase
)
) {
let this->errors[] = "Validation: the signature does not match";
}
return this;
}
/**
* @param int $timestamp
*
* @return int
*/
private function getTimestamp(int timestamp) -> int
{
return timestamp + this->timeShift;
}
}