]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Verify.php
Update copyright
[friendica.git] / src / Module / Settings / TwoFactor / Verify.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings\TwoFactor;
23
24 use BaconQrCode\Renderer\Image\SvgImageBackEnd;
25 use BaconQrCode\Renderer\ImageRenderer;
26 use BaconQrCode\Renderer\RendererStyle\RendererStyle;
27 use BaconQrCode\Writer;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session;
30 use Friendica\DI;
31 use Friendica\Module\BaseSettings;
32 use Friendica\Module\Security\Login;
33 use PragmaRX\Google2FA\Google2FA;
34
35 /**
36  * // Page 4: 2FA enabled but not verified, QR code and verification
37  *
38  * @package Friendica\Module\TwoFactor\Settings
39  */
40 class Verify extends BaseSettings
41 {
42         public static function init(array $parameters = [])
43         {
44                 if (!local_user()) {
45                         return;
46                 }
47
48                 $secret = DI::pConfig()->get(local_user(), '2fa', 'secret');
49                 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
50
51                 if ($secret && $verified) {
52                         DI::baseUrl()->redirect('settings/2fa');
53                 }
54
55                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
56                         notice(DI::l10n()->t('Please enter your password to access this page.'));
57                         DI::baseUrl()->redirect('settings/2fa');
58                 }
59         }
60
61         public static function post(array $parameters = [])
62         {
63                 if (!local_user()) {
64                         return;
65                 }
66
67                 if (($_POST['action'] ?? '') == 'verify') {
68                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/verify', 'settings_2fa_verify');
69
70                         $google2fa = new Google2FA();
71
72                         $valid = $google2fa->verifyKey(DI::pConfig()->get(local_user(), '2fa', 'secret'), $_POST['verify_code'] ?? '');
73
74                         if ($valid) {
75                                 DI::pConfig()->set(local_user(), '2fa', 'verified', true);
76                                 Session::set('2fa', true);
77
78                                 info(DI::l10n()->t('Two-factor authentication successfully activated.'));
79
80                                 DI::baseUrl()->redirect('settings/2fa');
81                         } else {
82                                 notice(DI::l10n()->t('Invalid code, please retry.'));
83                         }
84                 }
85         }
86
87         public static function content(array $parameters = [])
88         {
89                 if (!local_user()) {
90                         return Login::form('settings/2fa/verify');
91                 }
92
93                 parent::content($parameters);
94
95                 $company = 'Friendica';
96                 $holder = Session::get('my_address');
97                 $secret = DI::pConfig()->get(local_user(), '2fa', 'secret');
98
99                 $otpauthUrl = (new Google2FA())->getQRCodeUrl($company, $holder, $secret);
100
101                 $renderer = (new \BaconQrCode\Renderer\Image\Svg())
102                         ->setHeight(256)
103                         ->setWidth(256);
104
105                 $writer = new Writer($renderer);
106
107                 $qrcode_image = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $writer->writeString($otpauthUrl));
108
109                 $shortOtpauthUrl = explode('?', $otpauthUrl)[0];
110
111                 $manual_message = DI::l10n()->t('<p>Or you can submit the authentication settings manually:</p>
112 <dl>
113         <dt>Issuer</dt>
114         <dd>%s</dd>
115         <dt>Account Name</dt>
116         <dd>%s</dd>
117         <dt>Secret Key</dt>
118         <dd>%s</dd>
119         <dt>Type</dt>
120         <dd>Time-based</dd>
121         <dt>Number of digits</dt>
122         <dd>6</dd>
123         <dt>Hashing algorithm</dt>
124         <dd>SHA-1</dd>
125 </dl>', $company, $holder, $secret);
126
127                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/verify.tpl'), [
128                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_verify'),
129                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
130
131                         '$title'              => DI::l10n()->t('Two-factor code verification'),
132                         '$help_label'         => DI::l10n()->t('Help'),
133                         '$message'            => DI::l10n()->t('<p>Please scan this QR Code with your authenticator app and submit the provided code.</p>'),
134                         '$qrcode_image'       => $qrcode_image,
135                         '$qrcode_url_message' => DI::l10n()->t('<p>Or you can open the following URL in your mobile device:</p><p><a href="%s">%s</a></p>', $otpauthUrl, $shortOtpauthUrl),
136                         '$manual_message'     => $manual_message,
137                         '$company'            => $company,
138                         '$holder'             => $holder,
139                         '$secret'             => $secret,
140
141                         '$verify_code'  => ['verify_code', DI::l10n()->t('Please enter a code from your authentication app'), '', '', DI::l10n()->t('Required'), 'autofocus autocomplete="off" placeholder="000000"'],
142                         '$verify_label' => DI::l10n()->t('Verify code and enable two-factor authentication'),
143                 ]);
144         }
145 }