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