3 namespace Friendica\Module\Settings\TwoFactor;
5 use Friendica\Core\Renderer;
6 use Friendica\Core\Session;
8 use Friendica\Model\TwoFactor\AppSpecificPassword;
9 use Friendica\Model\TwoFactor\RecoveryCode;
10 use Friendica\Model\User;
11 use Friendica\Module\BaseSettingsModule;
12 use Friendica\Module\Security\Login;
13 use PragmaRX\Google2FA\Google2FA;
15 class Index extends BaseSettingsModule
17 public static function post(array $parameters = [])
23 self::checkFormSecurityTokenRedirectOnError('settings/2fa', 'settings_2fa');
26 User::getIdFromPasswordAuthentication(local_user(), $_POST['password'] ?? '');
28 $has_secret = (bool) DI::pConfig()->get(local_user(), '2fa', 'secret');
29 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
31 switch ($_POST['action'] ?? '') {
33 if (!$has_secret && !$verified) {
34 $Google2FA = new Google2FA();
36 DI::pConfig()->set(local_user(), '2fa', 'secret', $Google2FA->generateSecretKey(32));
38 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
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');
48 notice(DI::l10n()->t('Two-factor authentication successfully disabled.'));
49 DI::baseUrl()->redirect('settings/2fa');
54 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
59 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
64 DI::baseUrl()->redirect('settings/2fa/verify?t=' . self::getFormSecurityToken('settings_2fa_password'));
68 } catch (\Exception $e) {
69 notice(DI::l10n()->t('Wrong Password'));
73 public static function content(array $parameters = [])
76 return Login::form('settings/2fa');
79 parent::content($parameters);
81 $has_secret = (bool) DI::pConfig()->get(local_user(), '2fa', 'secret');
82 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
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,
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>'),
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>'),
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>'),
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'),