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\Module\Response;
11 use Friendica\Security\TwoFactor;
12 use Friendica\Util\DateTimeFormat;
13 use Friendica\Util\Profiler;
14 use Friendica\Util\Temporal;
15 use Psr\Log\LoggerInterface;
19 * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
21 class Trusted extends BaseSettings
23 /** @var IManagePersonalConfigValues */
25 /** @var TwoFactor\Repository\TrustedBrowser */
26 protected $trustedBrowserRepo;
28 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, array $server, array $parameters = [])
30 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
32 $this->pConfig = $pConfig;
33 $this->trustedBrowserRepo = $trustedBrowserRepo;
39 $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
42 $this->baseUrl->redirect('settings/2fa');
45 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
46 notice($this->t('Please enter your password to access this page.'));
47 $this->baseUrl->redirect('settings/2fa');
51 protected function post(array $request = [])
57 if (!empty($_POST['action'])) {
58 self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
60 switch ($_POST['action']) {
62 $this->trustedBrowserRepo->removeAllForUser(local_user());
63 info($this->t('Trusted browsers successfully removed.'));
64 $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
69 if (!empty($_POST['remove_id'])) {
70 self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
72 if ($this->trustedBrowserRepo->removeForUser(local_user(), $_POST['remove_id'])) {
73 info($this->t('Trusted browser successfully removed.'));
76 $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
81 protected function content(array $request = []): string
85 $trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid(local_user());
87 $parser = Parser::create();
89 $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
91 'created_ago' => Temporal::getRelativeDate($trustedBrowser->created),
92 'created_utc' => DateTimeFormat::utc($trustedBrowser->created, 'c'),
93 'created_local' => DateTimeFormat::local($trustedBrowser->created, 'r'),
94 'last_used_ago' => Temporal::getRelativeDate($trustedBrowser->last_used),
95 'last_used_utc' => DateTimeFormat::utc($trustedBrowser->last_used, 'c'),
96 'last_used_local' => DateTimeFormat::local($trustedBrowser->last_used, 'r'),
99 $result = $parser->parse($trustedBrowser->user_agent);
102 'os' => $result->os->family,
103 'device' => $result->device->family,
104 'browser' => $result->ua->family,
107 return $trustedBrowser->toArray() + $dates + $uaData;
108 }, $trustedBrowsers->getArrayCopy());
110 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
111 '$form_security_token' => self::getFormSecurityToken('settings_2fa_trusted'),
112 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
114 '$title' => $this->t('Two-factor Trusted Browsers'),
115 '$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.'),
116 '$device_label' => $this->t('Device'),
117 '$os_label' => $this->t('OS'),
118 '$browser_label' => $this->t('Browser'),
119 '$created_label' => $this->t('Trusted'),
120 '$last_used_label' => $this->t('Last Use'),
121 '$remove_label' => $this->t('Remove'),
122 '$remove_all_label' => $this->t('Remove All'),
124 '$trusted_browsers' => $trustedBrowserDisplay,