]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/Recovery.php
Merge remote-tracking branch 'upstream/develop' into api-rework
[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\Security\Authentication;
31 use Friendica\Security\TwoFactor\Model\RecoveryCode;
32
33 /**
34  * // Page 1a: Recovery code verification
35  *
36  * @package Friendica\Module\TwoFactor
37  */
38 class Recovery extends BaseModule
39 {
40         /** @var IHandleSessions */
41         protected $session;
42         /** @var App */
43         protected $app;
44         /** @var App\BaseURL */
45         protected $baseUrl;
46         /** @var Authentication */
47         protected $auth;
48
49         public function __construct(App $app, App\BaseURL $baseUrl, Authentication $auth, IHandleSessions $session, L10n $l10n, array $parameters = [])
50         {
51                 parent::__construct($l10n, $parameters);
52
53                 $this->app     = $app;
54                 $this->baseUrl = $baseUrl;
55                 $this->auth    = $auth;
56                 $this->session = $session;
57         }
58
59         public function post()
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         public function content(): 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 }