|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Description of App\Component\Jwt\Token |
|
5
|
|
|
* |
|
6
|
|
|
* @author pierrefromager |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Component\Jwt; |
|
10
|
|
|
|
|
11
|
|
|
use Firebase\JWT\JWT as Fjwt; |
|
12
|
|
|
use App\Config; |
|
13
|
|
|
use App\Http\Request; |
|
14
|
|
|
|
|
15
|
|
|
class Token implements Interfaces\IToken |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* token time value to be issue |
|
19
|
|
|
* |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
private $issueAt; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* token time to live |
|
26
|
|
|
* |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
private $ttl; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* token delay before issue |
|
33
|
|
|
* |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $issueAtDelay; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* config |
|
40
|
|
|
* |
|
41
|
|
|
* @var Config |
|
42
|
|
|
*/ |
|
43
|
|
|
private $config; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* request |
|
47
|
|
|
* |
|
48
|
|
|
* @var Request |
|
49
|
|
|
*/ |
|
50
|
|
|
private $request; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* token |
|
54
|
|
|
* |
|
55
|
|
|
* @var String |
|
56
|
|
|
*/ |
|
57
|
|
|
private $token; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* instanciate |
|
61
|
|
|
* |
|
62
|
|
|
* @param Config $config |
|
63
|
|
|
* @param Request $request |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(Config $config, Request $request) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->config = $config; |
|
68
|
|
|
$this->request = $request; |
|
69
|
|
|
$this->token = ''; |
|
70
|
|
|
$this->setIssueAt(0); |
|
71
|
|
|
$this->setIssueAtDelay(0); |
|
72
|
|
|
$this->setTtl(0); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* set token string |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $token |
|
79
|
|
|
* @return Token |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function setToken(string $token): Token |
|
82
|
|
|
{ |
|
83
|
|
|
$this->token = $token; |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* get token string |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getToken(): string |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->token; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* encode |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $uid |
|
101
|
|
|
* @param string $login |
|
102
|
|
|
* @param string $password |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function encode(int $uid, string $login, string $password): string |
|
106
|
|
|
{ |
|
107
|
|
|
$this->setToken(Fjwt::encode( |
|
108
|
|
|
$this->getToEncodePayload($uid, $login, $password), |
|
109
|
|
|
$this->getConfigSecret(), |
|
110
|
|
|
$this->getConfigAlgo() |
|
111
|
|
|
)); |
|
112
|
|
|
return $this->token; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* return to encode payload datas |
|
117
|
|
|
* |
|
118
|
|
|
* @param integer $uid |
|
119
|
|
|
* @param string $login |
|
120
|
|
|
* @param string $password |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getToEncodePayload( |
|
124
|
|
|
int $uid, |
|
125
|
|
|
string $login, |
|
126
|
|
|
string $password |
|
127
|
|
|
): array { |
|
128
|
|
|
$tokenId = base64_encode( |
|
129
|
|
|
openssl_random_pseudo_bytes(self::_RANDOM_BYTES_LEN) |
|
130
|
|
|
); |
|
131
|
|
|
$issuedAt = time() - 100; |
|
132
|
|
|
$notBefore = $issuedAt + $this->issueAtDelay; //Adding 10 seconds |
|
133
|
|
|
$expire = $notBefore + $this->ttl; // Adding 60 seconds |
|
134
|
|
|
$serverName = $this->request->getHost(); |
|
135
|
|
|
return [ |
|
136
|
|
|
self::_IAT => $issuedAt, // Issued at: time when the token was generated |
|
137
|
|
|
self::_JTI => $tokenId, // Json Token Id: an unique identifier for the token |
|
138
|
|
|
self::_ISS => $serverName, // Issuer |
|
139
|
|
|
self::_NBF => $notBefore, // Not before |
|
140
|
|
|
self::_EXP => $expire, // Expire |
|
141
|
|
|
self::_DATA => [// Data related to the signer user |
|
142
|
|
|
self::_DATA_ID => $uid, // userid from the users table |
|
143
|
|
|
self::_DATA_LOGIN => $login, // User name |
|
144
|
|
|
self::_DATA_PASSWORD_HASH => password_hash($password, PASSWORD_DEFAULT), |
|
145
|
|
|
self::_DATA_IAT_S => strftime('%c', $issuedAt), |
|
146
|
|
|
self::_DATA_NBF_S => strftime('%c', $notBefore), |
|
147
|
|
|
self::_DATA_EXP_S => strftime('%c', $expire), // Expire |
|
148
|
|
|
] |
|
149
|
|
|
]; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* decode |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $token |
|
156
|
|
|
* @return mixed |
|
157
|
|
|
*/ |
|
158
|
|
|
public function decode(string $token) |
|
159
|
|
|
{ |
|
160
|
|
|
return Fjwt::decode( |
|
161
|
|
|
$token, |
|
162
|
|
|
$this->getConfigSecret(), |
|
163
|
|
|
[$this->getConfigAlgo()] |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* return secret from jwt config |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function getConfigSecret(): string |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->getConfig()[self::_SECRET]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* return algo from jwt config |
|
179
|
|
|
* |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function getConfigAlgo(): string |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->getConfig()[self::_ALGO]; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* set token issue at time |
|
189
|
|
|
* |
|
190
|
|
|
* @param integer $dateTime |
|
191
|
|
|
* @return Token |
|
192
|
|
|
*/ |
|
193
|
|
|
public function setIssueAt(int $dateTime): Token |
|
194
|
|
|
{ |
|
195
|
|
|
$this->issueAt = ($dateTime > 0) ? $dateTime : time(); |
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* setIssueAtDelay |
|
201
|
|
|
* |
|
202
|
|
|
* @param int $delay |
|
203
|
|
|
*/ |
|
204
|
|
|
public function setIssueAtDelay(int $delay): Token |
|
205
|
|
|
{ |
|
206
|
|
|
$this->issueAtDelay = ($delay > 0) ? $delay : self::_ISSUE_AT_DELAY; |
|
207
|
|
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* set token ttl |
|
212
|
|
|
* |
|
213
|
|
|
* @param int $ttl |
|
214
|
|
|
*/ |
|
215
|
|
|
public function setTtl(int $ttl): Token |
|
216
|
|
|
{ |
|
217
|
|
|
$this->ttl = ($ttl > 0) ? $ttl : self::_TTL; |
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* get token config |
|
223
|
|
|
* |
|
224
|
|
|
* @return array |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function getConfig(): array |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->config->getSettings( |
|
229
|
|
|
self::_CONFIG_KEY |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|