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;
10 use Friendica\Model\TwoFactor\RecoveryCode;
13 * // Page 1a: Recovery code verification
15 * @package Friendica\Module\TwoFactor
17 class Recovery extends BaseModule
19 public static function init(array $parameters = [])
26 public static function post(array $parameters = [])
32 if (($_POST['action'] ?? '') == 'recover') {
33 self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
37 $recovery_code = $_POST['recovery_code'] ?? '';
39 if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
40 RecoveryCode::markUsedForUser(local_user(), $recovery_code);
41 Session::set('2fa', true);
42 notice(L10n::t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
44 // Resume normal login workflow
45 /** @var Authentication $authentication */
46 $authentication = self::getClass(Authentication::class);
47 $authentication->setForUser($a, $a->user, true, true);
49 notice(L10n::t('Invalid code, please retry.'));
54 public static function content(array $parameters = [])
57 self::getApp()->internalRedirect();
60 // Already authenticated with 2FA token
61 if (Session::get('2fa')) {
62 self::getApp()->internalRedirect();
65 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
66 '$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
68 '$title' => L10n::t('Two-factor recovery'),
69 '$message' => L10n::t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
70 '$recovery_message' => L10n::t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
71 '$recovery_code' => ['recovery_code', L10n::t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
72 '$recovery_label' => L10n::t('Submit recovery code and complete login'),