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