]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/Recovery.php
Get rid of App->user completely
[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\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
27 use Friendica\DI;
28 use Friendica\Model\User;
29 use Friendica\Security\TwoFactor\Model\RecoveryCode;
30
31 /**
32  * // Page 1a: Recovery code verification
33  *
34  * @package Friendica\Module\TwoFactor
35  */
36 class Recovery extends BaseModule
37 {
38         public static function init(array $parameters = [])
39         {
40                 if (!local_user()) {
41                         return;
42                 }
43         }
44
45         public static function post(array $parameters = [])
46         {
47                 if (!local_user()) {
48                         return;
49                 }
50
51                 if (($_POST['action'] ?? '') == 'recover') {
52                         self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_recovery');
53
54                         $a = DI::app();
55
56                         $recovery_code = $_POST['recovery_code'] ?? '';
57
58                         if (RecoveryCode::existsForUser(local_user(), $recovery_code)) {
59                                 RecoveryCode::markUsedForUser(local_user(), $recovery_code);
60                                 Session::set('2fa', true);
61                                 info(DI::l10n()->t('Remaining recovery codes: %d', RecoveryCode::countValidForUser(local_user())));
62
63                                 DI::auth()->setForUser($a, User::getById($a->getUserId()), true, true);
64                         } else {
65                                 notice(DI::l10n()->t('Invalid code, please retry.'));
66                         }
67                 }
68         }
69
70         public static function content(array $parameters = [])
71         {
72                 if (!local_user()) {
73                         DI::baseUrl()->redirect();
74                 }
75
76                 // Already authenticated with 2FA token
77                 if (Session::get('2fa')) {
78                         DI::baseUrl()->redirect();
79                 }
80
81                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/recovery.tpl'), [
82                         '$form_security_token' => self::getFormSecurityToken('twofactor_recovery'),
83
84                         '$title'            => DI::l10n()->t('Two-factor recovery'),
85                         '$message'          => DI::l10n()->t('<p>You can enter one of your one-time recovery codes in case you lost access to your mobile device.</p>'),
86                         '$recovery_message' => DI::l10n()->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
87                         '$recovery_code'    => ['recovery_code', DI::l10n()->t('Please enter a recovery code'), '', '', '', 'placeholder="000000-000000"'],
88                         '$recovery_label'   => DI::l10n()->t('Submit recovery code and complete login'),
89                 ]);
90         }
91 }