|
1
|
|
|
<?php |
|
2
|
|
|
namespace SilverStripe\MFA\Store; |
|
3
|
|
|
|
|
4
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
5
|
|
|
use SilverStripe\MFA\Extension\MemberExtension; |
|
6
|
|
|
use SilverStripe\Security\Member; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* This class provides an interface to store data in session during an MFA process. This is implemented as a measure to |
|
10
|
|
|
* prevent bleeding state between individual MFA auth types |
|
11
|
|
|
* |
|
12
|
|
|
* @package SilverStripe\MFA |
|
13
|
|
|
*/ |
|
14
|
|
|
class SessionStore implements StoreInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const SESSION_KEY = 'MFASessionStore'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The member that is currently going through the MFA process |
|
20
|
|
|
* |
|
21
|
|
|
* @var Member |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $member; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A string representing the current authentication method that is underway |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $method; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Any state that the current authentication method needs to retain while it is underway |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $state = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a store from the given request getting any initial state from the session of the request |
|
41
|
|
|
* |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function create(HTTPRequest $request) |
|
45
|
|
|
{ |
|
46
|
|
|
$state = $request->getSession()->get(static::SESSION_KEY); |
|
47
|
|
|
|
|
48
|
|
|
$new = new static(); |
|
49
|
|
|
|
|
50
|
|
|
if ($state) { |
|
51
|
|
|
$new->setMethod($state['method']); |
|
52
|
|
|
$new->setState($state['state']); |
|
53
|
|
|
if ($state['member']) { |
|
54
|
|
|
$new->setMember(Member::get_by_id($state['member'])); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $new; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return Member|MemberExtension |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getMember() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->member; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param Member $member |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setMember(Member $member) |
|
74
|
|
|
{ |
|
75
|
|
|
// Early return if there's no change |
|
76
|
|
|
if ($this->member && $this->member->ID === $member->ID) { |
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// If the member has changed we should null out the method that's underway and the state of it |
|
81
|
|
|
$this->resetMethod(); |
|
82
|
|
|
|
|
83
|
|
|
$this->member = $member; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getMethod() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->method; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $method |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setMethod($method) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->method = $method; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getState() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->state; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function setState(array $state) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->state = $state; |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Save this store into the session of the given request |
|
121
|
|
|
* |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function save(HTTPRequest $request) |
|
125
|
|
|
{ |
|
126
|
|
|
$request->getSession()->set(static::SESSION_KEY, $this->build()); |
|
127
|
|
|
|
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Clear any stored values for the given request |
|
133
|
|
|
* |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function clear(HTTPRequest $request) |
|
137
|
|
|
{ |
|
138
|
|
|
$request->getSession()->clear(static::SESSION_KEY); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
protected function resetMethod() |
|
142
|
|
|
{ |
|
143
|
|
|
$this->setMethod(null)->setState([]); |
|
144
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function build() |
|
149
|
|
|
{ |
|
150
|
|
|
return [ |
|
151
|
|
|
'member' => $this->getMember() ? $this->getMember()->ID : null, |
|
152
|
|
|
'method' => $this->getMethod(), |
|
153
|
|
|
'state' => $this->getState(), |
|
154
|
|
|
]; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|