3 namespace Friendica\Module\Settings\TwoFactor;
5 use Friendica\Core\L10n;
6 use Friendica\Core\Renderer;
8 use Friendica\Model\TwoFactor\RecoveryCode;
9 use Friendica\Module\BaseSettingsModule;
10 use Friendica\Module\Security\Login;
13 * // Page 3: 2FA enabled but not verified, show recovery codes
15 * @package Friendica\Module\TwoFactor
17 class Recovery extends BaseSettingsModule
19 public static function init(array $parameters = [])
25 $secret = DI::pConfig()->get(local_user(), '2fa', 'secret');
28 DI::baseUrl()->redirect('settings/2fa');
31 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
32 notice(DI::l10n()->t('Please enter your password to access this page.'));
33 DI::baseUrl()->redirect('settings/2fa');
37 public static function post(array $parameters = [])
43 if (!empty($_POST['action'])) {
44 self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
46 if ($_POST['action'] == 'regenerate') {
47 RecoveryCode::regenerateForUser(local_user());
48 notice(DI::l10n()->t('New recovery codes successfully generated.'));
49 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
54 public static function content(array $parameters = [])
57 return Login::form('settings/2fa/recovery');
60 parent::content($parameters);
62 if (!RecoveryCode::countValidForUser(local_user())) {
63 RecoveryCode::generateForUser(local_user());
66 $recoveryCodes = RecoveryCode::getListForUser(local_user());
68 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
70 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
71 '$form_security_token' => self::getFormSecurityToken('settings_2fa_recovery'),
72 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
74 '$title' => DI::l10n()->t('Two-factor recovery codes'),
75 '$help_label' => DI::l10n()->t('Help'),
76 '$message' => DI::l10n()->t('<p>Recovery codes can be used to access your account in the event you lose access to your device and cannot receive two-factor authentication codes.</p><p><strong>Put these in a safe spot!</strong> If you lose your device and don’t have the recovery codes you will lose access to your account.</p>'),
77 '$recovery_codes' => $recoveryCodes,
78 '$regenerate_message' => DI::l10n()->t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
79 '$regenerate_label' => DI::l10n()->t('Generate new recovery codes'),
80 '$verified' => $verified,
81 '$verify_label' => DI::l10n()->t('Next: Verification'),