3 namespace Friendica\Module\Settings\TwoFactor;
6 use Friendica\Core\L10n;
7 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
8 use Friendica\Core\Renderer;
9 use Friendica\Module\BaseSettings;
10 use Friendica\Security\TwoFactor;
11 use Friendica\Util\DateTimeFormat;
12 use Friendica\Util\Profiler;
13 use Friendica\Util\Temporal;
14 use Psr\Log\LoggerInterface;
18 * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
20 class Trusted extends BaseSettings
22 /** @var IManagePersonalConfigValues */
24 /** @var TwoFactor\Repository\TrustedBrowser */
25 protected $trustedBrowserRepo;
27 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, IManagePersonalConfigValues $pConfig, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, array $server, array $parameters = [])
29 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $server, $parameters);
31 $this->pConfig = $pConfig;
32 $this->trustedBrowserRepo = $trustedBrowserRepo;
38 $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
41 $this->baseUrl->redirect('settings/2fa');
44 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
45 notice($this->t('Please enter your password to access this page.'));
46 $this->baseUrl->redirect('settings/2fa');
50 protected function post(array $request = [], array $post = [])
56 if (!empty($_POST['action'])) {
57 self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
59 switch ($_POST['action']) {
61 $this->trustedBrowserRepo->removeAllForUser(local_user());
62 info($this->t('Trusted browsers successfully removed.'));
63 $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
68 if (!empty($_POST['remove_id'])) {
69 self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
71 if ($this->trustedBrowserRepo->removeForUser(local_user(), $_POST['remove_id'])) {
72 info($this->t('Trusted browser successfully removed.'));
75 $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
80 protected function content(array $request = []): string
84 $trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid(local_user());
86 $parser = Parser::create();
88 $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
90 'created_ago' => Temporal::getRelativeDate($trustedBrowser->created),
91 'created_utc' => DateTimeFormat::utc($trustedBrowser->created, 'c'),
92 'created_local' => DateTimeFormat::local($trustedBrowser->created, 'r'),
93 'last_used_ago' => Temporal::getRelativeDate($trustedBrowser->last_used),
94 'last_used_utc' => DateTimeFormat::utc($trustedBrowser->last_used, 'c'),
95 'last_used_local' => DateTimeFormat::local($trustedBrowser->last_used, 'r'),
98 $result = $parser->parse($trustedBrowser->user_agent);
101 'os' => $result->os->family,
102 'device' => $result->device->family,
103 'browser' => $result->ua->family,
106 return $trustedBrowser->toArray() + $dates + $uaData;
107 }, $trustedBrowsers->getArrayCopy());
109 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
110 '$form_security_token' => self::getFormSecurityToken('settings_2fa_trusted'),
111 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
113 '$title' => $this->t('Two-factor Trusted Browsers'),
114 '$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.'),
115 '$device_label' => $this->t('Device'),
116 '$os_label' => $this->t('OS'),
117 '$browser_label' => $this->t('Browser'),
118 '$created_label' => $this->t('Trusted'),
119 '$last_used_label' => $this->t('Last Use'),
120 '$remove_label' => $this->t('Remove'),
121 '$remove_all_label' => $this->t('Remove All'),
123 '$trusted_browsers' => $trustedBrowserDisplay,