Passed
Pull Request — master (#3)
by Robbie
02:02
created

MethodAuthenticationHandler::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
namespace SilverStripe\MFA\BasicMath;
3
4
use SilverStripe\Control\HTTPRequest;
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\MFA\Method\Handler\LoginHandlerInterface;
7
use SilverStripe\MFA\Store\StoreInterface;
8
9
/**
10
 * Handles login attempts for the Math Method
11
 */
12
class MethodAuthenticationHandler implements LoginHandlerInterface
13
{
14
    use Configurable;
15
16
    private static $number_of_numbers = 2;
0 ignored issues
show
introduced by
The private property $number_of_numbers is not used, and could be removed.
Loading history...
17
18
    /**
19
     * Prepare this authentication method to verify a member by initialising state in session and generating details to
20
     * provide to a frontend React component
21
     *
22
     * @param StoreInterface $store An object that hold session data (and the Member) that can be mutated
23
     * @return array Props to be passed to a front-end React component
24
     */
25
    public function start(StoreInterface $store)
26
    {
27
        $numbers = [];
28
29
        for ($i = 0; $i < static::config()->get('number_of_numbers'); $i++) {
30
            $numbers[] = rand(1, 9);
31
        }
32
33
        $store->setState([
34
            'answer' => array_sum($numbers),
35
        ]);
36
37
        return [
38
            'numbers' => $numbers,
39
        ];
40
    }
41
42
    /**
43
     * Verify the request has provided the right information to verify the member that aligns with any sessions state
44
     * that may have been set prior
45
     *
46
     * @param HTTPRequest $request
47
     * @param StoreInterface $store
48
     * @return bool
49
     */
50
    public function verify(HTTPRequest $request, StoreInterface $store)
51
    {
52
        $state = $store->getState();
53
        return hash_equals((string)$state['answer'], (string)$request->param('answer'));
54
    }
55
56
    /**
57
     * Provide a string (possibly passed through i18n) that serves as a lead in for choosing this option for
58
     * authentication
59
     *
60
     * eg. "Enter one of your recovery codes"
61
     *
62
     * @return string
63
     */
64
    public function getLeadInLabel()
65
    {
66
        return 'Verify by solving a complex math problem';
67
    }
68
}
69