3 namespace Friendica\Module\Settings\TwoFactor;
5 use Friendica\App\BaseURL;
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\Temporal;
16 * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
18 class Trusted extends BaseSettings
20 /** @var IManagePersonalConfigValues */
24 /** @var TwoFactor\Repository\TrustedBrowser */
25 protected $trustedBrowserRepo;
27 public function __construct(IManagePersonalConfigValues $pConfig, BaseURL $baseUrl, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, L10n $l10n, array $parameters = [])
29 parent::__construct($l10n, $parameters);
31 $this->pConfig = $pConfig;
32 $this->baseUrl = $baseUrl;
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 public function post()
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 public function content(): 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,