3 namespace Friendica\Module\Security\TwoFactor;
5 use Friendica\BaseModule;
6 use Friendica\Core\Renderer;
7 use Friendica\Core\Session;
9 use Friendica\Model\TwoFactor\RecoveryCode;
12 * // Page 1a: Recovery code verification
14 * @package Friendica\Module\TwoFactor
16 class Recovery extends BaseModule
18 public static function init(array $parameters = [])
25 public static function post(array $parameters = [])
31 if (($_POST['action'] ?? '') == 'recover') {
32 self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
36 $recovery_code = $_POST['recovery_code'] ?? '';
38 if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
39 RecoveryCode::markUsedForUser(local_user(), $recovery_code);
40 Session::set('2fa', true);
41 notice(DI::l10n()->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
43 DI::auth()->setForUser($a, $a->user, true, true);
45 notice(DI::l10n()->t('Invalid code, please retry.'));
50 public static function content(array $parameters = [])
53 DI::baseUrl()->redirect();
56 // Already authenticated with 2FA token
57 if (Session::get('2fa')) {
58 DI::baseUrl()->redirect();
61 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
62 '$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
64 '$title' => DI::l10n()->t('Two-factor recovery'),
65 '$message' => DI::l10n()->t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
66 '$recovery_message' => DI::l10n()->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
67 '$recovery_code' => ['recovery_code', DI::l10n()->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
68 '$recovery_label' => DI::l10n()->t('Submit recovery code and complete login'),