3 namespace Friendica\Module\Settings\TwoFactor;
5 use Friendica\Core\L10n;
6 use Friendica\Core\Renderer;
7 use Friendica\Core\Session;
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;
16 class Index extends BaseSettingsModule
18 public static function post(array $parameters = [])
24 self::checkFormSecurityTokenRedirectOnError('settings/2fa', 'settings_2fa');
27 User::getIdFromPasswordAuthentication(local_user(), $_POST['password'] ?? '');
29 $has_secret = (bool) DI::pConfig()->get(local_user(), '2fa', 'secret');
30 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
32 switch ($_POST['action'] ?? '') {
34 if (!$has_secret && !$verified) {
35 $Google2FA = new Google2FA();
37 DI::pConfig()->set(local_user(), '2fa', 'secret', $Google2FA->generateSecretKey(32));
39 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
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');
49 notice(L10n::t('Two-factor authentication successfully disabled.'));
50 DI::baseUrl()->redirect('settings/2fa');
55 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
60 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
65 DI::baseUrl()->redirect('settings/2fa/verify?t=' . self::getFormSecurityToken('settings_2fa_password'));
69 } catch (\Exception $e) {
70 notice(L10n::t('Wrong Password'));
74 public static function content(array $parameters = [])
77 return Login::form('settings/2fa');
80 parent::content($parameters);
82 $has_secret = (bool) DI::pConfig()->get(local_user(), '2fa', 'secret');
83 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
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,
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>'),
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>'),
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>'),
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'),