]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/AppSpecific.php
Introduce `Response` for Modules to create a testable way for module responses
[friendica.git] / src / Module / Settings / TwoFactor / AppSpecific.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\Module\Response;
29 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
30 use Friendica\Module\BaseSettings;
31 use Friendica\Module\Security\Login;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
34
35 /**
36  * // Page 5: 2FA enabled, app-specific password generation
37  *
38  * @package Friendica\Module\TwoFactor
39  */
40 class AppSpecific extends BaseSettings
41 {
42         private $appSpecificPassword = null;
43
44         /** @var IManagePersonalConfigValues */
45         protected $pConfig;
46
47         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, array $server, array $parameters = [])
48         {
49                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50
51                 $this->pConfig = $pConfig;
52
53                 if (!local_user()) {
54                         return;
55                 }
56
57                 $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
58
59                 if (!$verified) {
60                         $this->baseUrl->redirect('settings/2fa');
61                 }
62
63                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
64                         notice($this->t('Please enter your password to access this page.'));
65                         $this->baseUrl->redirect('settings/2fa');
66                 }
67         }
68
69         protected function post(array $request = [], array $post = [])
70         {
71                 if (!local_user()) {
72                         return;
73                 }
74
75                 if (!empty($_POST['action'])) {
76                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
77
78                         switch ($_POST['action']) {
79                                 case 'generate':
80                                         $description = $_POST['description'] ?? '';
81                                         if (empty($description)) {
82                                                 notice($this->t('App-specific password generation failed: The description is empty.'));
83                                                 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
84                                         } elseif (AppSpecificPassword::checkDuplicateForUser(local_user(), $description)) {
85                                                 notice($this->t('App-specific password generation failed: This description already exists.'));
86                                                 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
87                                         } else {
88                                                 $this->appSpecificPassword = AppSpecificPassword::generateForUser(local_user(), $_POST['description'] ?? '');
89                                                 info($this->t('New app-specific password generated.'));
90                                         }
91
92                                         break;
93                                 case 'revoke_all' :
94                                         AppSpecificPassword::deleteAllForUser(local_user());
95                                         info($this->t('App-specific passwords successfully revoked.'));
96                                         $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
97                                         break;
98                         }
99                 }
100
101                 if (!empty($_POST['revoke_id'])) {
102                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
103
104                         if (AppSpecificPassword::deleteForUser(local_user(), $_POST['revoke_id'])) {
105                                 info($this->t('App-specific password successfully revoked.'));
106                         }
107
108                         $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
109                 }
110         }
111
112         protected function content(array $request = []): string
113         {
114                 if (!local_user()) {
115                         return Login::form('settings/2fa/app_specific');
116                 }
117
118                 parent::content();
119
120                 $appSpecificPasswords = AppSpecificPassword::getListForUser(local_user());
121
122                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/app_specific.tpl'), [
123                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_app_specific'),
124                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
125
126                         '$title'                  => $this->t('Two-factor app-specific passwords'),
127                         '$help_label'             => $this->t('Help'),
128                         '$message'                => $this->t('<p>App-specific passwords are randomly generated passwords used instead your regular password to authenticate your account on third-party applications that don\'t support two-factor authentication.</p>'),
129                         '$generated_message'      => $this->t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
130                         '$generated_app_specific_password' => $this->appSpecificPassword,
131
132                         '$description_label'      => $this->t('Description'),
133                         '$last_used_label'        => $this->t('Last Used'),
134                         '$revoke_label'           => $this->t('Revoke'),
135                         '$revoke_all_label'       => $this->t('Revoke All'),
136
137                         '$app_specific_passwords' => $appSpecificPasswords,
138                         '$generate_message'       => $this->t('When you generate a new app-specific password, you must use it right away, it will be shown to you once after you generate it.'),
139                         '$generate_title'         => $this->t('Generate new app-specific password'),
140                         '$description_placeholder_label' => $this->t('Friendiqa on my Fairphone 2...'),
141                         '$generate_label' => $this->t('Generate'),
142                 ]);
143         }
144 }