1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tymon\JWTAuth\Validators; |
4
|
|
|
|
5
|
|
|
use Tymon\JWTAuth\Utils; |
6
|
|
|
use Tymon\JWTAuth\Exceptions\TokenExpiredException; |
7
|
|
|
use Tymon\JWTAuth\Exceptions\TokenInvalidException; |
8
|
|
|
|
9
|
|
|
class PayloadValidator extends AbstractValidator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $requiredClaims = ['iss', 'iat', 'exp', 'nbf', 'sub', 'jti']; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var integer |
18
|
|
|
*/ |
19
|
|
|
protected $refreshTTL = 20160; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Run the validations on the payload array |
23
|
|
|
* |
24
|
|
|
* @param array $value |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
18 |
|
public function check($value) |
28
|
|
|
{ |
29
|
18 |
|
$this->validateStructure($value); |
30
|
|
|
|
31
|
12 |
|
if (! $this->refreshFlow) { |
32
|
12 |
|
$this->validateTimestamps($value); |
33
|
2 |
|
} else { |
34
|
|
|
$this->validateRefresh($value); |
35
|
|
|
} |
36
|
3 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Ensure the payload contains the required claims and |
40
|
|
|
* the claims have the relevant type |
41
|
|
|
* |
42
|
|
|
* @param array $payload |
43
|
|
|
* @throws \Tymon\JWTAuth\Exceptions\TokenInvalidException |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
18 |
|
protected function validateStructure(array $payload) |
47
|
|
|
{ |
48
|
18 |
|
if (count(array_diff_key($this->requiredClaims, array_keys($payload))) !== 0) { |
49
|
6 |
|
throw new TokenInvalidException('JWT payload does not contain the required claims'); |
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Validate the payload timestamps |
57
|
|
|
* |
58
|
|
|
* @param array $payload |
59
|
|
|
* @throws \Tymon\JWTAuth\Exceptions\TokenExpiredException |
60
|
|
|
* @throws \Tymon\JWTAuth\Exceptions\TokenInvalidException |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
12 |
|
protected function validateTimestamps(array $payload) |
64
|
|
|
{ |
65
|
12 |
|
if (isset($payload['nbf']) && Utils::timestamp($payload['nbf'])->isFuture()) { |
66
|
3 |
|
throw new TokenInvalidException('Not Before (nbf) timestamp cannot be in the future', 400); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
if (isset($payload['iat']) && Utils::timestamp($payload['iat'])->isFuture()) { |
70
|
3 |
|
throw new TokenInvalidException('Issued At (iat) timestamp cannot be in the future', 400); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
if (Utils::timestamp($payload['exp'])->isPast()) { |
74
|
3 |
|
throw new TokenExpiredException('Token has expired'); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check the token in the refresh flow context |
82
|
|
|
* |
83
|
|
|
* @param $payload |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
protected function validateRefresh(array $payload) |
87
|
|
|
{ |
88
|
|
|
if (isset($payload['iat']) && Utils::timestamp($payload['iat'])->diffInMinutes(Utils::now()) >= $this->refreshTTL) { |
89
|
|
|
throw new TokenExpiredException('Token has expired and can no longer be refreshed', 400); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the required claims |
97
|
|
|
* |
98
|
|
|
* @param array $claims |
99
|
|
|
*/ |
100
|
|
|
public function setRequiredClaims(array $claims) |
101
|
|
|
{ |
102
|
|
|
$this->requiredClaims = $claims; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the refresh ttl |
109
|
|
|
* |
110
|
|
|
* @param integer $ttl |
111
|
|
|
*/ |
112
|
|
|
public function setRefreshTTL($ttl) |
113
|
|
|
{ |
114
|
|
|
$this->refreshTTL = $ttl; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|