3 * @copyright Copyright (C) 2010-2021, the Friendica project
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;
25 use Friendica\Core\L10n;
26 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
27 use Friendica\Core\Renderer;
28 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
29 use Friendica\Module\BaseSettings;
30 use Friendica\Module\Security\Login;
31 use Friendica\Util\Profiler;
32 use Psr\Log\LoggerInterface;
35 * // Page 5: 2FA enabled, app-specific password generation
37 * @package Friendica\Module\TwoFactor
39 class AppSpecific extends BaseSettings
41 private $appSpecificPassword = null;
43 /** @var IManagePersonalConfigValues */
46 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, IManagePersonalConfigValues $pConfig, array $server, array $parameters = [])
48 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $server, $parameters);
50 $this->pConfig = $pConfig;
56 $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
59 $this->baseUrl->redirect('settings/2fa');
62 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
63 notice($this->t('Please enter your password to access this page.'));
64 $this->baseUrl->redirect('settings/2fa');
68 protected function post(array $request = [], array $post = [])
74 if (!empty($_POST['action'])) {
75 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
77 switch ($_POST['action']) {
79 $description = $_POST['description'] ?? '';
80 if (empty($description)) {
81 notice($this->t('App-specific password generation failed: The description is empty.'));
82 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
83 } elseif (AppSpecificPassword::checkDuplicateForUser(local_user(), $description)) {
84 notice($this->t('App-specific password generation failed: This description already exists.'));
85 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
87 $this->appSpecificPassword = AppSpecificPassword::generateForUser(local_user(), $_POST['description'] ?? '');
88 info($this->t('New app-specific password generated.'));
93 AppSpecificPassword::deleteAllForUser(local_user());
94 info($this->t('App-specific passwords successfully revoked.'));
95 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
100 if (!empty($_POST['revoke_id'])) {
101 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
103 if (AppSpecificPassword::deleteForUser(local_user(), $_POST['revoke_id'])) {
104 info($this->t('App-specific password successfully revoked.'));
107 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
111 protected function content(array $request = []): string
114 return Login::form('settings/2fa/app_specific');
119 $appSpecificPasswords = AppSpecificPassword::getListForUser(local_user());
121 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/app_specific.tpl'), [
122 '$form_security_token' => self::getFormSecurityToken('settings_2fa_app_specific'),
123 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
125 '$title' => $this->t('Two-factor app-specific passwords'),
126 '$help_label' => $this->t('Help'),
127 '$message' => $this->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>'),
128 '$generated_message' => $this->t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
129 '$generated_app_specific_password' => $this->appSpecificPassword,
131 '$description_label' => $this->t('Description'),
132 '$last_used_label' => $this->t('Last Used'),
133 '$revoke_label' => $this->t('Revoke'),
134 '$revoke_all_label' => $this->t('Revoke All'),
136 '$app_specific_passwords' => $appSpecificPasswords,
137 '$generate_message' => $this->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.'),
138 '$generate_title' => $this->t('Generate new app-specific password'),
139 '$description_placeholder_label' => $this->t('Friendiqa on my Fairphone 2...'),
140 '$generate_label' => $this->t('Generate'),