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