]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Recovery.php
Merge branch 'friendica:2023.09-rc' into Leftovers-from-PR-#13339
[friendica.git] / src / Module / Settings / TwoFactor / Recovery.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings\TwoFactor;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Module\BaseSettings;
30 use Friendica\Module\Response;
31 use Friendica\Module\Security\Login;
32 use Friendica\Navigation\SystemMessages;
33 use Friendica\Security\TwoFactor\Model\RecoveryCode;
34 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
36
37 /**
38  * // Page 3: 2FA enabled but not verified, show recovery codes
39  *
40  * @package Friendica\Module\TwoFactor
41  */
42 class Recovery extends BaseSettings
43 {
44         /** @var IManagePersonalConfigValues */
45         protected $pConfig;
46         /** @var SystemMessages */
47         protected $systemMessages;
48
49         public function __construct(SystemMessages $systemMessages, 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 = [])
50         {
51                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
52
53                 $this->pConfig = $pConfig;
54                 $this->systemMessages = $systemMessages;
55
56                 if (!$this->session->getLocalUserId()) {
57                         return;
58                 }
59
60                 $secret = $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'secret');
61
62                 if (!$secret) {
63                         $this->baseUrl->redirect('settings/2fa');
64                 }
65
66                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
67                         $this->systemMessages->addNotice($this->t('Please enter your password to access this page.'));
68                         $this->baseUrl->redirect('settings/2fa');
69                 }
70         }
71
72         protected function post(array $request = [])
73         {
74                 if (!$this->session->getLocalUserId()) {
75                         return;
76                 }
77
78                 if (!empty($_POST['action'])) {
79                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
80
81                         if ($_POST['action'] == 'regenerate') {
82                                 RecoveryCode::regenerateForUser($this->session->getLocalUserId());
83                                 $this->systemMessages->addInfo($this->t('New recovery codes successfully generated.'));
84                                 $this->baseUrl->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
85                         }
86                 }
87         }
88
89         protected function content(array $request = []): string
90         {
91                 if (!$this->session->getLocalUserId()) {
92                         return Login::form('settings/2fa/recovery');
93                 }
94
95                 parent::content();
96
97                 if (!RecoveryCode::countValidForUser($this->session->getLocalUserId())) {
98                         RecoveryCode::generateForUser($this->session->getLocalUserId());
99                 }
100
101                 $recoveryCodes = RecoveryCode::getListForUser($this->session->getLocalUserId());
102
103                 $verified = $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'verified');
104
105                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
106                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_recovery'),
107                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
108
109                         '$title'              => $this->t('Two-factor recovery codes'),
110                         '$help_label'         => $this->t('Help'),
111                         '$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>'),
112                         '$recovery_codes'     => $recoveryCodes,
113                         '$regenerate_message' => $this->t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
114                         '$regenerate_label'   => $this->t('Generate new recovery codes'),
115                         '$verified'           => $verified,
116                         '$verify_label'       => $this->t('Next: Verification'),
117                 ]);
118         }
119 }