3 namespace Friendica\Module\Settings\TwoFactor;
5 use Friendica\Core\L10n;
6 use Friendica\Core\Renderer;
8 use Friendica\Model\TwoFactor\AppSpecificPassword;
9 use Friendica\Module\BaseSettingsModule;
10 use Friendica\Module\Security\Login;
13 * // Page 5: 2FA enabled, app-specific password generation
15 * @package Friendica\Module\TwoFactor
17 class AppSpecific extends BaseSettingsModule
19 private static $appSpecificPassword = null;
21 public static function init(array $parameters = [])
27 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
30 DI::baseUrl()->redirect('settings/2fa');
33 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
34 notice(L10n::t('Please enter your password to access this page.'));
35 DI::baseUrl()->redirect('settings/2fa');
39 public static function post(array $parameters = [])
45 if (!empty($_POST['action'])) {
46 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
48 switch ($_POST['action']) {
50 $description = $_POST['description'] ?? '';
51 if (empty($description)) {
52 notice(L10n::t('App-specific password generation failed: The description is empty.'));
53 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
54 } elseif (AppSpecificPassword::checkDuplicateForUser(local_user(), $description)) {
55 notice(L10n::t('App-specific password generation failed: This description already exists.'));
56 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
58 self::$appSpecificPassword = AppSpecificPassword::generateForUser(local_user(), $_POST['description'] ?? '');
59 notice(L10n::t('New app-specific password generated.'));
64 AppSpecificPassword::deleteAllForUser(local_user());
65 notice(L10n::t('App-specific passwords successfully revoked.'));
66 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
71 if (!empty($_POST['revoke_id'])) {
72 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
74 if (AppSpecificPassword::deleteForUser(local_user(), $_POST['revoke_id'])) {
75 notice(L10n::t('App-specific password successfully revoked.'));
78 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
82 public static function content(array $parameters = [])
85 return Login::form('settings/2fa/app_specific');
88 parent::content($parameters);
90 $appSpecificPasswords = AppSpecificPassword::getListForUser(local_user());
92 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/app_specific.tpl'), [
93 '$form_security_token' => self::getFormSecurityToken('settings_2fa_app_specific'),
94 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
96 '$title' => L10n::t('Two-factor app-specific passwords'),
97 '$help_label' => L10n::t('Help'),
98 '$message' => 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>'),
99 '$generated_message' => L10n::t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
100 '$generated_app_specific_password' => self::$appSpecificPassword,
102 '$description_label' => L10n::t('Description'),
103 '$last_used_label' => L10n::t('Last Used'),
104 '$revoke_label' => L10n::t('Revoke'),
105 '$revoke_all_label' => L10n::t('Revoke All'),
107 '$app_specific_passwords' => $appSpecificPasswords,
108 '$generate_message' => 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.'),
109 '$generate_title' => L10n::t('Generate new app-specific password'),
110 '$description_placeholder_label' => L10n::t('Friendiqa on my Fairphone 2...'),
111 '$generate_label' => L10n::t('Generate'),