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