]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/Recovery.php
old boot.php functions replaced in src/module (4)
[friendica.git] / src / Module / Security / TwoFactor / Recovery.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;
29 use Friendica\Core\Session\Capability\IHandleSessions;
30 use Friendica\DI;
31 use Friendica\Model\User;
32 use Friendica\Module\Response;
33 use Friendica\Security\Authentication;
34 use Friendica\Security\TwoFactor\Model\RecoveryCode;
35 use Friendica\Util\Profiler;
36 use Psr\Log\LoggerInterface;
37
38 /**
39  * // Page 1a: Recovery code verification
40  *
41  * @package Friendica\Module\TwoFactor
42  */
43 class Recovery extends BaseModule
44 {
45         /** @var IHandleSessions */
46         protected $session;
47         /** @var App */
48         protected $app;
49         /** @var Authentication */
50         protected $auth;
51
52         public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Authentication $auth, IHandleSessions $session, array $server, array $parameters = [])
53         {
54                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55
56                 $this->app     = $app;
57                 $this->auth    = $auth;
58                 $this->session = $session;
59         }
60
61         protected function post(array $request = [])
62         {
63                 if (!Session::getLocalUser()) {
64                         return;
65                 }
66
67                 if (($_POST['action'] ?? '') == 'recover') {
68                         self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
69
70                         $recovery_code = $_POST['recovery_code'] ?? '';
71
72                         if (RecoveryCode::existsForUser(Session::getLocalUser(), $recovery_code)) {
73                                 RecoveryCode::markUsedForUser(Session::getLocalUser(), $recovery_code);
74                                 $this->session->set('2fa', true);
75                                 DI::sysmsg()->addInfo($this->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(Session::getLocalUser())));
76
77                                 $this->auth->setForUser($this->app, User::getById($this->app->getLoggedInUserId()), true, true);
78
79                                 $this->baseUrl->redirect($this->session->pop('return_path', ''));
80                         } else {
81                                 DI::sysmsg()->addNotice($this->t('Invalid code, please retry.'));
82                         }
83                 }
84         }
85
86         protected function content(array $request = []): string
87         {
88                 if (!Session::getLocalUser()) {
89                         $this->baseUrl->redirect();
90                 }
91
92                 // Already authenticated with 2FA token
93                 if ($this->session->get('2fa')) {
94                         $this->baseUrl->redirect();
95                 }
96
97                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
98                         '$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
99
100                         '$title'            => $this->t('Two-factor recovery'),
101                         '$message'          => $this->t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
102                         '$recovery_message' => $this->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
103                         '$recovery_code'    => ['recovery_code', $this->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
104                         '$recovery_label'   => $this->t('Submit recovery code and complete login'),
105                 ]);
106         }
107 }