3 namespace Friendica\Module\Settings\TwoFactor;
5 use Friendica\Core\Renderer;
7 use Friendica\Module\BaseSettings;
8 use Friendica\Security\TwoFactor;
9 use Friendica\Util\Temporal;
13 * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
15 class Trusted extends BaseSettings
17 public static function init(array $parameters = [])
23 $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
26 DI::baseUrl()->redirect('settings/2fa');
29 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
30 notice(DI::l10n()->t('Please enter your password to access this page.'));
31 DI::baseUrl()->redirect('settings/2fa');
35 public static function post(array $parameters = [])
41 $trustedBrowserRepository = new TwoFactor\Repository\TrustedBrowser(DI::dba(), DI::logger());
43 if (!empty($_POST['action'])) {
44 self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
46 switch ($_POST['action']) {
48 $trustedBrowserRepository->removeAllForUser(local_user());
49 info(DI::l10n()->t('Trusted browsers successfully removed.'));
50 DI::baseUrl()->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
55 if (!empty($_POST['remove_id'])) {
56 self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
58 if ($trustedBrowserRepository->removeForUser(local_user(), $_POST['remove_id'])) {
59 info(DI::l10n()->t('Trusted browser successfully removed.'));
62 DI::baseUrl()->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
67 public static function content(array $parameters = []): string
69 parent::content($parameters);
71 $trustedBrowserRepository = new TwoFactor\Repository\TrustedBrowser(DI::dba(), DI::logger());
72 $trustedBrowsers = $trustedBrowserRepository->selectAllByUid(local_user());
74 $parser = Parser::create();
76 $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
78 'created_ago' => Temporal::getRelativeDate($trustedBrowser->created),
79 'last_used_ago' => Temporal::getRelativeDate($trustedBrowser->last_used),
82 $result = $parser->parse($trustedBrowser->user_agent);
85 'os' => $result->os->family,
86 'device' => $result->device->family,
87 'browser' => $result->ua->family,
90 return $trustedBrowser->toArray() + $dates + $uaData;
91 }, $trustedBrowsers->getArrayCopy());
93 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
94 '$form_security_token' => self::getFormSecurityToken('settings_2fa_trusted'),
95 '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
97 '$title' => DI::l10n()->t('Two-factor Trusted Browsers'),
98 '$message' => DI::l10n()->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.'),
99 '$device_label' => DI::l10n()->t('Device'),
100 '$os_label' => DI::l10n()->t('OS'),
101 '$browser_label' => DI::l10n()->t('Browser'),
102 '$created_label' => DI::l10n()->t('Trusted'),
103 '$last_used_label' => DI::l10n()->t('Last Use'),
104 '$remove_label' => DI::l10n()->t('Remove'),
105 '$remove_all_label' => DI::l10n()->t('Remove All'),
107 '$trusted_browsers' => $trustedBrowserDisplay,