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