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