TOTPProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A verifyToken() 0 27 6
1
<?php
2
3
4
namespace ElliotSawyer\TOTPAuthenticator;
5
6
use Firesphere\BootstrapMFA\Providers\BootstrapMFAProvider;
7
use Firesphere\BootstrapMFA\Providers\MFAProvider;
8
use lfkeitel\phptotp\Base32;
9
use lfkeitel\phptotp\Totp;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\ORM\ValidationException;
12
use SilverStripe\ORM\ValidationResult;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\PasswordEncryptor_NotFoundException;
15
use ElliotSawyer\TOTPAuthenticator\TOTPAuthenticator;
16
17
/**
18
 * Class TOTPProvider
19
 * @package ElliotSawyer\TOTPAuthenticator
20
 */
21
class TOTPProvider extends BootstrapMFAProvider implements MFAProvider
22
{
23
    /**
24
     * @param string $token
25
     * @param null $result
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $result is correct as it would always require null to be passed?
Loading history...
26
     * @return bool|Member
27
     * @throws ValidationException
28
     * @throws PasswordEncryptor_NotFoundException
29
     * @throws \Exception
30
     */
31
    public function verifyToken($token, &$result = null)
32
    {
33
        if (!$result) {
0 ignored issues
show
introduced by
$result is of type null, thus it always evaluated to false.
Loading history...
34
            $result = Injector::inst()->get(ValidationResult::class);
35
        }
36
        $member = $this->getMember();
37
        if ($member && $member->ID) {
38
            if (!$token) {
39
                $result->addError(_t(self::class . '.INVALIDORMISSINGTOKEN', 'Invalid or missing second factor token'));
40
            } else {
41
                $secret = Base32::decode($member->TOTPSecret);
42
                $algorithm = TOTPAuthenticator::get_algorithm();
43
44
                $totp = new Totp($algorithm);
45
                $key = $totp->GenerateToken($secret);
46
                $user_submitted_key = $token;
47
                if ($user_submitted_key !== $key) {
48
                    $result->addError(
49
                        _t(self::class . '.INVALIDORMISSINGTOKEN', 'Invalid or missing second factor token')
50
                    );
51
                } else {
52
                    return $this->member;
53
                }
54
            }
55
        }
56
57
        return parent::verifyToken($token, $result);
58
    }
59
}
60