|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
7
|
|
|
use Doctrine\Common\Collections\Collection; |
|
8
|
|
|
use LogicException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Yann Eugoné <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Token |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var int |
|
17
|
|
|
*/ |
|
18
|
|
|
private $id; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $userClass; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $userId; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $value; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $purpose; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $payload = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var DateTime |
|
47
|
|
|
*/ |
|
48
|
|
|
private $createdAt; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
private $createdInformation = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var integer |
|
57
|
|
|
*/ |
|
58
|
|
|
private $allowedUsages; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var DateTime |
|
62
|
|
|
*/ |
|
63
|
|
|
private $expiresAt; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var DateTime |
|
67
|
|
|
*/ |
|
68
|
|
|
private $keepUntil; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var Collection|TokenUsage[] |
|
72
|
|
|
*/ |
|
73
|
|
|
private $usages; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $userClass |
|
77
|
|
|
* @param string $userId |
|
78
|
|
|
* @param string $value |
|
79
|
|
|
* @param string $purpose |
|
80
|
|
|
* @param string $validDuration |
|
81
|
|
|
* @param string $keepDuration |
|
82
|
|
|
* @param integer $allowedUsages |
|
83
|
|
|
* @param array $payload |
|
84
|
|
|
* @param array $information |
|
85
|
|
|
*/ |
|
86
|
10 |
|
public function __construct( |
|
87
|
|
|
$userClass, |
|
88
|
|
|
$userId, |
|
89
|
|
|
$value, |
|
90
|
|
|
$purpose, |
|
91
|
|
|
$validDuration, |
|
92
|
|
|
$keepDuration, |
|
93
|
|
|
$allowedUsages = 1, |
|
94
|
|
|
array $payload = [], |
|
95
|
|
|
array $information = [] |
|
96
|
|
|
) { |
|
97
|
10 |
|
$this->userClass = $userClass; |
|
98
|
10 |
|
$this->userId = $userId; |
|
99
|
10 |
|
$this->value = $value; |
|
100
|
10 |
|
$this->purpose = $purpose; |
|
101
|
10 |
|
$this->createdAt = new DateTime(); |
|
102
|
10 |
|
$this->expiresAt = (new DateTime())->modify($validDuration); |
|
103
|
10 |
|
$this->keepUntil = (clone $this->expiresAt)->modify($keepDuration); |
|
104
|
10 |
|
$this->allowedUsages = $allowedUsages; |
|
105
|
10 |
|
$this->payload = $payload; |
|
106
|
10 |
|
$this->createdInformation = $information; |
|
107
|
10 |
|
$this->usages = new ArrayCollection(); |
|
108
|
10 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return int |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getId() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->id; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function getUserClass() |
|
122
|
|
|
{ |
|
123
|
2 |
|
return $this->userClass; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function getUserId() |
|
130
|
|
|
{ |
|
131
|
2 |
|
return $this->userId; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function getValue() |
|
138
|
|
|
{ |
|
139
|
1 |
|
return $this->value; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function getPurpose() |
|
146
|
|
|
{ |
|
147
|
1 |
|
return $this->purpose; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return array |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function getPayload() |
|
154
|
|
|
{ |
|
155
|
1 |
|
return $this->payload; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return DateTime |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function getCreatedAt() |
|
162
|
|
|
{ |
|
163
|
1 |
|
return $this->createdAt; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function getCreatedInformation() |
|
170
|
|
|
{ |
|
171
|
1 |
|
return $this->createdInformation; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return DateTime |
|
176
|
|
|
*/ |
|
177
|
2 |
|
public function getExpiresAt() |
|
178
|
|
|
{ |
|
179
|
2 |
|
return $this->expiresAt; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @return DateTime |
|
184
|
|
|
*/ |
|
185
|
1 |
|
public function getKeepUntil() |
|
186
|
|
|
{ |
|
187
|
1 |
|
return $this->keepUntil; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return DateTime|null |
|
192
|
|
|
* |
|
193
|
|
|
* @deprecated since version 2.2 and will be removed in 3.0 |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getUsedAt() |
|
196
|
|
|
{ |
|
197
|
|
|
@trigger_error( |
|
|
|
|
|
|
198
|
|
|
'The '.__METHOD__ |
|
199
|
|
|
.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.', |
|
200
|
|
|
E_USER_DEPRECATED |
|
201
|
|
|
); |
|
202
|
|
|
|
|
203
|
|
|
$usage = $this->getLastUsage(); |
|
204
|
|
|
if (null === $usage) { |
|
205
|
|
|
return null; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $usage->getCreatedAt(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param DateTime $usedAt |
|
213
|
|
|
* |
|
214
|
|
|
* @deprecated since version 2.2 and will be removed in 3.0 |
|
215
|
|
|
*/ |
|
216
|
|
|
public function setUsedAt($usedAt) |
|
217
|
|
|
{ |
|
218
|
|
|
@trigger_error( |
|
|
|
|
|
|
219
|
|
|
'The '.__METHOD__ |
|
220
|
|
|
.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.', |
|
221
|
|
|
E_USER_DEPRECATED |
|
222
|
|
|
); |
|
223
|
|
|
|
|
224
|
|
|
$this->consume([], $usedAt); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return array |
|
|
|
|
|
|
229
|
|
|
* |
|
230
|
|
|
* @deprecated since version 2.2 and will be removed in 3.0 |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getUsedInformation() |
|
233
|
|
|
{ |
|
234
|
|
|
@trigger_error( |
|
|
|
|
|
|
235
|
|
|
'The '.__METHOD__ |
|
236
|
|
|
.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.', |
|
237
|
|
|
E_USER_DEPRECATED |
|
238
|
|
|
); |
|
239
|
|
|
|
|
240
|
|
|
$usage = $this->getLastUsage(); |
|
241
|
|
|
if (null === $usage) { |
|
242
|
|
|
return null; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $usage->getInformation(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param array $usedInformation |
|
250
|
|
|
* |
|
251
|
|
|
* @deprecated since version 2.2 and will be removed in 3.0 |
|
252
|
|
|
*/ |
|
253
|
|
|
public function setUsedInformation($usedInformation) |
|
254
|
|
|
{ |
|
255
|
|
|
@trigger_error( |
|
|
|
|
|
|
256
|
|
|
'The '.__METHOD__ |
|
257
|
|
|
.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.', |
|
258
|
|
|
E_USER_DEPRECATED |
|
259
|
|
|
); |
|
260
|
|
|
|
|
261
|
|
|
$this->consume($usedInformation); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return boolean |
|
266
|
|
|
*/ |
|
267
|
4 |
|
public function isExpired() |
|
268
|
|
|
{ |
|
269
|
4 |
|
return $this->expiresAt < new DateTime(); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @deprecated since 2.3 and will be removed in 3.0. Use isConsumed instead. |
|
274
|
|
|
* @return boolean |
|
275
|
|
|
*/ |
|
276
|
|
|
public function isUsed() |
|
277
|
|
|
{ |
|
278
|
|
|
@trigger_error( |
|
|
|
|
|
|
279
|
|
|
__METHOD__.' is deprecated. Use '.__CLASS__.'::isConsumed instead', |
|
280
|
|
|
E_USER_DEPRECATED |
|
281
|
|
|
); |
|
282
|
|
|
|
|
283
|
|
|
return $this->isConsumed(); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @return boolean |
|
288
|
|
|
*/ |
|
289
|
5 |
|
public function isConsumed() |
|
290
|
|
|
{ |
|
291
|
5 |
|
return $this->getCountUsages() >= $this->getAllowedUsages(); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @return int |
|
296
|
|
|
*/ |
|
297
|
5 |
|
public function getAllowedUsages() |
|
298
|
|
|
{ |
|
299
|
5 |
|
return $this->allowedUsages; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @return int |
|
304
|
|
|
*/ |
|
305
|
5 |
|
public function getCountUsages() |
|
306
|
|
|
{ |
|
307
|
5 |
|
return count($this->usages); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @return TokenUsage[] |
|
312
|
|
|
*/ |
|
313
|
2 |
|
public function getUsages() |
|
314
|
|
|
{ |
|
315
|
2 |
|
return $this->usages->toArray(); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @return TokenUsage|null |
|
320
|
|
|
*/ |
|
321
|
1 |
|
public function getLastUsage() |
|
322
|
|
|
{ |
|
323
|
1 |
|
return $this->usages->last(); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* @param array $information |
|
328
|
|
|
* @param DateTime|null $date |
|
329
|
|
|
*/ |
|
330
|
3 |
|
public function consume(array $information, DateTime $date = null) |
|
331
|
|
|
{ |
|
332
|
3 |
|
if ($this->isConsumed()) { |
|
333
|
|
|
throw new LogicException( |
|
334
|
|
|
sprintf('Token "%d" is already consumed.', $this->id) |
|
335
|
|
|
); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
3 |
|
$this->usages->add(new TokenUsage($this, $information, $date)); |
|
339
|
3 |
|
} |
|
340
|
|
|
} |
|
341
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: