Passed
Pull Request — master (#28)
by Simon
02:32
created

MockAuthenticator::verifyMFA()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 4
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\BootstrapMFA\Tests\Mocks;
4
5
6
use Firesphere\BootstrapMFA\Authenticators\BootstrapMFAAuthenticator;
7
use Firesphere\BootstrapMFA\Forms\BootstrapMFALoginForm;
8
use Firesphere\BootstrapMFA\Handlers\BootstrapMFALoginHandler;
9
use Firesphere\BootstrapMFA\Interfaces\MFAAuthenticator;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Dev\TestOnly;
12
use SilverStripe\Forms\FieldList;
13
use SilverStripe\Forms\FormAction;
14
use SilverStripe\Forms\TextField;
15
use SilverStripe\ORM\ValidationResult;
16
use SilverStripe\Security\Member;
17
18
class MockAuthenticator extends BootstrapMFAAuthenticator implements TestOnly, MFAAuthenticator
19
{
20
21
    /**
22
     * Get the MFA form
23
     *
24
     * @param BootstrapMFALoginHandler $controller
25
     * @param string $name
26
     * @return BootstrapMFALoginForm
27
     */
28
    public function getMFAForm($controller, $name)
29
    {
30
        $fields = FieldList::create([
31
            TextField::create('token', 'Token')
32
        ]);
33
        $actions = FieldList::create([
34
            FormAction::create('verifyMFA', 'Verify')
35
        ]);
36
37
        $form = BootstrapMFALoginForm::create($controller, $name, $fields, $actions);
38
        $form->setAuthenticatorClass(self::class);
39
40
        return $form;
41
    }
42
43
    /**
44
     * Verify the MFA code. Or just fake it
45
     *
46
     * @param array $data
47
     * @param HTTPRequest $request
48
     * @param string $token
49
     * @param ValidationResult $result
50
     * @return mixed
51
     */
52
    public function verifyMFA($data, $request, $token, &$result)
53
    {
54
        if (!$result) {
0 ignored issues
show
introduced by
$result is of type SilverStripe\ORM\ValidationResult, thus it always evaluated to true.
Loading history...
55
            $result = ValidationResult::create();
56
        }
57
        if ($token === 'success') {
58
            $id = $request->getSession()->get(BootstrapMFAAuthenticator::SESSION_KEY . '.MemberID');
59
            return Member::get()->byID($id);
60
        } else {
61
            $result->addError('This is not correct');
62
            return false;
63
        }
64
    }
65
66
    public function getTokenField()
67
    {
68
        return 'token';
69
    }
70
}