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