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