/
SessionStore.php
266 lines (227 loc) · 6.84 KB
/
SessionStore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
declare(strict_types=1);
namespace SilverStripe\MFA\Store;
use RuntimeException;
use Serializable;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\MFA\Exception\InvalidMethodException;
use SilverStripe\MFA\Extension\MemberExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member;
use SilverStripe\Dev\Deprecation;
/**
* This class provides an interface to store data in session during an MFA process. This is implemented as a measure to
* prevent bleeding state between individual MFA auth types
*
* @package SilverStripe\MFA
*/
class SessionStore implements StoreInterface, Serializable
{
public const SESSION_KEY = 'MFASessionStore';
/**
* The member that is currently going through the MFA process
*
* @var Member
*/
protected $member;
/**
* MemberID is only used on unserialising from session as we can defer the DB call for the member
*
* @var int
*/
protected $memberID;
/**
* A string representing the current authentication method that is underway
*
* @var string
*/
protected $method;
/**
* Any state that the current authentication method needs to retain while it is underway
*
* @var array
*/
protected $state = [];
/**
* The URL segment identifiers of methods that have been verified in this session
*
* @var string[]
*/
protected $verifiedMethods = [];
/**
* Attempt to create a store from the given request getting any existing state from the session of the request
*
* {@inheritdoc}
*/
public function __construct(Member $member)
{
$this->setMember($member);
}
/**
* @return Member&MemberExtension|null
*/
public function getMember(): ?Member
{
if (!$this->member && $this->memberID) {
$this->member = DataObject::get_by_id(Member::class, $this->memberID);
}
return $this->member;
}
/**
* @param Member $member
* @return $this
*/
public function setMember(Member $member): StoreInterface
{
// Early return if there's no change
if ($this->member && $this->member->ID === $member->ID) {
return $this;
}
// If the member has changed we should null out the method that's underway and the state of it
$this->resetMethod();
$this->member = $member;
$this->memberID = $member->ID;
// When the member changes the list of verified methods should reset
$this->verifiedMethods = [];
return $this;
}
/**
* @return string|null
*/
public function getMethod(): ?string
{
return $this->method;
}
/**
* @param string|null $method
* @return $this
*/
public function setMethod(?string $method): StoreInterface
{
if (in_array($method, $this->getVerifiedMethods() ?? [])) {
throw new InvalidMethodException('You cannot verify with a method you have already verified');
}
$this->method = $method;
return $this;
}
public function getState(): array
{
return $this->state;
}
public function setState(array $state): StoreInterface
{
$this->state = $state;
return $this;
}
public function addState(array $state): StoreInterface
{
$this->state = array_merge($this->state, $state);
return $this;
}
public function addVerifiedMethod(string $method): StoreInterface
{
if (!in_array($method, $this->verifiedMethods ?? [])) {
$this->verifiedMethods[] = $method;
}
return $this;
}
public function getVerifiedMethods(): array
{
return $this->verifiedMethods;
}
/**
* Save this store into the session of the given request
*
* {@inheritdoc}
*/
public function save(HTTPRequest $request): StoreInterface
{
$request->getSession()->set(static::SESSION_KEY, $this);
return $this;
}
/**
* Load a StoreInterface from the given request and return it if it exists
*
* @param HTTPRequest $request
* @return StoreInterface|null
*/
public static function load(HTTPRequest $request): ?StoreInterface
{
$store = $request->getSession()->get(static::SESSION_KEY);
return $store instanceof self ? $store : null;
}
/**
* Clear any stored values for the given request
*
* {@inheritdoc}
*/
public static function clear(HTTPRequest $request): void
{
$request->getSession()->clear(static::SESSION_KEY);
}
/**
* "Reset" the method currently in progress by clearing the identifier and state
*
* @return StoreInterface
*/
protected function resetMethod(): StoreInterface
{
$this->setMethod(null)->setState([]);
return $this;
}
public function __serialize(): array
{
// Use the stored member ID by default.
// We should do this because we can avoid ever fetching the member object
// from the database if the member was never accessed during this request.
$memberID = $this->memberID;
if (!$memberID && $this->getMember()) {
$memberID = $this->getMember()->ID;
}
return [
'member' => $memberID,
'method' => $this->getMethod(),
'state' => $this->getState(),
'verifiedMethods' => $this->getVerifiedMethods(),
];
}
public function __unserialize(array $data): void
{
$this->memberID = $data['member'];
$this->setMethod($data['method']);
$this->setState($data['state']);
foreach ($data['verifiedMethods'] as $method) {
$this->addVerifiedMethod($method);
}
}
/**
* The __serialize() magic method will be automatically used instead of this
*
* @return string
* @deprecated 4.7.0 Use __serialize() instead
*/
public function serialize(): string
{
Deprecation::notice('4.7.0', 'Use __serialize() instead');
$data = $this->__serialize();
$str = json_encode($data);
if (!$str) {
throw new RuntimeException(json_last_error_msg());
}
return $str;
}
/**
* The __unserialize() magic method will be automatically used instead of this almost all the time
* This method will be automatically used if existing serialized data was not saved as an associative array
* and the PHP version used in less than PHP 9.0
*
* @param string $serialized
* @deprecated 4.7.0 Use __unserialize() instead
*/
public function unserialize($serialized): void
{
Deprecation::notice('4.7.0', 'Use __unserialize() instead');
$data = json_decode($serialized ?? '', true);
$this->__unserialize($data);
}
}