]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/Verify.php
Add more 2fa properties
[friendica.git] / src / Module / Security / TwoFactor / Verify.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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 PragmaRX\Google2FA\Google2FA;
30 use Friendica\Security\TwoFactor;
31
32 /**
33  * Page 1: Authenticator code verification
34  *
35  * @package Friendica\Module\TwoFactor
36  */
37 class Verify extends BaseModule
38 {
39         private static $errors = [];
40
41         protected function post(array $request = [])
42         {
43                 if (!local_user()) {
44                         return;
45                 }
46
47                 if (($_POST['action'] ?? '') == 'verify') {
48                         self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_verify');
49
50                         $a = DI::app();
51
52                         $code = $_POST['verify_code'] ?? '';
53
54                         $valid = (new Google2FA())->verifyKey(DI::pConfig()->get(local_user(), '2fa', 'secret'), $code);
55
56                         // The same code can't be used twice even if it's valid
57                         if ($valid && Session::get('2fa') !== $code) {
58                                 Session::set('2fa', $code);
59
60                                 // Trust this browser feature
61                                 if (!empty($_REQUEST['trust_browser'])) {
62                                         $trustedBrowserFactory = new TwoFactor\Factory\TrustedBrowser(DI::logger());
63                                         $trustedBrowserRepository = new TwoFactor\Repository\TrustedBrowser(DI::dba(), DI::logger(), $trustedBrowserFactory);
64
65                                         $trustedBrowser = $trustedBrowserFactory->createForUserWithUserAgent(local_user(), $_SERVER['HTTP_USER_AGENT']);
66
67                                         $trustedBrowserRepository->save($trustedBrowser);
68
69                                         // The string is sent to the browser to be sent back with each request
70                                         DI::cookie()->set('trusted', $trustedBrowser->cookie_hash);
71                                 }
72
73                                 // Resume normal login workflow
74                                 DI::auth()->setForUser($a, User::getById($a->getLoggedInUserId()), true, true);
75                         } else {
76                                 self::$errors[] = DI::l10n()->t('Invalid code, please retry.');
77                         }
78                 }
79         }
80
81         protected function content(array $request = []): string
82         {
83                 if (!local_user()) {
84                         DI::baseUrl()->redirect();
85                 }
86
87                 // Already authenticated with 2FA token
88                 if (Session::get('2fa')) {
89                         DI::baseUrl()->redirect();
90                 }
91
92                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/verify.tpl'), [
93                         '$form_security_token' => self::getFormSecurityToken('twofactor_verify'),
94
95                         '$title'            => DI::l10n()->t('Two-factor authentication'),
96                         '$message'          => DI::l10n()->t('<p>Open the two-factor authentication app on your device to get an authentication code and verify your identity.</p>'),
97                         '$errors_label'     => DI::l10n()->tt('Error', 'Errors', count(self::$errors)),
98                         '$errors'           => self::$errors,
99                         '$recovery_message' => DI::l10n()->t('If you do not have access to your authentication code you can use <a href="%s">a two-factor recovery code</a>.', '2fa/recovery'),
100                         '$verify_code'      => ['verify_code', DI::l10n()->t('Please enter a code from your authentication app'), '', '', DI::l10n()->t('Required'), 'autofocus autocomplete="one-time-code" placeholder="000000" inputmode="numeric" pattern="[0-9]*"', 'tel'],
101                         '$trust_browser'    => ['trust_browser', DI::l10n()->t('This is my two-factor authenticator app device'), !empty($_REQUEST['trust_browser'])],
102                         '$verify_label'     => DI::l10n()->t('Verify code and complete login'),
103                 ]);
104         }
105 }