3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Settings\TwoFactor;
24 use Friendica\Core\Renderer;
26 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
27 use Friendica\Module\BaseSettings;
28 use Friendica\Module\Security\Login;
31 * // Page 5: 2FA enabled, app-specific password generation
33 * @package Friendica\Module\TwoFactor
35 class AppSpecific extends BaseSettings
37 private static $appSpecificPassword = null;
39 public static function init(array $parameters = [])
45 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
48 DI::baseUrl()->redirect('settings/2fa');
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');
57 public static function post(array $parameters = [])
63 if (!empty($_POST['action'])) {
64 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
66 switch ($_POST['action']) {
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'));
76 self::$appSpecificPassword = AppSpecificPassword::generateForUser(local_user(), $_POST['description'] ?? '');
77 info(DI::l10n()->t('New app-specific password generated.'));
82 AppSpecificPassword::deleteAllForUser(local_user());
83 info(DI::l10n()->t('App-specific passwords successfully revoked.'));
84 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
89 if (!empty($_POST['revoke_id'])) {
90 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
92 if (AppSpecificPassword::deleteForUser(local_user(), $_POST['revoke_id'])) {
93 info(DI::l10n()->t('App-specific password successfully revoked.'));
96 DI::baseUrl()->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
100 public static function content(array $parameters = [])
103 return Login::form('settings/2fa/app_specific');
106 parent::content($parameters);
108 $appSpecificPasswords = AppSpecificPassword::getListForUser(local_user());
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'),
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,
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'),
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'),