1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palladium\Entity; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstraction, that contains information about user's authentication details |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Palladium\Contract\HasId; |
10
|
|
|
use Palladium\Exception\InvalidToken; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Identity implements HasId |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
const TOKEN_SIZE = 16; |
17
|
|
|
|
18
|
|
|
const ACTION_NONE = null; |
19
|
|
|
const ACTION_VERIFY = 1; |
20
|
|
|
const ACTION_RESET = 2; |
21
|
|
|
const ACTION_UPDATE = 4; |
22
|
|
|
|
23
|
|
|
const STATUS_ANY = null; |
24
|
|
|
const STATUS_NEW = 1; // not verified user |
25
|
|
|
const STATUS_ACTIVE = 2; // this is the "good" state |
26
|
|
|
const STATUS_DISCARDED = 4; // user logged out or changed password |
27
|
|
|
const STATUS_BLOCKED = 8; // someone tried to us an invalid auth cookie |
28
|
|
|
const STATUS_EXPIRED = 16; |
29
|
|
|
|
30
|
|
|
const TYPE_ANY = null; |
31
|
|
|
const TYPE_EMAIL = 1; |
32
|
|
|
const TYPE_COOKIE = 2; |
33
|
|
|
const TYPE_NONCE = 4; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
private $identityId; |
37
|
|
|
private $parentId; |
38
|
|
|
private $accountId; |
39
|
|
|
protected $type = self::TYPE_ANY; |
40
|
|
|
|
41
|
|
|
private $status; |
42
|
|
|
private $statusChangedOn; |
43
|
|
|
|
44
|
|
|
private $usedOn; |
45
|
|
|
private $expiresOn; |
46
|
|
|
|
47
|
|
|
private $token; |
48
|
|
|
private $tokenAction; |
49
|
|
|
private $tokenExpiresOn; |
50
|
|
|
private $tokenPayload; |
51
|
|
|
|
52
|
|
|
|
53
|
8 |
|
public function setId($identityId) |
54
|
|
|
{ |
55
|
8 |
|
$data = (int) $identityId; |
56
|
|
|
|
57
|
8 |
|
if ($data > 0) { |
58
|
3 |
|
$this->identityId = $data; |
59
|
|
|
} |
60
|
8 |
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @codeCoverageIgnore |
65
|
|
|
*/ |
66
|
|
|
public function getId() |
67
|
|
|
{ |
68
|
|
|
return $this->identityId; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
8 |
|
public function setParentId($parentId) |
73
|
|
|
{ |
74
|
8 |
|
$data = (int) $parentId; |
75
|
|
|
|
76
|
8 |
|
if ($data > 0) { |
77
|
3 |
|
$this->parentId = $data; |
78
|
|
|
} |
79
|
8 |
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @codeCoverageIgnore |
84
|
|
|
*/ |
85
|
|
|
public function getParentId() |
86
|
|
|
{ |
87
|
|
|
return $this->parentId; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
8 |
|
public function setAccountId($accountId) |
92
|
|
|
{ |
93
|
8 |
|
$data = (int) $accountId; |
94
|
|
|
|
95
|
8 |
|
if ($data > 0) { |
96
|
3 |
|
$this->accountId = $data; |
97
|
|
|
} |
98
|
8 |
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @codeCoverageIgnore |
103
|
|
|
*/ |
104
|
|
|
public function getAccountId() |
105
|
|
|
{ |
106
|
|
|
return $this->accountId; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @codeCoverageIgnore |
112
|
|
|
*/ |
113
|
|
|
public function getType() |
114
|
|
|
{ |
115
|
|
|
return $this->type; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
8 |
|
public function setExpiresOn($expiresOn) |
120
|
|
|
{ |
121
|
8 |
|
$data = (int) $expiresOn; |
122
|
|
|
|
123
|
8 |
|
if ($data > 0) { |
124
|
3 |
|
$this->expiresOn = $data; |
125
|
|
|
} |
126
|
8 |
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @codeCoverageIgnore |
131
|
|
|
*/ |
132
|
|
|
public function getExpiresOn() |
133
|
|
|
{ |
134
|
|
|
return $this->expiresOn; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
1 |
|
public function setStatus($status) |
139
|
|
|
{ |
140
|
1 |
|
if ($status !== $this->status) { |
141
|
1 |
|
$this->setStatusChangedOn(time()); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
$this->status = (int) $status; |
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @codeCoverageIgnore |
150
|
|
|
*/ |
151
|
|
|
public function getStatus() |
152
|
|
|
{ |
153
|
|
|
return $this->status; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
9 |
|
public function setStatusChangedOn($timestamp) |
158
|
|
|
{ |
159
|
9 |
|
$data = (int) $timestamp; |
160
|
|
|
|
161
|
9 |
|
if ($data > 0) { |
162
|
4 |
|
$this->statusChangedOn = $data; |
163
|
|
|
} |
164
|
9 |
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @codeCoverageIgnore |
169
|
|
|
*/ |
170
|
|
|
public function getStatusChangedOn() |
171
|
|
|
{ |
172
|
|
|
return $this->statusChangedOn; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
2 |
|
public function setToken($token) |
177
|
|
|
{ |
178
|
2 |
|
if ($token !== null && strlen($token) !== 2 * Identity::TOKEN_SIZE) { |
179
|
1 |
|
throw new InvalidToken; |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
$this->token = $token; |
183
|
1 |
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
2 |
|
public function generateToken() |
187
|
|
|
{ |
188
|
2 |
|
$this->token = bin2hex(random_bytes(Identity::TOKEN_SIZE)); |
189
|
2 |
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @codeCoverageIgnore |
194
|
|
|
*/ |
195
|
|
|
public function getToken() |
196
|
|
|
{ |
197
|
|
|
return $this->token; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
2 |
|
public function setTokenAction($tokenAction) |
202
|
|
|
{ |
203
|
2 |
|
$data = (int) $tokenAction; |
204
|
|
|
|
205
|
2 |
|
if ($data > 0) { |
206
|
2 |
|
$this->tokenAction = $data; |
207
|
2 |
|
return; |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
$this->tokenAction = null; |
211
|
1 |
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @codeCoverageIgnore |
216
|
|
|
*/ |
217
|
|
|
public function getTokenAction() |
218
|
|
|
{ |
219
|
|
|
return $this->tokenAction; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
8 |
|
public function setTokenEndOfLife($timestamp) |
224
|
|
|
{ |
225
|
8 |
|
$data = (int) $timestamp; |
226
|
|
|
|
227
|
8 |
|
if ($data > 0) { |
228
|
3 |
|
$this->tokenExpiresOn = $data; |
229
|
|
|
} |
230
|
8 |
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @codeCoverageIgnore |
235
|
|
|
*/ |
236
|
|
|
public function getTokenEndOfLife() |
237
|
|
|
{ |
238
|
|
|
return $this->tokenExpiresOn; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @codeCoverageIgnore |
244
|
|
|
*/ |
245
|
|
|
public function setTokenPayload(array $tokenPayload = null) |
246
|
|
|
{ |
247
|
|
|
$this->tokenPayload = $tokenPayload; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @codeCoverageIgnore |
253
|
|
|
*/ |
254
|
|
|
public function getTokenPayload() |
255
|
|
|
{ |
256
|
|
|
return $this->tokenPayload; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
1 |
|
public function clearToken() |
261
|
|
|
{ |
262
|
1 |
|
$this->token = null; |
263
|
1 |
|
$this->tokenAction = Identity::ACTION_NONE; |
264
|
1 |
|
$this->tokenExpiresOn = null; |
265
|
1 |
|
$this->tokenPayload = null; |
266
|
1 |
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
8 |
|
public function setLastUsed($timestamp) |
270
|
|
|
{ |
271
|
8 |
|
$data = (int) $timestamp; |
272
|
|
|
|
273
|
8 |
|
if ($data > 0) { |
274
|
3 |
|
$this->usedOn = $data; |
275
|
|
|
} |
276
|
8 |
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @codeCoverageIgnore |
281
|
|
|
*/ |
282
|
|
|
public function getLastUsed() |
283
|
|
|
{ |
284
|
|
|
return $this->usedOn; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|