1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon Framework. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Phalcon\Http\JWT; |
13
|
|
|
|
14
|
|
|
use Phalcon\Http\JWT\Exceptions\ValidatorException; |
15
|
|
|
use Phalcon\Http\JWT\Signer\SignerInterface; |
16
|
|
|
use Phalcon\Http\JWT\Token\Enum; |
17
|
|
|
use Phalcon\Http\JWT\Token\Token; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Validator |
21
|
|
|
* |
22
|
|
|
* @property int $timeShift |
23
|
|
|
* @property Token $token |
24
|
|
|
*/ |
25
|
|
|
class Validator |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $timeShift = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Token |
34
|
|
|
*/ |
35
|
|
|
private $token; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Validator constructor. |
39
|
|
|
* |
40
|
|
|
* @param Token $token |
41
|
|
|
* @param int $timeShift |
42
|
|
|
*/ |
43
|
8 |
|
public function __construct(Token $token, int $timeShift = 0) |
44
|
|
|
{ |
45
|
8 |
|
$this->token = $token; |
46
|
8 |
|
$this->timeShift = $timeShift; |
47
|
8 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Token $token |
51
|
|
|
* |
52
|
|
|
* @return Validator |
53
|
|
|
*/ |
54
|
1 |
|
public function setToken(Token $token): Validator |
55
|
|
|
{ |
56
|
1 |
|
$this->token = $token; |
57
|
|
|
|
58
|
1 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $audience |
63
|
|
|
* |
64
|
|
|
* @return Validator |
65
|
|
|
* @throws ValidatorException |
66
|
|
|
*/ |
67
|
2 |
|
public function validateAudience(string $audience): Validator |
68
|
|
|
{ |
69
|
2 |
|
if (!in_array($audience, $this->token->getClaims()->get(Enum::AUDIENCE, []))) { |
|
|
|
|
70
|
1 |
|
throw new ValidatorException( |
71
|
1 |
|
"Validation: audience not allowed" |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $timestamp |
80
|
|
|
* |
81
|
|
|
* @return Validator |
82
|
|
|
* @throws ValidatorException |
83
|
|
|
*/ |
84
|
2 |
|
public function validateExpiration(int $timestamp): Validator |
85
|
|
|
{ |
86
|
|
|
if ( |
87
|
2 |
|
$this->token->getClaims()->has(Enum::EXPIRATION_TIME) && |
88
|
2 |
|
$this->getTimestamp($timestamp) >= (int) $this->token->getClaims()->get(Enum::EXPIRATION_TIME) |
89
|
|
|
) { |
90
|
1 |
|
throw new ValidatorException( |
91
|
1 |
|
"Validation: the token has expired" |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $id |
100
|
|
|
* |
101
|
|
|
* @return Validator |
102
|
|
|
* @throws ValidatorException |
103
|
|
|
*/ |
104
|
2 |
|
public function validateId(string $id): Validator |
105
|
|
|
{ |
106
|
2 |
|
if ($id !== (string) $this->token->getClaims()->get(Enum::ID)) { |
107
|
1 |
|
throw new ValidatorException( |
108
|
1 |
|
"Validation: incorrect Id" |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $timestamp |
117
|
|
|
* |
118
|
|
|
* @return Validator |
119
|
|
|
* @throws ValidatorException |
120
|
|
|
*/ |
121
|
3 |
|
public function validateIssuedAt(int $timestamp): Validator |
122
|
|
|
{ |
123
|
3 |
|
if ($this->getTimestamp($timestamp) <= (int) $this->token->getClaims()->get(Enum::ISSUED_AT)) { |
124
|
2 |
|
throw new ValidatorException( |
125
|
2 |
|
"Validation: the token cannot be used yet (future)" |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $issuer |
134
|
|
|
* |
135
|
|
|
* @return Validator |
136
|
|
|
* @throws ValidatorException |
137
|
|
|
*/ |
138
|
2 |
|
public function validateIssuer(string $issuer): Validator |
139
|
|
|
{ |
140
|
2 |
|
if ($issuer !== (string) $this->token->getClaims()->get(Enum::ISSUER)) { |
141
|
1 |
|
throw new ValidatorException( |
142
|
1 |
|
"Validation: incorrect issuer" |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param int $timestamp |
151
|
|
|
* |
152
|
|
|
* @return Validator |
153
|
|
|
* @throws ValidatorException |
154
|
|
|
*/ |
155
|
2 |
|
public function validateNotBefore(int $timestamp): Validator |
156
|
|
|
{ |
157
|
2 |
|
if ($this->getTimestamp($timestamp) <= (int) $this->token->getClaims()->get(Enum::NOT_BEFORE)) { |
158
|
1 |
|
throw new ValidatorException( |
159
|
1 |
|
"Validation: the token cannot be used yet (not before)" |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param SignerInterface $signer |
168
|
|
|
* @param string $passphrase |
169
|
|
|
* |
170
|
|
|
* @return Validator |
171
|
|
|
* @throws ValidatorException |
172
|
|
|
*/ |
173
|
|
|
public function validateSignature(SignerInterface $signer, string $passphrase): Validator |
174
|
|
|
{ |
175
|
|
|
if (!$signer->verify($this->token->getSignature(), $this->token->getPayload(), $passphrase)) { |
176
|
|
|
throw new ValidatorException( |
177
|
|
|
"Validation: the signature does not match" |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param int $timestamp |
186
|
|
|
* |
187
|
|
|
* @return int |
188
|
|
|
*/ |
189
|
5 |
|
private function getTimestamp(int $timestamp): int |
190
|
|
|
{ |
191
|
5 |
|
return $timestamp + $this->timeShift; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|