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