|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SilverStripe\MFA\Tests\Stub\BasicMath; |
|
6
|
|
|
|
|
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
9
|
|
|
use SilverStripe\Dev\TestOnly; |
|
10
|
|
|
use SilverStripe\MFA\Method\Handler\VerifyHandlerInterface; |
|
11
|
|
|
use SilverStripe\MFA\Model\RegisteredMethod; |
|
12
|
|
|
use SilverStripe\MFA\State\Result; |
|
13
|
|
|
use SilverStripe\MFA\Store\StoreInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handles login attempts for the Math Method |
|
17
|
|
|
*/ |
|
18
|
|
|
class MethodVerifyHandler implements VerifyHandlerInterface, TestOnly |
|
19
|
|
|
{ |
|
20
|
|
|
use Configurable; |
|
21
|
|
|
|
|
22
|
|
|
private static $number_of_numbers = 2; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Prepare this authentication method to verify a member by initialising state in session and generating details to |
|
26
|
|
|
* provide to a frontend React component |
|
27
|
|
|
* |
|
28
|
|
|
* @param StoreInterface $store An object that hold session data (and the Member) that can be mutated |
|
29
|
|
|
* @return array Props to be passed to a front-end React component |
|
30
|
|
|
*/ |
|
31
|
|
|
public function start(StoreInterface $store, RegisteredMethod $registeredMethod): array |
|
32
|
|
|
{ |
|
33
|
|
|
$numbers = []; |
|
34
|
|
|
|
|
35
|
|
|
$numberOfNumbers = $this->config()->get('number_of_numbers') ?: 2; |
|
36
|
|
|
|
|
37
|
|
|
for ($i = 0; $i < $numberOfNumbers; $i++) { |
|
38
|
|
|
$numbers[] = rand(1, 9); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$store->setState([ |
|
42
|
|
|
'answer' => array_sum($numbers), |
|
43
|
|
|
]); |
|
44
|
|
|
|
|
45
|
|
|
return [ |
|
46
|
|
|
'numbers' => $numbers, |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Verify the request has provided the right information to verify the member that aligns with any sessions state |
|
52
|
|
|
* that may have been set prior |
|
53
|
|
|
* |
|
54
|
|
|
* @param HTTPRequest $request |
|
55
|
|
|
* @param StoreInterface $store |
|
56
|
|
|
* @return Result |
|
57
|
|
|
*/ |
|
58
|
|
|
public function verify(HTTPRequest $request, StoreInterface $store, RegisteredMethod $registeredMethod): Result |
|
59
|
|
|
{ |
|
60
|
|
|
$body = json_decode($request->getBody(), true); |
|
61
|
|
|
|
|
62
|
|
|
if (!$body['answer']) { |
|
63
|
|
|
return Result::create(false, 'Answer was missing'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$state = $store->getState(); |
|
67
|
|
|
$hashComparison = hash_equals((string)$state['answer'], (string)$body['answer']); |
|
68
|
|
|
if (!$hashComparison) { |
|
69
|
|
|
return Result::create(false, 'Answer was wrong'); |
|
70
|
|
|
} |
|
71
|
|
|
return Result::create(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the key that a React UI component is registered under (with @silverstripe/react-injector on the front-end) |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getComponent(): string |
|
80
|
|
|
{ |
|
81
|
|
|
// This component does not exist |
|
82
|
|
|
return 'BasicMathLogin'; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|