]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Index.php
Refactor deprecated App::internalRedirect() to DI::baseUrl()->redirect()
[friendica.git] / src / Module / Settings / TwoFactor / Index.php
1 <?php
2
3
4 namespace Friendica\Module\Settings\TwoFactor;
5
6
7 use Friendica\Core\L10n;
8 use Friendica\Core\PConfig;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\Session;
11 use Friendica\DI;
12 use Friendica\Model\TwoFactor\AppSpecificPassword;
13 use Friendica\Model\TwoFactor\RecoveryCode;
14 use Friendica\Model\User;
15 use Friendica\Module\BaseSettingsModule;
16 use Friendica\Module\Security\Login;
17 use PragmaRX\Google2FA\Google2FA;
18
19 class Index extends BaseSettingsModule
20 {
21         public static function post(array $parameters = [])
22         {
23                 if (!local_user()) {
24                         return;
25                 }
26
27                 self::checkFormSecurityTokenRedirectOnError('settings/2fa', 'settings_2fa');
28
29                 try {
30                         User::getIdFromPasswordAuthentication(local_user(), $_POST['password'] ?? '');
31
32                         $has_secret = (bool) PConfig::get(local_user(), '2fa', 'secret');
33                         $verified = PConfig::get(local_user(), '2fa', 'verified');
34
35                         switch ($_POST['action'] ?? '') {
36                                 case 'enable':
37                                         if (!$has_secret && !$verified) {
38                                                 $Google2FA = new Google2FA();
39
40                                                 PConfig::set(local_user(), '2fa', 'secret', $Google2FA->generateSecretKey(32));
41
42                                                 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
43                                         }
44                                         break;
45                                 case 'disable':
46                                         if ($has_secret) {
47                                                 RecoveryCode::deleteForUser(local_user());
48                                                 PConfig::delete(local_user(), '2fa', 'secret');
49                                                 PConfig::delete(local_user(), '2fa', 'verified');
50                                                 Session::remove('2fa');
51
52                                                 notice(L10n::t('Two-factor authentication successfully disabled.'));
53                                                 DI::baseUrl()->redirect('settings/2fa');
54                                         }
55                                         break;
56                                 case 'recovery':
57                                         if ($has_secret) {
58                                                 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
59                                         }
60                                         break;
61                                 case 'app_specific':
62                                         if ($has_secret) {
63                                                 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
64                                         }
65                                         break;
66                                 case 'configure':
67                                         if (!$verified) {
68                                                 DI::baseUrl()->redirect('settings/2fa/verify?t=' . self::getFormSecurityToken('settings_2fa_password'));
69                                         }
70                                         break;
71                         }
72                 } catch (\Exception $e) {
73                         notice(L10n::t('Wrong Password'));
74                 }
75         }
76
77         public static function content(array $parameters = [])
78         {
79                 if (!local_user()) {
80                         return Login::form('settings/2fa');
81                 }
82
83                 parent::content($parameters);
84
85                 $has_secret = (bool) PConfig::get(local_user(), '2fa', 'secret');
86                 $verified = PConfig::get(local_user(), '2fa', 'verified');
87
88                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/index.tpl'), [
89                         '$form_security_token' => self::getFormSecurityToken('settings_2fa'),
90                         '$title'               => L10n::t('Two-factor authentication'),
91                         '$help_label'          => L10n::t('Help'),
92                         '$status_title'        => L10n::t('Status'),
93                         '$message'             => L10n::t('<p>Use an application on a mobile device to get two-factor authentication codes when prompted on login.</p>'),
94                         '$has_secret'          => $has_secret,
95                         '$verified'            => $verified,
96
97                         '$auth_app_label'         => L10n::t('Authenticator app'),
98                         '$app_status'             => $has_secret ? $verified ? L10n::t('Configured') : L10n::t('Not Configured') : L10n::t('Disabled'),
99                         '$not_configured_message' => L10n::t('<p>You haven\'t finished configuring your authenticator app.</p>'),
100                         '$configured_message'     => L10n::t('<p>Your authenticator app is correctly configured.</p>'),
101
102                         '$recovery_codes_title'     => L10n::t('Recovery codes'),
103                         '$recovery_codes_remaining' => L10n::t('Remaining valid codes'),
104                         '$recovery_codes_count'     => RecoveryCode::countValidForUser(local_user()),
105                         '$recovery_codes_message'   => L10n::t('<p>These one-use codes can replace an authenticator app code in case you have lost access to it.</p>'),
106
107                         '$app_specific_passwords_title'     => L10n::t('App-specific passwords'),
108                         '$app_specific_passwords_remaining' => L10n::t('Generated app-specific passwords'),
109                         '$app_specific_passwords_count'     => AppSpecificPassword::countForUser(local_user()),
110                         '$app_specific_passwords_message'   => L10n::t('<p>These randomly generated passwords allow you to authenticate on apps not supporting two-factor authentication.</p>'),
111
112                         '$action_title'         => L10n::t('Actions'),
113                         '$password'             => ['password', L10n::t('Current password:'), '', L10n::t('You need to provide your current password to change two-factor authentication settings.'), 'required', 'autofocus'],
114                         '$enable_label'         => L10n::t('Enable two-factor authentication'),
115                         '$disable_label'        => L10n::t('Disable two-factor authentication'),
116                         '$recovery_codes_label' => L10n::t('Show recovery codes'),
117                         '$app_specific_passwords_label' => L10n::t('Manage app-specific passwords'),
118                         '$configure_label'      => L10n::t('Finish app configuration'),
119                 ]);
120         }
121 }