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