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\Temporal;
15 * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
17 class Trusted extends BaseSettings
19 /** @var IManagePersonalConfigValues */
23 /** @var TwoFactor\Repository\TrustedBrowser */
24 protected $trustedBrowserRepo;
26 public function __construct(IManagePersonalConfigValues $pConfig, BaseURL $baseUrl, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, L10n $l10n, array $parameters = [])
28 parent::__construct($l10n, $parameters);
30 $this->pConfig = $pConfig;
31 $this->baseUrl = $baseUrl;
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 public function 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 public function content(): 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 'last_used_ago' => Temporal::getRelativeDate($trustedBrowser->last_used),
94 $result = $parser->parse($trustedBrowser->user_agent);
97 'os' => $result->os->family,
98 'device' => $result->device->family,
99 'browser' => $result->ua->family,
102 return $trustedBrowser->toArray() + $dates + $uaData;
103 }, $trustedBrowsers->getArrayCopy());
105 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
106 '$form_security_token' => self::getFormSecurityToken('settings_2fa_trusted'),
107 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
109 '$title' => $this->t('Two-factor Trusted Browsers'),
110 '$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.'),
111 '$device_label' => $this->t('Device'),
112 '$os_label' => $this->t('OS'),
113 '$browser_label' => $this->t('Browser'),
114 '$created_label' => $this->t('Trusted'),
115 '$last_used_label' => $this->t('Last Use'),
116 '$remove_label' => $this->t('Remove'),
117 '$remove_all_label' => $this->t('Remove All'),
119 '$trusted_browsers' => $trustedBrowserDisplay,