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