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