]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/AppSpecific.php
Merge branch 'friendica:2023.09-rc' into Leftovers-from-PR-#13339
[friendica.git] / src / Module / Settings / TwoFactor / AppSpecific.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\AppSpecificPassword;
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         /** @var SystemMessages */
49         protected $systemMessages;
50
51         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 = [])
52         {
53                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
54
55                 $this->pConfig = $pConfig;
56                 $this->systemMessages = $systemMessages;
57
58                 if (!$this->session->getLocalUserId()) {
59                         return;
60                 }
61
62                 $verified = $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'verified');
63
64                 if (!$verified) {
65                         $this->baseUrl->redirect('settings/2fa');
66                 }
67
68                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
69                         $this->systemMessages->addNotice($this->t('Please enter your password to access this page.'));
70                         $this->baseUrl->redirect('settings/2fa');
71                 }
72         }
73
74         protected function post(array $request = [])
75         {
76                 if (!$this->session->getLocalUserId()) {
77                         return;
78                 }
79
80                 if (!empty($request['action'])) {
81                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
82
83                         switch ($request['action']) {
84                                 case 'generate':
85                                         $description = $request['description'] ?? '';
86                                         if (empty($description)) {
87                                                 $this->systemMessages->addNotice($this->t('App-specific password generation failed: The description is empty.'));
88                                                 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
89                                         } elseif (AppSpecificPassword::checkDuplicateForUser($this->session->getLocalUserId(), $description)) {
90                                                 $this->systemMessages->addNotice($this->t('App-specific password generation failed: This description already exists.'));
91                                                 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
92                                         } else {
93                                                 $this->appSpecificPassword = AppSpecificPassword::generateForUser($this->session->getLocalUserId(), $request['description'] ?? '');
94                                                 $this->systemMessages->addInfo($this->t('New app-specific password generated.'));
95                                         }
96
97                                         break;
98                                 case 'revoke_all' :
99                                         AppSpecificPassword::deleteAllForUser($this->session->getLocalUserId());
100                                         $this->systemMessages->addInfo($this->t('App-specific passwords successfully revoked.'));
101                                         $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
102                                         break;
103                         }
104                 }
105
106                 if (!empty($request['revoke_id'])) {
107                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
108
109                         if (AppSpecificPassword::deleteForUser($this->session->getLocalUserId(), $request['revoke_id'])) {
110                                 $this->systemMessages->addInfo($this->t('App-specific password successfully revoked.'));
111                         }
112
113                         $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
114                 }
115         }
116
117         protected function content(array $request = []): string
118         {
119                 if (!$this->session->getLocalUserId()) {
120                         return Login::form('settings/2fa/app_specific');
121                 }
122
123                 parent::content();
124
125                 $appSpecificPasswords = AppSpecificPassword::getListForUser($this->session->getLocalUserId());
126
127                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/app_specific.tpl'), [
128                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_app_specific'),
129                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
130
131                         '$title'                  => $this->t('Two-factor app-specific passwords'),
132                         '$help_label'             => $this->t('Help'),
133                         '$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>'),
134                         '$generated_message'      => $this->t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
135                         '$generated_app_specific_password' => $this->appSpecificPassword,
136
137                         '$description_label'      => $this->t('Description'),
138                         '$last_used_label'        => $this->t('Last Used'),
139                         '$revoke_label'           => $this->t('Revoke'),
140                         '$revoke_all_label'       => $this->t('Revoke All'),
141
142                         '$app_specific_passwords' => $appSpecificPasswords,
143                         '$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.'),
144                         '$generate_title'         => $this->t('Generate new app-specific password'),
145                         '$description_placeholder_label' => $this->t('Friendiqa on my Fairphone 2...'),
146                         '$generate_label' => $this->t('Generate'),
147                 ]);
148         }
149 }