1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\MFA\Controller; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Admin\LeftAndMain; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Control\HTTPResponse; |
8
|
|
|
use SilverStripe\MFA\RequestHandler\BaseHandlerTrait; |
9
|
|
|
use SilverStripe\MFA\RequestHandler\RegistrationHandlerTrait; |
10
|
|
|
use SilverStripe\MFA\Service\MethodRegistry; |
11
|
|
|
use SilverStripe\Security\Security; |
12
|
|
|
|
13
|
|
|
class AdminRegistrationController extends LeftAndMain |
14
|
|
|
{ |
15
|
|
|
use RegistrationHandlerTrait; |
16
|
|
|
use BaseHandlerTrait; |
17
|
|
|
|
18
|
|
|
private static $url_segment = 'mfa'; |
|
|
|
|
19
|
|
|
|
20
|
|
|
private static $url_handlers = [ |
|
|
|
|
21
|
|
|
'GET register/$Method' => 'startRegistration', |
22
|
|
|
'POST register/$Method' => 'finishRegistration', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
private static $allowed_actions = [ |
|
|
|
|
26
|
|
|
'startRegistration', |
27
|
|
|
'finishRegistration', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
public function startRegistration(HTTPRequest $request): HTTPResponse |
31
|
|
|
{ |
32
|
|
|
// Create a fresh store from the current logged in user |
33
|
|
|
$member = Security::getCurrentUser(); |
34
|
|
|
$store = $this->createStore($member); |
35
|
|
|
|
36
|
|
|
// Get the specified method |
37
|
|
|
$method = MethodRegistry::singleton()->getMethodByURLSegment($request->param('Method')); |
38
|
|
|
|
39
|
|
|
if (!$method) { |
40
|
|
|
return $this->jsonResponse( |
41
|
|
|
['errors' => [_t(__CLASS__ . '.INVALID_METHOD', 'No such method is available')]], |
42
|
|
|
400 |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$response = $this->createStartRegistrationResponse($store, $method); |
47
|
|
|
$store->save($request); |
48
|
|
|
|
49
|
|
|
return $response; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function finishRegistration(HTTPRequest $request): HTTPResponse |
53
|
|
|
{ |
54
|
|
|
$store = $this->getStore(); |
55
|
|
|
|
56
|
|
|
if (!$store) { |
|
|
|
|
57
|
|
|
return $this->jsonResponse( |
58
|
|
|
['errors' => [_t(__CLASS__ . '.INVALID_SESSION', 'Invalid session. Please try again')]], |
59
|
|
|
400 |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$method = MethodRegistry::singleton()->getMethodByURLSegment($request->param('Method')); |
64
|
|
|
|
65
|
|
|
if (!$method) { |
66
|
|
|
return $this->jsonResponse( |
67
|
|
|
['errors' => [_t(__CLASS__ . '.INVALID_METHOD', 'No such method is available')]], |
68
|
|
|
400 |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$result = $this->completeRegistrationRequest($store, $method, $request); |
73
|
|
|
|
74
|
|
|
if (!$result->isSuccessful()) { |
75
|
|
|
return $this->jsonResponse(['errors' => [$result->getMessage()]], 400); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$store::clear($request); |
79
|
|
|
|
80
|
|
|
return $this->jsonResponse(['success' => true], 201); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Respond with the given array as a JSON response |
85
|
|
|
* |
86
|
|
|
* @param array $response |
87
|
|
|
* @param int $code The HTTP response code to set on the response |
88
|
|
|
* @return HTTPResponse |
89
|
|
|
*/ |
90
|
|
|
protected function jsonResponse(array $response, int $code = 200): HTTPResponse |
91
|
|
|
{ |
92
|
|
|
return HTTPResponse::create(json_encode($response)) |
93
|
|
|
->addHeader('Content-Type', 'application/json') |
94
|
|
|
->setStatusCode($code); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|