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