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