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
|
2 |
|
public function getValue() |
138
|
|
|
{ |
139
|
2 |
|
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
|
|
|
* @return boolean |
274
|
|
|
*/ |
275
|
5 |
|
public function isUsed() |
276
|
|
|
{ |
277
|
5 |
|
return $this->getCountUsages() >= $this->getAllowedUsages(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return int |
282
|
|
|
*/ |
283
|
5 |
|
public function getAllowedUsages() |
284
|
|
|
{ |
285
|
5 |
|
return $this->allowedUsages; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return int |
290
|
|
|
*/ |
291
|
5 |
|
public function getCountUsages() |
292
|
|
|
{ |
293
|
5 |
|
return count($this->usages); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return TokenUsage[] |
298
|
|
|
*/ |
299
|
2 |
|
public function getUsages() |
300
|
|
|
{ |
301
|
2 |
|
return $this->usages->toArray(); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return TokenUsage|null |
306
|
|
|
*/ |
307
|
1 |
|
public function getLastUsage() |
308
|
|
|
{ |
309
|
1 |
|
return $this->usages->last(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param array $information |
314
|
|
|
* @param DateTime|null $date |
315
|
|
|
*/ |
316
|
3 |
|
public function consume(array $information, DateTime $date = null) |
317
|
|
|
{ |
318
|
3 |
|
if ($this->isUsed()) { |
319
|
|
|
throw new LogicException( |
320
|
|
|
sprintf('Token "%d" is already used.', $this->id) |
321
|
|
|
); |
322
|
|
|
} |
323
|
|
|
|
324
|
3 |
|
$this->usages->add(new TokenUsage($this, $information,$date)); |
325
|
3 |
|
} |
326
|
|
|
} |
327
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: