]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Index.php
Merge pull request #12027 from annando/no-boot-src-module-3
[friendica.git] / src / Module / Settings / TwoFactor / Index.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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 Friendica\Core\Renderer;
25 use Friendica\Core\Session;
26 use Friendica\DI;
27 use Friendica\Network\HTTPException\FoundException;
28 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
29 use Friendica\Security\TwoFactor\Model\RecoveryCode;
30 use Friendica\Model\User;
31 use Friendica\Module\BaseSettings;
32 use Friendica\Module\Security\Login;
33 use PragmaRX\Google2FA\Google2FA;
34
35 class Index extends BaseSettings
36 {
37         protected function post(array $request = [])
38         {
39                 if (!Session::getLocalUser()) {
40                         return;
41                 }
42
43                 self::checkFormSecurityTokenRedirectOnError('settings/2fa', 'settings_2fa');
44
45                 try {
46                         User::getIdFromPasswordAuthentication(Session::getLocalUser(), $_POST['password'] ?? '');
47
48                         $has_secret = (bool)DI::pConfig()->get(Session::getLocalUser(), '2fa', 'secret');
49                         $verified   = DI::pConfig()->get(Session::getLocalUser(), '2fa', 'verified');
50
51                         switch ($_POST['action'] ?? '') {
52                                 case 'enable':
53                                         if (!$has_secret && !$verified) {
54                                                 $Google2FA = new Google2FA();
55
56                                                 DI::pConfig()->set(Session::getLocalUser(), '2fa', 'secret', $Google2FA->generateSecretKey(32));
57
58                                                 DI::baseUrl()
59                                                   ->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
60                                         }
61                                         break;
62                                 case 'disable':
63                                         if ($has_secret) {
64                                                 RecoveryCode::deleteForUser(Session::getLocalUser());
65                                                 DI::pConfig()->delete(Session::getLocalUser(), '2fa', 'secret');
66                                                 DI::pConfig()->delete(Session::getLocalUser(), '2fa', 'verified');
67                                                 DI::session()->remove('2fa');
68
69                                                 DI::sysmsg()->addInfo(DI::l10n()->t('Two-factor authentication successfully disabled.'));
70                                                 DI::baseUrl()->redirect('settings/2fa');
71                                         }
72                                         break;
73                                 case 'recovery':
74                                         if ($has_secret) {
75                                                 DI::baseUrl()
76                                                   ->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
77                                         }
78                                         break;
79                                 case 'app_specific':
80                                         if ($has_secret) {
81                                                 DI::baseUrl()
82                                                   ->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
83                                         }
84                                         break;
85                                 case 'trusted':
86                                         if ($has_secret) {
87                                                 DI::baseUrl()
88                                                   ->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
89                                         }
90                                         break;
91                                 case 'configure':
92                                         if (!$verified) {
93                                                 DI::baseUrl()
94                                                   ->redirect('settings/2fa/verify?t=' . self::getFormSecurityToken('settings_2fa_password'));
95                                         }
96                                         break;
97                         }
98                 } catch (FoundException $exception) {
99                         // Nothing to do here
100                 } catch (\Exception $e) {
101                         DI::sysmsg()->addNotice(DI::l10n()->t($e->getMessage()));
102                 }
103         }
104
105         protected function content(array $request = []): string
106         {
107                 if (!Session::getLocalUser()) {
108                         return Login::form('settings/2fa');
109                 }
110
111                 parent::content();
112
113                 $has_secret = (bool) DI::pConfig()->get(Session::getLocalUser(), '2fa', 'secret');
114                 $verified = DI::pConfig()->get(Session::getLocalUser(), '2fa', 'verified');
115
116                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/index.tpl'), [
117                         '$form_security_token' => self::getFormSecurityToken('settings_2fa'),
118                         '$title'               => DI::l10n()->t('Two-factor authentication'),
119                         '$help_label'          => DI::l10n()->t('Help'),
120                         '$status_title'        => DI::l10n()->t('Status'),
121                         '$message'             => DI::l10n()->t('<p>Use an application on a mobile device to get two-factor authentication codes when prompted on login.</p>'),
122                         '$has_secret'          => $has_secret,
123                         '$verified'            => $verified,
124
125                         '$auth_app_label'         => DI::l10n()->t('Authenticator app'),
126                         '$app_status'             => $has_secret ? $verified ? DI::l10n()->t('Configured') : DI::l10n()->t('Not Configured') : DI::l10n()->t('Disabled'),
127                         '$not_configured_message' => DI::l10n()->t('<p>You haven\'t finished configuring your authenticator app.</p>'),
128                         '$configured_message'     => DI::l10n()->t('<p>Your authenticator app is correctly configured.</p>'),
129
130                         '$recovery_codes_title'     => DI::l10n()->t('Recovery codes'),
131                         '$recovery_codes_remaining' => DI::l10n()->t('Remaining valid codes'),
132                         '$recovery_codes_count'     => RecoveryCode::countValidForUser(Session::getLocalUser()),
133                         '$recovery_codes_message'   => DI::l10n()->t('<p>These one-use codes can replace an authenticator app code in case you have lost access to it.</p>'),
134
135                         '$app_specific_passwords_title'     => DI::l10n()->t('App-specific passwords'),
136                         '$app_specific_passwords_remaining' => DI::l10n()->t('Generated app-specific passwords'),
137                         '$app_specific_passwords_count'     => AppSpecificPassword::countForUser(Session::getLocalUser()),
138                         '$app_specific_passwords_message'   => DI::l10n()->t('<p>These randomly generated passwords allow you to authenticate on apps not supporting two-factor authentication.</p>'),
139
140                         '$action_title'         => DI::l10n()->t('Actions'),
141                         '$password'             => ['password', DI::l10n()->t('Current password:'), '', DI::l10n()->t('You need to provide your current password to change two-factor authentication settings.'), DI::l10n()->t('Required'), 'autofocus'],
142                         '$enable_label'         => DI::l10n()->t('Enable two-factor authentication'),
143                         '$disable_label'        => DI::l10n()->t('Disable two-factor authentication'),
144                         '$recovery_codes_label' => DI::l10n()->t('Show recovery codes'),
145                         '$app_specific_passwords_label' => DI::l10n()->t('Manage app-specific passwords'),
146                         '$trusted_browsers_label' => DI::l10n()->t('Manage trusted browsers'),
147                         '$configure_label'      => DI::l10n()->t('Finish app configuration'),
148                 ]);
149         }
150 }