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