3 * @copyright Copyright (C) 2010-2022, 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\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Module\Response;
31 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
32 use Friendica\Module\BaseSettings;
33 use Friendica\Module\Security\Login;
34 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
38 * // Page 5: 2FA enabled, app-specific password generation
40 * @package Friendica\Module\TwoFactor
42 class AppSpecific extends BaseSettings
44 private $appSpecificPassword = null;
46 /** @var IManagePersonalConfigValues */
49 public function __construct(IManagePersonalConfigValues $pConfig, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
51 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
53 $this->pConfig = $pConfig;
55 if (!DI::userSession()->getLocalUserId()) {
59 $verified = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
62 $this->baseUrl->redirect('settings/2fa');
65 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
66 DI::sysmsg()->addNotice($this->t('Please enter your password to access this page.'));
67 $this->baseUrl->redirect('settings/2fa');
71 protected function post(array $request = [])
73 if (!DI::userSession()->getLocalUserId()) {
77 if (!empty($_POST['action'])) {
78 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
80 switch ($_POST['action']) {
82 $description = $_POST['description'] ?? '';
83 if (empty($description)) {
84 DI::sysmsg()->addNotice($this->t('App-specific password generation failed: The description is empty.'));
85 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
86 } elseif (AppSpecificPassword::checkDuplicateForUser(DI::userSession()->getLocalUserId(), $description)) {
87 DI::sysmsg()->addNotice($this->t('App-specific password generation failed: This description already exists.'));
88 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
90 $this->appSpecificPassword = AppSpecificPassword::generateForUser(DI::userSession()->getLocalUserId(), $_POST['description'] ?? '');
91 DI::sysmsg()->addInfo($this->t('New app-specific password generated.'));
96 AppSpecificPassword::deleteAllForUser(DI::userSession()->getLocalUserId());
97 DI::sysmsg()->addInfo($this->t('App-specific passwords successfully revoked.'));
98 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
103 if (!empty($_POST['revoke_id'])) {
104 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
106 if (AppSpecificPassword::deleteForUser(DI::userSession()->getLocalUserId(), $_POST['revoke_id'])) {
107 DI::sysmsg()->addInfo($this->t('App-specific password successfully revoked.'));
110 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
114 protected function content(array $request = []): string
116 if (!DI::userSession()->getLocalUserId()) {
117 return Login::form('settings/2fa/app_specific');
122 $appSpecificPasswords = AppSpecificPassword::getListForUser(DI::userSession()->getLocalUserId());
124 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/app_specific.tpl'), [
125 '$form_security_token' => self::getFormSecurityToken('settings_2fa_app_specific'),
126 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
128 '$title' => $this->t('Two-factor app-specific passwords'),
129 '$help_label' => $this->t('Help'),
130 '$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>'),
131 '$generated_message' => $this->t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
132 '$generated_app_specific_password' => $this->appSpecificPassword,
134 '$description_label' => $this->t('Description'),
135 '$last_used_label' => $this->t('Last Used'),
136 '$revoke_label' => $this->t('Revoke'),
137 '$revoke_all_label' => $this->t('Revoke All'),
139 '$app_specific_passwords' => $appSpecificPasswords,
140 '$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.'),
141 '$generate_title' => $this->t('Generate new app-specific password'),
142 '$description_placeholder_label' => $this->t('Friendiqa on my Fairphone 2...'),
143 '$generate_label' => $this->t('Generate'),