3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\User;
29 use Friendica\Security\TwoFactor\Model\RecoveryCode;
32 * // Page 1a: Recovery code verification
34 * @package Friendica\Module\TwoFactor
36 class Recovery extends BaseModule
38 public static function init(array $parameters = [])
45 public static function post(array $parameters = [])
51 if (($_POST['action'] ?? '') == 'recover') {
52 self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
56 $recovery_code = $_POST['recovery_code'] ?? '';
58 if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
59 RecoveryCode::markUsedForUser(local_user(), $recovery_code);
60 Session::set('2fa', true);
61 info(DI::l10n()->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
63 DI::auth()->setForUser($a, User::getById($a->getUserId()), true, true);
65 notice(DI::l10n()->t('Invalid code, please retry.'));
70 public static function content(array $parameters = [])
73 DI::baseUrl()->redirect();
76 // Already authenticated with 2FA token
77 if (Session::get('2fa')) {
78 DI::baseUrl()->redirect();
81 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
82 '$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
84 '$title' => DI::l10n()->t('Two-factor recovery'),
85 '$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>'),
86 '$recovery_message' => DI::l10n()->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
87 '$recovery_code' => ['recovery_code', DI::l10n()->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
88 '$recovery_label' => DI::l10n()->t('Submit recovery code and complete login'),