]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Trusted.php
Merge branch 'friendica:2023.09-rc' into Leftovers-from-PR-#13339
[friendica.git] / src / Module / Settings / TwoFactor / Trusted.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings\TwoFactor;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Module\BaseSettings;
30 use Friendica\Module\Response;
31 use Friendica\Navigation\SystemMessages;
32 use Friendica\Security\TwoFactor;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Profiler;
35 use Friendica\Util\Temporal;
36 use Psr\Log\LoggerInterface;
37 use UAParser\Parser;
38
39 /**
40  * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
41  */
42 class Trusted extends BaseSettings
43 {
44         /** @var IManagePersonalConfigValues */
45         protected $pConfig;
46         /** @var TwoFactor\Repository\TrustedBrowser */
47         protected $trustedBrowserRepo;
48         /** @var SystemMessages */
49         protected $systemMessages;
50
51         public function __construct(SystemMessages $systemMessages, IManagePersonalConfigValues $pConfig, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
52         {
53                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
54
55                 $this->pConfig            = $pConfig;
56                 $this->trustedBrowserRepo = $trustedBrowserRepo;
57                 $this->systemMessages     = $systemMessages;
58
59                 if (!$this->session->getLocalUserId()) {
60                         return;
61                 }
62
63                 $verified = $this->pConfig->get($this->session->getLocalUserId(), '2fa', 'verified');
64
65                 if (!$verified) {
66                         $this->baseUrl->redirect('settings/2fa');
67                 }
68
69                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
70                         $this->systemMessages->addNotice($this->t('Please enter your password to access this page.'));
71                         $this->baseUrl->redirect('settings/2fa');
72                 }
73         }
74
75         protected function post(array $request = [])
76         {
77                 if (!$this->session->getLocalUserId()) {
78                         return;
79                 }
80
81                 if (!empty($request['action'])) {
82                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
83
84                         switch ($request['action']) {
85                                 case 'remove_all':
86                                         $this->trustedBrowserRepo->removeAllForUser($this->session->getLocalUserId());
87                                         $this->systemMessages->addInfo($this->t('Trusted browsers successfully removed.'));
88                                         $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
89                                         break;
90                         }
91                 }
92
93                 if (!empty($request['remove_id'])) {
94                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
95
96                         if ($this->trustedBrowserRepo->removeForUser($this->session->getLocalUserId(), $request['remove_id'])) {
97                                 $this->systemMessages->addInfo($this->t('Trusted browser successfully removed.'));
98                         }
99
100                         $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
101                 }
102         }
103
104
105         protected function content(array $request = []): string
106         {
107                 parent::content();
108
109                 $trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid($this->session->getLocalUserId());
110
111                 $parser = Parser::create();
112
113                 $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
114                         $dates = [
115                                 'created_ago'     => Temporal::getRelativeDate($trustedBrowser->created),
116                                 'created_utc'     => DateTimeFormat::utc($trustedBrowser->created, 'c'),
117                                 'created_local'   => DateTimeFormat::local($trustedBrowser->created, 'r'),
118                                 'last_used_ago'   => Temporal::getRelativeDate($trustedBrowser->last_used),
119                                 'last_used_utc'   => $trustedBrowser->last_used ? DateTimeFormat::utc($trustedBrowser->last_used, 'c') : '',
120                                 'last_used_local' => $trustedBrowser->last_used ? DateTimeFormat::local($trustedBrowser->last_used, 'r') : '',
121                         ];
122
123                         $result = $parser->parse($trustedBrowser->user_agent);
124
125                         $uaData = [
126                                 'os'              => $result->os->family,
127                                 'device'          => $result->device->family,
128                                 'browser'         => $result->ua->family,
129                                 'trusted_labeled' => $trustedBrowser->trusted ? $this->t('Yes') : $this->t('No'),
130                         ];
131
132                         return $trustedBrowser->toArray() + $dates + $uaData;
133                 }, $trustedBrowsers->getArrayCopy());
134
135                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
136                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_trusted'),
137                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
138
139                         '$title'            => $this->t('Two-factor Trusted Browsers'),
140                         '$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.'),
141                         '$device_label'     => $this->t('Device'),
142                         '$os_label'         => $this->t('OS'),
143                         '$browser_label'    => $this->t('Browser'),
144                         '$trusted_label'    => $this->t('Trusted'),
145                         '$created_label'    => $this->t('Created At'),
146                         '$last_used_label'  => $this->t('Last Use'),
147                         '$remove_label'     => $this->t('Remove'),
148                         '$remove_all_label' => $this->t('Remove All'),
149
150                         '$trusted_browsers' => $trustedBrowserDisplay,
151                 ]);
152         }
153 }