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 Collection|TokenUsage[] |
67
|
|
|
*/ |
68
|
|
|
private $usages; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $userClass |
72
|
|
|
* @param string $userId |
73
|
|
|
* @param string $value |
74
|
|
|
* @param string $purpose |
75
|
|
|
* @param string $duration |
76
|
|
|
* @param integer $allowedUsages |
77
|
|
|
* @param array $payload |
78
|
|
|
* @param array $information |
79
|
|
|
*/ |
80
|
10 |
|
public function __construct( |
81
|
|
|
$userClass, |
82
|
|
|
$userId, |
83
|
|
|
$value, |
84
|
|
|
$purpose, |
85
|
|
|
$duration, |
86
|
|
|
$allowedUsages = 1, |
87
|
|
|
array $payload = [], |
88
|
|
|
array $information = [] |
89
|
|
|
) { |
90
|
10 |
|
$this->userClass = $userClass; |
91
|
10 |
|
$this->userId = $userId; |
92
|
10 |
|
$this->value = $value; |
93
|
10 |
|
$this->purpose = $purpose; |
94
|
10 |
|
$this->createdAt = new DateTime(); |
95
|
10 |
|
$this->expiresAt = (new DateTime())->modify($duration); |
96
|
10 |
|
$this->allowedUsages = $allowedUsages; |
97
|
10 |
|
$this->payload = $payload; |
98
|
10 |
|
$this->createdInformation = $information; |
99
|
10 |
|
$this->usages = new ArrayCollection(); |
100
|
10 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getId() |
106
|
|
|
{ |
107
|
|
|
return $this->id; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
2 |
|
public function getUserClass() |
114
|
|
|
{ |
115
|
2 |
|
return $this->userClass; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
2 |
|
public function getUserId() |
122
|
|
|
{ |
123
|
2 |
|
return $this->userId; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
2 |
|
public function getValue() |
130
|
|
|
{ |
131
|
2 |
|
return $this->value; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
1 |
|
public function getPurpose() |
138
|
|
|
{ |
139
|
1 |
|
return $this->purpose; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
1 |
|
public function getPayload() |
146
|
|
|
{ |
147
|
1 |
|
return $this->payload; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return DateTime |
152
|
|
|
*/ |
153
|
1 |
|
public function getCreatedAt() |
154
|
|
|
{ |
155
|
1 |
|
return $this->createdAt; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
1 |
|
public function getCreatedInformation() |
162
|
|
|
{ |
163
|
1 |
|
return $this->createdInformation; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return DateTime |
168
|
|
|
*/ |
169
|
2 |
|
public function getExpiresAt() |
170
|
|
|
{ |
171
|
2 |
|
return $this->expiresAt; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return DateTime|null |
176
|
|
|
* |
177
|
|
|
* @deprecated since version 2.2 and will be removed in 3.0 |
178
|
|
|
*/ |
179
|
|
|
public function getUsedAt() |
180
|
|
|
{ |
181
|
|
|
@trigger_error( |
|
|
|
|
182
|
|
|
'The '.__METHOD__ |
183
|
|
|
.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.', |
184
|
|
|
E_USER_DEPRECATED |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$usage = $this->getLastUsage(); |
188
|
|
|
if (null === $usage) { |
189
|
|
|
return null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $usage->getCreatedAt(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param DateTime $usedAt |
197
|
|
|
* |
198
|
|
|
* @deprecated since version 2.2 and will be removed in 3.0 |
199
|
|
|
*/ |
200
|
|
|
public function setUsedAt($usedAt) |
201
|
|
|
{ |
202
|
|
|
@trigger_error( |
|
|
|
|
203
|
|
|
'The '.__METHOD__ |
204
|
|
|
.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.', |
205
|
|
|
E_USER_DEPRECATED |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$this->consume([], $usedAt); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return array |
213
|
|
|
* |
214
|
|
|
* @deprecated since version 2.2 and will be removed in 3.0 |
215
|
|
|
*/ |
216
|
|
|
public function getUsedInformation() |
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
|
|
|
$usage = $this->getLastUsage(); |
225
|
|
|
if (null === $usage) { |
226
|
|
|
return null; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $usage->getInformation(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param array $usedInformation |
234
|
|
|
* |
235
|
|
|
* @deprecated since version 2.2 and will be removed in 3.0 |
236
|
|
|
*/ |
237
|
|
|
public function setUsedInformation($usedInformation) |
238
|
|
|
{ |
239
|
|
|
@trigger_error( |
|
|
|
|
240
|
|
|
'The '.__METHOD__ |
241
|
|
|
.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.', |
242
|
|
|
E_USER_DEPRECATED |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
$this->consume($usedInformation); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return boolean |
250
|
|
|
*/ |
251
|
4 |
|
public function isExpired() |
252
|
|
|
{ |
253
|
4 |
|
return $this->expiresAt < new DateTime(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return boolean |
258
|
|
|
*/ |
259
|
5 |
|
public function isUsed() |
260
|
|
|
{ |
261
|
5 |
|
return $this->getCountUsages() >= $this->getAllowedUsages(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return int |
266
|
|
|
*/ |
267
|
5 |
|
public function getAllowedUsages() |
268
|
|
|
{ |
269
|
5 |
|
return $this->allowedUsages; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return int |
274
|
|
|
*/ |
275
|
5 |
|
public function getCountUsages() |
276
|
|
|
{ |
277
|
5 |
|
return count($this->usages); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return TokenUsage[] |
282
|
|
|
*/ |
283
|
2 |
|
public function getUsages() |
284
|
|
|
{ |
285
|
2 |
|
return $this->usages->toArray(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return TokenUsage|null |
290
|
|
|
*/ |
291
|
1 |
|
public function getLastUsage() |
292
|
|
|
{ |
293
|
1 |
|
return $this->usages->last(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param array $information |
298
|
|
|
* @param DateTime|null $date |
299
|
|
|
*/ |
300
|
3 |
|
public function consume(array $information, DateTime $date = null) |
301
|
|
|
{ |
302
|
3 |
|
if ($this->isUsed()) { |
303
|
|
|
throw new LogicException( |
304
|
|
|
sprintf('Token "%d" is already used.', $this->id) |
305
|
|
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
3 |
|
$this->usages->add(new TokenUsage($this, $information,$date)); |
309
|
3 |
|
} |
310
|
|
|
} |
311
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: