/
ConfirmationMiddleware.php
303 lines (266 loc) · 7.96 KB
/
ConfirmationMiddleware.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
namespace SilverStripe\Control\Middleware;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\Session;
use SilverStripe\Security\Confirmation;
use SilverStripe\Security\Security;
/**
* Checks whether user manual confirmation is required for HTTPRequest
* depending on the rules given.
*
* How it works:
* - Gives the request to every single rule
* - If no confirmation items are found by the rules, then move on to the next middleware
* - initialize the Confirmation\Storage with all the confirmation items found
* - Check whether the storage has them confirmed already and if yes, move on to the next middleware
* - Otherwise redirect to the confirmation URL
*/
class ConfirmationMiddleware implements HTTPMiddleware
{
/**
* The confirmation storage identifier
*
* @var string
*/
protected $confirmationId = 'middleware';
/**
* Confirmation form URL
* WARNING: excluding SS_BASE_URL
*
* @var string
*/
protected $confirmationFormUrl = '/dev/confirm';
/**
* The list of rules to check requests against
*
* @var ConfirmationMiddleware\Rule[]
*/
protected $rules;
/**
* The list of bypasses
*
* @var ConfirmationMiddleware\Bypass[]
*/
protected $bypasses = [];
/**
* Where user should be redirected when refusing
* the action on the confirmation form
*
* @var string
*/
private $declineUrl;
/**
* Init the middleware with the rules
*
* @param ConfirmationMiddleware\Rule[] $rules Rules to check requests against
*/
public function __construct(...$rules)
{
$this->rules = $rules;
$this->declineUrl = Director::baseURL();
}
/**
* The URL of the confirmation form ("Security/confirm/middleware" by default)
*
* @param HTTPRequest $request Active request
* @param string $confirmationStorageId ID of the confirmation storage to be used
*
* @return string URL of the confirmation form
*/
protected function getConfirmationUrl(HTTPRequest $request, $confirmationStorageId)
{
$url = $this->confirmationFormUrl;
if (substr($url ?? '', 0, 1) === '/') {
// add BASE_URL explicitly if not absolute
$url = Controller::join_links(Director::baseURL(), $url);
}
return Controller::join_links(
$url,
urlencode($confirmationStorageId ?? '')
);
}
/**
* Returns the URL where the user to be redirected
* when declining the action (on the confirmation form)
*
* @param HTTPRequest $request Active request
*
* @return string URL
*/
protected function generateDeclineUrlForRequest(HTTPRequest $request)
{
return $this->declineUrl;
}
/**
* Override the default decline url
*
* @param string $url
*
* @return $this
*/
public function setDeclineUrl($url)
{
$this->declineUrl = $url;
return $this;
}
/**
* Check whether the rules can be bypassed
* without user confirmation
*
* @param HTTPRequest $request
*
* @return bool
*/
public function canBypass(HTTPRequest $request)
{
foreach ($this->bypasses as $bypass) {
if ($bypass->checkRequestForBypass($request)) {
return true;
}
}
return false;
}
/**
* Extract the confirmation items from the request and return
*
* @param HTTPRequest $request
*
* @return Confirmation\Item[] list of confirmation items
*/
public function getConfirmationItems(HTTPRequest $request)
{
$confirmationItems = [];
foreach ($this->rules as $rule) {
if ($item = $rule->getRequestConfirmationItem($request)) {
$confirmationItems[] = $item;
}
}
return $confirmationItems;
}
/**
* Initialize the confirmation session storage
* with the confirmation items and return an HTTPResponse
* redirecting to the according confirmation form.
*
* @param HTTPRequest $request
* @param Confirmation\Storage $storage
* @param Confirmation\Item[] $confirmationItems
*
* @return HTTPResponse
*/
protected function buildConfirmationRedirect(HTTPRequest $request, Confirmation\Storage $storage, array $confirmationItems)
{
$storage->cleanup();
foreach ($confirmationItems as $item) {
$storage->putItem($item);
}
$storage->setSuccessRequest($request);
$storage->setFailureUrl($this->generateDeclineUrlForRequest($request));
$result = new HTTPResponse();
$result->redirect($this->getConfirmationUrl($request, $this->confirmationId));
return $result;
}
/**
* Process the confirmation items and either perform the confirmedEffect
* and pass the request to the next middleware, or return a redirect to
* the confirmation form
*
* @param HTTPRequest $request
* @param callable $delegate
* @param Confirmation\Item[] $items
*
* @return HTTPResponse
*/
protected function processItems(HTTPRequest $request, callable $delegate, $items)
{
$storage = Injector::inst()->createWithArgs(Confirmation\Storage::class, [$request->getSession(), $this->confirmationId, false]);
if (!count($storage->getItems() ?? [])) {
return $this->buildConfirmationRedirect($request, $storage, $items);
}
$confirmed = false;
if ($storage->getHttpMethod() === 'POST') {
$postVars = $request->postVars();
$csrfToken = $storage->getCsrfToken();
$confirmed = $storage->confirm($postVars) && isset($postVars[$csrfToken]);
} else {
$confirmed = $storage->check($items);
}
if (!$confirmed) {
return $this->buildConfirmationRedirect($request, $storage, $items);
}
if ($response = $this->confirmedEffect($request)) {
return $response;
}
$storage->cleanup();
return $delegate($request);
}
/**
* The middleware own effects that should be performed on confirmation
*
* This method is getting called before the confirmation storage cleanup
* so that any responses returned here don't trigger a new confirmtation
* for the same request traits
*
* @param HTTPRequest $request
*
* @return null|HTTPResponse
*/
protected function confirmedEffect(HTTPRequest $request)
{
return null;
}
public function process(HTTPRequest $request, callable $delegate)
{
if ($this->canBypass($request)) {
if ($response = $this->confirmedEffect($request)) {
return $response;
} else {
return $delegate($request);
}
}
if (!$items = $this->getConfirmationItems($request)) {
return $delegate($request);
}
return $this->processItems($request, $delegate, $items);
}
/**
* Override the confirmation storage ID
*
* @param string $id
*
* @return $this
*/
public function setConfirmationStorageId($id)
{
$this->confirmationId = $id;
return $this;
}
/**
* Override the confirmation form url
*
* @param string $url
*
* @return $this
*/
public function setConfirmationFormUrl($url)
{
$this->confirmationFormUrl = $url;
return $this;
}
/**
* Set the list of bypasses for the confirmation
*
* @param ConfirmationMiddleware\Bypass[] $bypasses
*
* @return $this
*/
public function setBypasses($bypasses)
{
$this->bypasses = $bypasses;
return $this;
}
}