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\Navigation\SystemMessages;
32 use Friendica\Security\TwoFactor;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Profiler;
35 use Friendica\Util\Temporal;
36 use Psr\Log\LoggerInterface;
40 * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
42 class Trusted extends BaseSettings
44 /** @var IManagePersonalConfigValues */
46 /** @var TwoFactor\Repository\TrustedBrowser */
47 protected $trustedBrowserRepo;
48 /** @var SystemMessages */
49 protected $systemMessages;
51 public function __construct(SystemMessages $systemMessages, IManagePersonalConfigValues $pConfig, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, 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->trustedBrowserRepo = $trustedBrowserRepo;
57 $this->systemMessages = $systemMessages;
59 if (!$this->session->getLocalUserId()) {
63 $verified = $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'verified');
66 $this->baseUrl->redirect('settings/2fa');
69 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
70 $this->systemMessages->addNotice($this->t('Please enter your password to access this page.'));
71 $this->baseUrl->redirect('settings/2fa');
75 protected function post(array $request = [])
77 if (!$this->session->getLocalUserId()) {
81 if (!empty($request['action'])) {
82 self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
84 switch ($request['action']) {
86 $this->trustedBrowserRepo->removeAllForUser($this->session->getLocalUserId());
87 $this->systemMessages->addInfo($this->t('Trusted browsers successfully removed.'));
88 $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
93 if (!empty($request['remove_id'])) {
94 self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
96 if ($this->trustedBrowserRepo->removeForUser($this->session->getLocalUserId(), $request['remove_id'])) {
97 $this->systemMessages->addInfo($this->t('Trusted browser successfully removed.'));
100 $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
105 protected function content(array $request = []): string
109 $trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid($this->session->getLocalUserId());
111 $parser = Parser::create();
113 $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
115 'created_ago' => Temporal::getRelativeDate($trustedBrowser->created),
116 'created_utc' => DateTimeFormat::utc($trustedBrowser->created, 'c'),
117 'created_local' => DateTimeFormat::local($trustedBrowser->created, 'r'),
118 'last_used_ago' => Temporal::getRelativeDate($trustedBrowser->last_used),
119 'last_used_utc' => $trustedBrowser->last_used ? DateTimeFormat::utc($trustedBrowser->last_used, 'c') : '',
120 'last_used_local' => $trustedBrowser->last_used ? DateTimeFormat::local($trustedBrowser->last_used, 'r') : '',
123 $result = $parser->parse($trustedBrowser->user_agent);
126 'os' => $result->os->family,
127 'device' => $result->device->family,
128 'browser' => $result->ua->family,
129 'trusted_labeled' => $trustedBrowser->trusted ? $this->t('Yes') : $this->t('No'),
132 return $trustedBrowser->toArray() + $dates + $uaData;
133 }, $trustedBrowsers->getArrayCopy());
135 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
136 '$form_security_token' => self::getFormSecurityToken('settings_2fa_trusted'),
137 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
139 '$title' => $this->t('Two-factor Trusted Browsers'),
140 '$message' => $this->t('Trusted browsers are individual browsers you chose to skip two-factor authentication to access Friendica. Please use this feature sparingly, as it can negate the benefit of two-factor authentication.'),
141 '$device_label' => $this->t('Device'),
142 '$os_label' => $this->t('OS'),
143 '$browser_label' => $this->t('Browser'),
144 '$trusted_label' => $this->t('Trusted'),
145 '$created_label' => $this->t('Created At'),
146 '$last_used_label' => $this->t('Last Use'),
147 '$remove_label' => $this->t('Remove'),
148 '$remove_all_label' => $this->t('Remove All'),
150 '$trusted_browsers' => $trustedBrowserDisplay,