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
![]() |
|||||
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
|
|||||
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
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
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. ![]() |
|||||
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
|
|||||
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 |