1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Guarded Authentication package. |
4
|
|
|
* |
5
|
|
|
* (c) Jafar Jabr <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Jafar\Bundle\GuardedAuthenticationBundle\Api\KeyLoader; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class LoadedJWS. |
15
|
|
|
* |
16
|
|
|
* @author Jafar Jabr <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class LoadedJWS |
19
|
|
|
{ |
20
|
|
|
const VERIFIED = 'verified'; |
21
|
|
|
|
22
|
|
|
const EXPIRED = 'expired'; |
23
|
|
|
|
24
|
|
|
const INVALID = 'invalid'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $payload; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $state; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private $hasLifetime; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* LoadedJWS constructor. |
43
|
|
|
* |
44
|
|
|
* @param array $payload |
45
|
|
|
* @param bool $isVerified |
46
|
|
|
* @param bool $hasLifetime |
47
|
|
|
*/ |
48
|
|
|
public function __construct(array $payload, bool $isVerified, bool $hasLifetime = true) |
49
|
|
|
{ |
50
|
|
|
$this->payload = $payload; |
51
|
|
|
$this->hasLifetime = $hasLifetime; |
52
|
|
|
|
53
|
|
|
if (true === $isVerified) { |
54
|
|
|
$this->state = self::VERIFIED; |
55
|
|
|
} |
56
|
|
|
$this->checkIssuedAt(); |
57
|
|
|
$this->checkExpiration(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getPayload() |
64
|
|
|
{ |
65
|
|
|
return $this->payload; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isVerified() |
72
|
|
|
{ |
73
|
|
|
return self::VERIFIED === $this->state; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function isExpired() |
80
|
|
|
{ |
81
|
|
|
$this->checkExpiration(); |
82
|
|
|
|
83
|
|
|
return self::EXPIRED === $this->state; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function isInvalid() |
90
|
|
|
{ |
91
|
|
|
return self::INVALID === $this->state; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Ensures that the signature is not expired. |
96
|
|
|
*/ |
97
|
|
|
private function checkExpiration() |
98
|
|
|
{ |
99
|
|
|
if (!$this->hasLifetime) { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!isset($this->payload['exp']) || !is_numeric($this->payload['exp'])) { |
104
|
|
|
return $this->state = self::INVALID; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (0 <= (new \DateTime())->format('U') - $this->payload['exp']) { |
108
|
|
|
return $this->state = self::EXPIRED; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Ensures that the iat claim is not in the future. |
116
|
|
|
*/ |
117
|
|
|
private function checkIssuedAt() |
118
|
|
|
{ |
119
|
|
|
if (isset($this->payload['iat']) && (int) $this->payload['iat'] > time()) { |
120
|
|
|
return $this->state = self::INVALID; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|