3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Security\TwoFactor;
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
28 use Friendica\Model\TwoFactor\RecoveryCode;
31 * // Page 1a: Recovery code verification
33 * @package Friendica\Module\TwoFactor
35 class Recovery extends BaseModule
37 public static function init(array $parameters = [])
44 public static function post(array $parameters = [])
50 if (($_POST['action'] ?? '') == 'recover') {
51 self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
55 $recovery_code = $_POST['recovery_code'] ?? '';
57 if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
58 RecoveryCode::markUsedForUser(local_user(), $recovery_code);
59 Session::set('2fa', true);
60 info(DI::l10n()->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
62 DI::auth()->setForUser($a, $a->user, true, true);
64 notice(DI::l10n()->t('Invalid code, please retry.'));
69 public static function content(array $parameters = [])
72 DI::baseUrl()->redirect();
75 // Already authenticated with 2FA token
76 if (Session::get('2fa')) {
77 DI::baseUrl()->redirect();
80 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
81 '$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
83 '$title' => DI::l10n()->t('Two-factor recovery'),
84 '$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>'),
85 '$recovery_message' => DI::l10n()->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
86 '$recovery_code' => ['recovery_code', DI::l10n()->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
87 '$recovery_label' => DI::l10n()->t('Submit recovery code and complete login'),