]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/TwoFactor/Verify.php
Merge pull request #8142 from nupplaphil/task/di_config
[friendica.git] / src / Module / Security / TwoFactor / Verify.php
1 <?php
2
3 namespace Friendica\Module\Security\TwoFactor;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\Renderer;
7 use Friendica\Core\Session;
8 use Friendica\DI;
9 use PragmaRX\Google2FA\Google2FA;
10
11 /**
12  * Page 1: Authenticator code verification
13  *
14  * @package Friendica\Module\TwoFactor
15  */
16 class Verify extends BaseModule
17 {
18         private static $errors = [];
19
20         public static function post(array $parameters = [])
21         {
22                 if (!local_user()) {
23                         return;
24                 }
25
26                 if (($_POST['action'] ?? '') == 'verify') {
27                         self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_verify');
28
29                         $a = DI::app();
30
31                         $code = $_POST['verify_code'] ?? '';
32
33                         $valid = (new Google2FA())->verifyKey(DI::pConfig()->get(local_user(), '2fa', 'secret'), $code);
34
35                         // The same code can't be used twice even if it's valid
36                         if ($valid && Session::get('2fa') !== $code) {
37                                 Session::set('2fa', $code);
38
39                                 // Resume normal login workflow
40                                 DI::auth()->setForUser($a, $a->user, true, true);
41                         } else {
42                                 self::$errors[] = DI::l10n()->t('Invalid code, please retry.');
43                         }
44                 }
45         }
46
47         public static function content(array $parameters = [])
48         {
49                 if (!local_user()) {
50                         DI::baseUrl()->redirect();
51                 }
52
53                 // Already authenticated with 2FA token
54                 if (Session::get('2fa')) {
55                         DI::baseUrl()->redirect();
56                 }
57
58                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/verify.tpl'), [
59                         '$form_security_token' => self::getFormSecurityToken('twofactor_verify'),
60
61                         '$title'            => DI::l10n()->t('Two-factor authentication'),
62                         '$message'          => DI::l10n()->t('<p>Open the two-factor authentication app on your device to get an authentication code and verify your identity.</p>'),
63                         '$errors_label'     => DI::l10n()->tt('Error', 'Errors', count(self::$errors)),
64                         '$errors'           => self::$errors,
65                         '$recovery_message' => DI::l10n()->t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
66                         '$verify_code'      => ['verify_code', DI::l10n()->t('Please enter a code from your authentication app'), '', '', 'required', 'autofocus placeholder="000000"', 'tel'],
67                         '$verify_label'     => DI::l10n()->t('Verify code and complete login'),
68                 ]);
69         }
70 }