]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Recovery.php
0d324aacaf9f7d4a6a9d08995029c56aafcf0c94
[friendica.git] / src / Module / Settings / TwoFactor / Recovery.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Security\TwoFactor\Model\RecoveryCode;
29 use Friendica\Module\BaseSettings;
30 use Friendica\Module\Security\Login;
31 use Friendica\Util\Profiler;
32 use Psr\Log\LoggerInterface;
33
34 /**
35  * // Page 3: 2FA enabled but not verified, show recovery codes
36  *
37  * @package Friendica\Module\TwoFactor
38  */
39 class Recovery extends BaseSettings
40 {
41         /** @var IManagePersonalConfigValues */
42         protected $pConfig;
43
44         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, IManagePersonalConfigValues $pConfig, array $server, array $parameters = [])
45         {
46                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $server, $parameters);
47
48                 $this->pConfig = $pConfig;
49
50                 if (!local_user()) {
51                         return;
52                 }
53
54                 $secret = $this->pConfig->get(local_user(), '2fa', 'secret');
55
56                 if (!$secret) {
57                         $this->baseUrl->redirect('settings/2fa');
58                 }
59
60                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
61                         notice($this->t('Please enter your password to access this page.'));
62                         $this->baseUrl->redirect('settings/2fa');
63                 }
64         }
65
66         protected function post(array $request = [], array $post = [])
67         {
68                 if (!local_user()) {
69                         return;
70                 }
71
72                 if (!empty($_POST['action'])) {
73                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
74
75                         if ($_POST['action'] == 'regenerate') {
76                                 RecoveryCode::regenerateForUser(local_user());
77                                 info($this->t('New recovery codes successfully generated.'));
78                                 $this->baseUrl->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
79                         }
80                 }
81         }
82
83         protected function content(array $request = []): string
84         {
85                 if (!local_user()) {
86                         return Login::form('settings/2fa/recovery');
87                 }
88
89                 parent::content();
90
91                 if (!RecoveryCode::countValidForUser(local_user())) {
92                         RecoveryCode::generateForUser(local_user());
93                 }
94
95                 $recoveryCodes = RecoveryCode::getListForUser(local_user());
96
97                 $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
98                 
99                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
100                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_recovery'),
101                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
102
103                         '$title'              => $this->t('Two-factor recovery codes'),
104                         '$help_label'         => $this->t('Help'),
105                         '$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>'),
106                         '$recovery_codes'     => $recoveryCodes,
107                         '$regenerate_message' => $this->t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
108                         '$regenerate_label'   => $this->t('Generate new recovery codes'),
109                         '$verified'           => $verified,
110                         '$verify_label'       => $this->t('Next: Verification'),
111                 ]);
112         }
113 }