]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/Trust.php
adaptions
[friendica.git] / src / Module / Security / TwoFactor / Trust.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Security\TwoFactor;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleSessions;
29 use Friendica\Model\User;
30 use Friendica\Model\User\Cookie;
31 use Friendica\Module\Response;
32 use Friendica\Network\HTTPException\NotFoundException;
33 use Friendica\Security\Authentication;
34 use Friendica\Util\Profiler;
35 use Friendica\Security\TwoFactor;
36 use Psr\Log\LoggerInterface;
37
38 /**
39  * Page 2: Trust Browser dialog
40  *
41  * @package Friendica\Module\TwoFactor
42  */
43 class Trust extends BaseModule
44 {
45         /** @var App  */
46         protected $app;
47         /** @var Authentication  */
48         protected $auth;
49         /** @var IHandleSessions  */
50         protected $session;
51         /** @var Cookie  */
52         protected $cookie;
53         /** @var TwoFactor\Factory\TrustedBrowser  */
54         protected $trustedBrowserFactory;
55         /** @var TwoFactor\Repository\TrustedBrowser  */
56         protected $trustedBrowserRepositoy;
57
58         public function __construct(App $app, Authentication $auth, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, IHandleSessions $session, Cookie $cookie, TwoFactor\Factory\TrustedBrowser $trustedBrowserFactory, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepositoy, Response $response, array $server, array $parameters = [])
59         {
60                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
61
62                 $this->app                     = $app;
63                 $this->auth                    = $auth;
64                 $this->session                 = $session;
65                 $this->cookie                  = $cookie;
66                 $this->trustedBrowserFactory   = $trustedBrowserFactory;
67                 $this->trustedBrowserRepositoy = $trustedBrowserRepositoy;
68         }
69
70         protected function post(array $request = [])
71         {
72                 if (!local_user() || !$this->session->get('2fa')) {
73                         return;
74                 }
75
76                 $action = $request['action'] ?? '';
77
78                 if (!empty($action)) {
79                         self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_trust');
80
81                         switch ($action) {
82                                 case 'trust':
83                                 case 'dont_trust':
84                                         $trustedBrowser = $this->trustedBrowserFactory->createForUserWithUserAgent(local_user(), $this->server['HTTP_USER_AGENT'], $action === 'trust');
85                                         $this->trustedBrowserRepositoy->save($trustedBrowser);
86
87                                         // The string is sent to the browser to be sent back with each request
88                                         if (!$this->cookie->set('2fa_cookie_hash', $trustedBrowser->cookie_hash)) {
89                                                 notice($this->t('Couldn\'t save browser to Cookie.'));
90                                         };
91                                         break;
92                         }
93
94                         $this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
95                 }
96         }
97
98         protected function content(array $request = []): string
99         {
100                 if (!local_user() || !$this->session->get('2fa')) {
101                         $this->baseUrl->redirect();
102                 }
103
104                 if ($this->cookie->get('2fa_cookie_hash')) {
105                         try {
106                                 $trustedBrowser = $this->trustedBrowserRepositoy->selectOneByHash($this->cookie->get('2fa_cookie_hash'));
107                                 if (!$trustedBrowser->trusted) {
108                                         $this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
109                                         $this->baseUrl->redirect();
110                                 }
111                         } catch (NotFoundException $exception) {
112                                 $this->logger->notice('Trusted Browser of the cookie not found.', ['cookie_hash' => $this->cookie->get('trusted'), 'uid' => $this->app->getLoggedInUserId(), 'exception' => $exception]);
113                         }
114                 }
115
116                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/trust.tpl'), [
117                         '$form_security_token' => self::getFormSecurityToken('twofactor_trust'),
118
119                         '$title'            => $this->t('Trust this browser?'),
120                         '$message'          => $this->t('<p>If you choose to trust this browser, you will not be asked for a verification code the next time you sign in.</p>'),
121                         '$not_now_label'    => $this->t('Not now'),
122                         '$dont_trust_label' => $this->t('Don\'t trust'),
123                         '$trust_label'      => $this->t('Trust'),
124                 ]);
125         }
126 }