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