]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/SignOut.php
Add a lot of log-points
[friendica.git] / src / Module / Security / TwoFactor / SignOut.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\Cookie;
30 use Friendica\Module\Response;
31 use Friendica\Network\HTTPException\NotFoundException;
32 use Friendica\Util\Profiler;
33 use Friendica\Security\TwoFactor;
34 use Psr\Log\LoggerInterface;
35
36 /**
37  * Page 4: Logout dialog for trusted browsers
38  *
39  * @package Friendica\Module\TwoFactor
40  */
41 class SignOut extends BaseModule
42 {
43         protected $errors = [];
44
45         /** @var IHandleSessions  */
46         protected $session;
47         /** @var Cookie  */
48         protected $cookie;
49         /** @var TwoFactor\Repository\TrustedBrowser  */
50         protected $trustedBrowserRepository;
51
52         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger,  IHandleSessions $session, Cookie $cookie, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepository, Profiler $profiler, Response $response, array $server, array $parameters = [])
53         {
54                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55
56                 $this->session                  = $session;
57                 $this->cookie                   = $cookie;
58                 $this->trustedBrowserRepository = $trustedBrowserRepository;
59         }
60
61         protected function post(array $request = [])
62         {
63                 if (!local_user() || !($this->cookie->get('2fa_cookie_hash'))) {
64                         return;
65                 }
66
67                 $action = $request['action'] ?? '';
68
69                 if (!empty($action)) {
70                         self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_signout');
71
72                         switch ($action) {
73                                 case 'trust_and_sign_out':
74                                         $trusted = $this->cookie->get('2fa_cookie_hash');
75                                         $this->cookie->reset(['2fa_cookie_hash' => $trusted]);
76                                         $this->session->clear();
77
78                                         info($this->t('Logged out.'));
79                                         $this->baseUrl->redirect();
80                                         break;
81                                 case 'sign_out':
82                                         $this->trustedBrowserRepository->removeForUser(local_user(), $this->cookie->get('2fa_cookie_hash'));
83                                         $this->cookie->clear();
84                                         $this->session->clear();
85
86                                         info($this->t('Logged out.'));
87                                         $this->baseUrl->redirect();
88                                         break;
89                                 default:
90                                         $this->baseUrl->redirect();
91                         }
92                 }
93         }
94
95         protected function content(array $request = []): string
96         {
97                 if (!local_user() || !($this->cookie->get('2fa_cookie_hash'))) {
98                         $this->baseUrl->redirect();
99                 }
100
101                 try {
102                         $trustedBrowser = $this->trustedBrowserRepository->selectOneByHash($this->cookie->get('2fa_cookie_hash'));
103                         if (!$trustedBrowser->trusted) {
104                                 $trusted = $this->cookie->get('2fa_cookie_hash');
105                                 $this->cookie->reset(['2fa_cookie_hash' => $trusted]);
106                                 $this->session->clear();
107
108                                 info($this->t('Logged out.'));
109                                 $this->baseUrl->redirect();
110                         }
111                 } catch (TwoFactor\Exception\TrustedBrowserNotFoundException $exception) {
112                         $this->cookie->clear();
113                         $this->session->clear();
114
115                         info($this->t('Logged out.'));
116                         $this->baseUrl->redirect();
117                 }
118
119                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/signout.tpl'), [
120                         '$form_security_token' => self::getFormSecurityToken('twofactor_signout'),
121
122                         '$title'                    => $this->t('Sign out of this browser?'),
123                         '$message'                  => $this->t('<p>If you trust this browser, you will not be asked for verification code the next time you sign in.</p>'),
124                         '$sign_out_label'           => $this->t('Sign out'),
125                         '$cancel_label'             => $this->t('Cancel'),
126                         '$trust_and_sign_out_label' => $this->t('Trust and sign out'),
127                 ]);
128         }
129 }