1<?php
2
3use dokuwiki\Form\Form;
4use dokuwiki\plugin\twofactor\OtpField;
5use dokuwiki\plugin\twofactor\Provider;
6use dokuwiki\plugin\twofactorgoogleauth\QRCode;
7
8/**
9 * Twofactor Provider for TOTP aka Google Authenticator
10 */
11class action_plugin_twofactorgoogleauth extends Provider
12{
13
14    /** @inheritDoc */
15    public function isConfigured()
16    {
17        return $this->settings->get('verified');
18    }
19
20    /** @inheritdoc */
21    public function getLabel()
22    {
23        return 'Google Authenticator (TOTP)';
24    }
25
26    /** @inheritdoc */
27    public function renderProfileForm(Form $form)
28    {
29        global $conf;
30        global $USERINFO;
31
32        $secret = $this->getSecret();
33        $name = $USERINFO['name'] . '@' . $conf['title'];
34        $url = 'otpauth://totp/' . rawurlencode($name) . '?secret=' . $secret;
35        $svg = QRCode::svg($url);
36
37        $form->addHTML('<figure><p>' . $this->getLang('directions') . '</p>');
38        $form->addHTML($svg);
39        $form->addHTML('<figcaption><code>'.$secret.'</code></figcaption>');
40        $form->addHTML('</figure>');
41
42        $form->addHTML('<p>' . $this->getLang('verifynotice') . '</p>');
43        $form->addElement(new OtpField('googleauth_verify'));
44
45        return $form;
46    }
47
48    /** @inheritdoc */
49    public function handleProfileForm()
50    {
51        global $INPUT;
52
53        // create secret when setup is initialized
54        if ($INPUT->bool('init')) {
55            $this->initSecret();
56        }
57
58        $otp = $INPUT->str('googleauth_verify');
59        if (!$otp) return;
60
61        if ($this->checkCode($otp)) {
62            $this->settings->set('verified', true);
63        }
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function transmitMessage($code)
70    {
71        return $this->getLang('verifymodule');
72    }
73}
74