3 namespace Friendica\Module\Settings\TwoFactor;
5 use Friendica\Core\Renderer;
7 use Friendica\Model\TwoFactor\AppSpecificPassword;
8 use Friendica\Module\BaseSettingsModule;
9 use Friendica\Module\Security\Login;
12 * // Page 5: 2FA enabled, app-specific password generation
14 * @package Friendica\Module\TwoFactor
16 class AppSpecific extends BaseSettingsModule
18 private static $appSpecificPassword = null;
20 public static function init(array $parameters = [])
26 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
29 DI::baseUrl()->redirect('settings/2fa');
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');
38 public static function post(array $parameters = [])
44 if (!empty($_POST['action'])) {
45 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
47 switch ($_POST['action']) {
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'));
57 self::$appSpecificPassword = AppSpecificPassword::generateForUser(local_user(), $_POST['description'] ?? '');
58 notice(DI::l10n()->t('New app-specific password generated.'));
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'));
70 if (!empty($_POST['revoke_id'])) {
71 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
73 if (AppSpecificPassword::deleteForUser(local_user(), $_POST['revoke_id'])) {
74 notice(DI::l10n()->t('App-specific password successfully revoked.'));
77 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
81 public static function content(array $parameters = [])
84 return Login::form('settings/2fa/app_specific');
87 parent::content($parameters);
89 $appSpecificPasswords = AppSpecificPassword::getListForUser(local_user());
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'),
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,
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'),
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'),