3 * @copyright Copyright (C) 2010-2023, 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;
29 use Friendica\Module\BaseSettings;
30 use Friendica\Module\Response;
31 use Friendica\Module\Security\Login;
32 use Friendica\Navigation\SystemMessages;
33 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
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 */
48 /** @var SystemMessages */
49 protected $systemMessages;
51 public function __construct(SystemMessages $systemMessages, 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 = [])
53 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55 $this->pConfig = $pConfig;
56 $this->systemMessages = $systemMessages;
58 if (!$this->session->getLocalUserId()) {
62 $verified = $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'verified');
65 $this->baseUrl->redirect('settings/2fa');
68 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
69 $this->systemMessages->addNotice($this->t('Please enter your password to access this page.'));
70 $this->baseUrl->redirect('settings/2fa');
74 protected function post(array $request = [])
76 if (!$this->session->getLocalUserId()) {
80 if (!empty($request['action'])) {
81 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
83 switch ($request['action']) {
85 $description = $request['description'] ?? '';
86 if (empty($description)) {
87 $this->systemMessages->addNotice($this->t('App-specific password generation failed: The description is empty.'));
88 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
89 } elseif (AppSpecificPassword::checkDuplicateForUser($this->session->getLocalUserId(), $description)) {
90 $this->systemMessages->addNotice($this->t('App-specific password generation failed: This description already exists.'));
91 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
93 $this->appSpecificPassword = AppSpecificPassword::generateForUser($this->session->getLocalUserId(), $request['description'] ?? '');
94 $this->systemMessages->addInfo($this->t('New app-specific password generated.'));
99 AppSpecificPassword::deleteAllForUser($this->session->getLocalUserId());
100 $this->systemMessages->addInfo($this->t('App-specific passwords successfully revoked.'));
101 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
106 if (!empty($request['revoke_id'])) {
107 self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
109 if (AppSpecificPassword::deleteForUser($this->session->getLocalUserId(), $request['revoke_id'])) {
110 $this->systemMessages->addInfo($this->t('App-specific password successfully revoked.'));
113 $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
117 protected function content(array $request = []): string
119 if (!$this->session->getLocalUserId()) {
120 return Login::form('settings/2fa/app_specific');
125 $appSpecificPasswords = AppSpecificPassword::getListForUser($this->session->getLocalUserId());
127 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/app_specific.tpl'), [
128 '$form_security_token' => self::getFormSecurityToken('settings_2fa_app_specific'),
129 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
131 '$title' => $this->t('Two-factor app-specific passwords'),
132 '$help_label' => $this->t('Help'),
133 '$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>'),
134 '$generated_message' => $this->t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
135 '$generated_app_specific_password' => $this->appSpecificPassword,
137 '$description_label' => $this->t('Description'),
138 '$last_used_label' => $this->t('Last Used'),
139 '$revoke_label' => $this->t('Revoke'),
140 '$revoke_all_label' => $this->t('Revoke All'),
142 '$app_specific_passwords' => $appSpecificPasswords,
143 '$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.'),
144 '$generate_title' => $this->t('Generate new app-specific password'),
145 '$description_placeholder_label' => $this->t('Friendiqa on my Fairphone 2...'),
146 '$generate_label' => $this->t('Generate'),