]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/Verify.php
Merge pull request #9690 from MrPetovan/bug/9672-empty-acl-public
[friendica.git] / src / Module / Security / TwoFactor / Verify.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 PragmaRX\Google2FA\Google2FA;
29
30 /**
31  * Page 1: Authenticator code verification
32  *
33  * @package Friendica\Module\TwoFactor
34  */
35 class Verify extends BaseModule
36 {
37         private static $errors = [];
38
39         public static function post(array $parameters = [])
40         {
41                 if (!local_user()) {
42                         return;
43                 }
44
45                 if (($_POST['action'] ?? '') == 'verify') {
46                         self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_verify');
47
48                         $a = DI::app();
49
50                         $code = $_POST['verify_code'] ?? '';
51
52                         $valid = (new Google2FA())->verifyKey(DI::pConfig()->get(local_user(), '2fa', 'secret'), $code);
53
54                         // The same code can't be used twice even if it's valid
55                         if ($valid && Session::get('2fa') !== $code) {
56                                 Session::set('2fa', $code);
57
58                                 // Resume normal login workflow
59                                 DI::auth()->setForUser($a, $a->user, true, true);
60                         } else {
61                                 self::$errors[] = DI::l10n()->t('Invalid code, please retry.');
62                         }
63                 }
64         }
65
66         public static function content(array $parameters = [])
67         {
68                 if (!local_user()) {
69                         DI::baseUrl()->redirect();
70                 }
71
72                 // Already authenticated with 2FA token
73                 if (Session::get('2fa')) {
74                         DI::baseUrl()->redirect();
75                 }
76
77                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/verify.tpl'), [
78                         '$form_security_token' => self::getFormSecurityToken('twofactor_verify'),
79
80                         '$title'            => DI::l10n()->t('Two-factor authentication'),
81                         '$message'          => DI::l10n()->t('<p>Open the two-factor authentication app on your device to get an authentication code and verify your identity.</p>'),
82                         '$errors_label'     => DI::l10n()->tt('Error', 'Errors', count(self::$errors)),
83                         '$errors'           => self::$errors,
84                         '$recovery_message' => DI::l10n()->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
85                         '$verify_code'      => ['verify_code', DI::l10n()->t('Please enter a code from your authentication app'), '', '', DI::l10n()->t('Required'), 'autofocus autocomplete="off" placeholder="000000"', 'tel'],
86                         '$verify_label'     => DI::l10n()->t('Verify code and complete login'),
87                 ]);
88         }
89 }