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\Settings\TwoFactor;
24 use Friendica\App\BaseURL;
25 use Friendica\Core\L10n;
26 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
27 use Friendica\Core\Renderer;
28 use Friendica\Security\TwoFactor\Model\RecoveryCode;
29 use Friendica\Module\BaseSettings;
30 use Friendica\Module\Security\Login;
33 * // Page 3: 2FA enabled but not verified, show recovery codes
35 * @package Friendica\Module\TwoFactor
37 class Recovery extends BaseSettings
39 /** @var IManagePersonalConfigValues */
44 public function __construct(IManagePersonalConfigValues $pConfig, BaseURL $baseUrl, L10n $l10n, array $parameters = [])
46 parent::__construct($l10n, $parameters);
48 $this->pConfig = $pConfig;
49 $this->baseUrl = $baseUrl;
55 $secret = $this->pConfig->get(local_user(), '2fa', 'secret');
58 $this->baseUrl->redirect('settings/2fa');
61 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
62 notice($this->t('Please enter your password to access this page.'));
63 $this->baseUrl->redirect('settings/2fa');
67 public function post()
73 if (!empty($_POST['action'])) {
74 self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
76 if ($_POST['action'] == 'regenerate') {
77 RecoveryCode::regenerateForUser(local_user());
78 info($this->t('New recovery codes successfully generated.'));
79 $this->baseUrl->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
84 public function content(): string
87 return Login::form('settings/2fa/recovery');
92 if (!RecoveryCode::countValidForUser(local_user())) {
93 RecoveryCode::generateForUser(local_user());
96 $recoveryCodes = RecoveryCode::getListForUser(local_user());
98 $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
100 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
101 '$form_security_token' => self::getFormSecurityToken('settings_2fa_recovery'),
102 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
104 '$title' => $this->t('Two-factor recovery codes'),
105 '$help_label' => $this->t('Help'),
106 '$message' => $this->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>'),
107 '$recovery_codes' => $recoveryCodes,
108 '$regenerate_message' => $this->t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
109 '$regenerate_label' => $this->t('Generate new recovery codes'),
110 '$verified' => $verified,
111 '$verify_label' => $this->t('Next: Verification'),