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\Settings\TwoFactor;
24 use Friendica\Core\Renderer;
26 use Friendica\Model\TwoFactor\RecoveryCode;
27 use Friendica\Module\BaseSettings;
28 use Friendica\Module\Security\Login;
31 * // Page 3: 2FA enabled but not verified, show recovery codes
33 * @package Friendica\Module\TwoFactor
35 class Recovery extends BaseSettings
37 public static function init(array $parameters = [])
43 $secret = DI::pConfig()->get(local_user(), '2fa', 'secret');
46 DI::baseUrl()->redirect('settings/2fa');
49 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
50 notice(DI::l10n()->t('Please enter your password to access this page.'));
51 DI::baseUrl()->redirect('settings/2fa');
55 public static function post(array $parameters = [])
61 if (!empty($_POST['action'])) {
62 self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
64 if ($_POST['action'] == 'regenerate') {
65 RecoveryCode::regenerateForUser(local_user());
66 notice(DI::l10n()->t('New recovery codes successfully generated.'));
67 DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
72 public static function content(array $parameters = [])
75 return Login::form('settings/2fa/recovery');
78 parent::content($parameters);
80 if (!RecoveryCode::countValidForUser(local_user())) {
81 RecoveryCode::generateForUser(local_user());
84 $recoveryCodes = RecoveryCode::getListForUser(local_user());
86 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
88 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
89 '$form_security_token' => self::getFormSecurityToken('settings_2fa_recovery'),
90 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
92 '$title' => DI::l10n()->t('Two-factor recovery codes'),
93 '$help_label' => DI::l10n()->t('Help'),
94 '$message' => DI::l10n()->t('<p>Recovery codes can be used to access your account in the event you lose access to your device and cannot receive two-factor authentication codes.</p><p><strong>Put these in a safe spot!</strong> If you lose your device and don’t have the recovery codes you will lose access to your account.</p>'),
95 '$recovery_codes' => $recoveryCodes,
96 '$regenerate_message' => DI::l10n()->t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
97 '$regenerate_label' => DI::l10n()->t('Generate new recovery codes'),
98 '$verified' => $verified,
99 '$verify_label' => DI::l10n()->t('Next: Verification'),