MemberExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeWrite() 0 8 3
A generateOTPAuthString() 0 12 1
A GoogleAuthenticatorQRCode() 0 8 1
A updateCMSFields() 0 14 3
1
<?php
2
3
namespace ElliotSawyer\TOTPAuthenticator;
4
5
use Endroid\QrCode\Exception\InvalidWriterException;
6
use Endroid\QrCode\QrCode;
7
use lfkeitel\phptotp\Base32;
8
use lfkeitel\phptotp\Totp;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\LiteralField;
11
use SilverStripe\Forms\ToggleCompositeField;
12
use SilverStripe\ORM\DataExtension;
13
use SilverStripe\SiteConfig\SiteConfig;
14
15
/**
16
 * Class MemberExtension
17
 *
18
 * @package ElliotSawyer\TOTPAuthenticator
19
 * @property MemberExtension $owner
20
 * @property string $TOTPSecret
21
 */
22
class MemberExtension extends DataExtension
23
{
24
    /**
25
     * @var array
26
     */
27
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
28
        'TOTPSecret' => 'Varchar(1024)',
29
    ];
30
31
    /**
32
     * @throws \Exception
33
     */
34
    public function onBeforeWrite()
35
    {
36
        // Only regenerate if there is no secret and MFA is not enabled yet
37
        // Inherits MFAEnabled from Bootstrap object extension
38
        if (!$this->owner->TOTPSecret || !$this->owner->MFAEnabled) {
0 ignored issues
show
Bug Best Practice introduced by
The property MFAEnabled does not exist on ElliotSawyer\TOTPAuthenticator\MemberExtension. Did you maybe forget to declare it?
Loading history...
39
            $secret = Totp::GenerateSecret(16);
40
            $secret = Base32::encode($secret);
41
            $this->owner->TOTPSecret = $secret;
42
        }
43
    }
44
45
    /**
46
     * @param FieldList $fields
47
     * @throws InvalidWriterException
48
     */
49
    public function updateCMSFields(FieldList $fields)
50
    {
51
        if (!$this->owner->exists()) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on ElliotSawyer\TOTPAuthenticator\MemberExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        if (!$this->owner->/** @scrutinizer ignore-call */ exists()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
            $fields->removeByName('TOTPSecret');
53
        }
54
55
        if (strlen($this->owner->TOTPSecret)) {
56
            $qrcodeURI = $this->GoogleAuthenticatorQRCode();
57
            $fields->addFieldToTab('Root.Main', ToggleCompositeField::create(
58
                null,
59
                _t(self::class . '.CMSTOGGLEQRCODELABEL', 'Second Factor Token Secret'),
60
                LiteralField::create(null, sprintf("<img src=\"%s\" />", $qrcodeURI))
61
            ));
62
            $fields->removeByName('TOTPSecret');
63
        }
64
    }
65
66
    /**
67
     * @return string
68
     * @throws InvalidWriterException
69
     */
70
    public function GoogleAuthenticatorQRCode()
71
    {
72
        $qrCode = new QrCode($this->generateOTPAuthString());
73
        $qrCode->setSize(300);
74
        $qrCode->setWriterByName('png');
75
        $qrcodeURI = $qrCode->writeDataUri();
76
77
        return $qrcodeURI;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function generateOTPAuthString()
84
    {
85
        $label = urlencode(SiteConfig::current_site_config()->Title);
86
        $secret = $this->owner->TOTPSecret;
87
        $email = $this->owner->Email;
0 ignored issues
show
Bug Best Practice introduced by
The property Email does not exist on ElliotSawyer\TOTPAuthenticator\MemberExtension. Did you maybe forget to declare it?
Loading history...
88
89
        return sprintf(
90
            'otpauth://totp/%s:%s?secret=%s&issuer=%s',
91
            $label,
92
            $email,
93
            $secret,
94
            $label
95
        );
96
    }
97
}
98