Completed
Push — master ( bade18...269a91 )
by Robbie
11s
created

MethodAuthenticator::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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