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