3 * @copyright Copyright (C) 2010-2022, 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;
25 use Friendica\Core\L10n;
26 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Module\Response;
31 use Friendica\Security\TwoFactor\Model\RecoveryCode;
32 use Friendica\Module\BaseSettings;
33 use Friendica\Module\Security\Login;
34 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
38 * // Page 3: 2FA enabled but not verified, show recovery codes
40 * @package Friendica\Module\TwoFactor
42 class Recovery extends BaseSettings
44 /** @var IManagePersonalConfigValues */
47 public function __construct(IManagePersonalConfigValues $pConfig, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
49 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
51 $this->pConfig = $pConfig;
53 if (!DI::userSession()->getLocalUserId()) {
57 $secret = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'secret');
60 $this->baseUrl->redirect('settings/2fa');
63 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
64 DI::sysmsg()->addNotice($this->t('Please enter your password to access this page.'));
65 $this->baseUrl->redirect('settings/2fa');
69 protected function post(array $request = [])
71 if (!DI::userSession()->getLocalUserId()) {
75 if (!empty($_POST['action'])) {
76 self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
78 if ($_POST['action'] == 'regenerate') {
79 RecoveryCode::regenerateForUser(DI::userSession()->getLocalUserId());
80 DI::sysmsg()->addInfo($this->t('New recovery codes successfully generated.'));
81 $this->baseUrl->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
86 protected function content(array $request = []): string
88 if (!DI::userSession()->getLocalUserId()) {
89 return Login::form('settings/2fa/recovery');
94 if (!RecoveryCode::countValidForUser(DI::userSession()->getLocalUserId())) {
95 RecoveryCode::generateForUser(DI::userSession()->getLocalUserId());
98 $recoveryCodes = RecoveryCode::getListForUser(DI::userSession()->getLocalUserId());
100 $verified = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
102 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
103 '$form_security_token' => self::getFormSecurityToken('settings_2fa_recovery'),
104 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
106 '$title' => $this->t('Two-factor recovery codes'),
107 '$help_label' => $this->t('Help'),
108 '$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>'),
109 '$recovery_codes' => $recoveryCodes,
110 '$regenerate_message' => $this->t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
111 '$regenerate_label' => $this->t('Generate new recovery codes'),
112 '$verified' => $verified,
113 '$verify_label' => $this->t('Next: Verification'),