3 namespace Friendica\Module\Security\TwoFactor;
5 use Friendica\BaseModule;
6 use Friendica\Core\Renderer;
7 use Friendica\Core\Session;
9 use PragmaRX\Google2FA\Google2FA;
12 * Page 1: Authenticator code verification
14 * @package Friendica\Module\TwoFactor
16 class Verify extends BaseModule
18 private static $errors = [];
20 public static function post(array $parameters = [])
26 if (($_POST['action'] ?? '') == 'verify') {
27 self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_verify');
31 $code = $_POST['verify_code'] ?? '';
33 $valid = (new Google2FA())->verifyKey(DI::pConfig()->get(local_user(), '2fa', 'secret'), $code);
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);
39 // Resume normal login workflow
40 DI::auth()->setForUser($a, $a->user, true, true);
42 self::$errors[] = DI::l10n()->t('Invalid code, please retry.');
47 public static function content(array $parameters = [])
50 DI::baseUrl()->redirect();
53 // Already authenticated with 2FA token
54 if (Session::get('2fa')) {
55 DI::baseUrl()->redirect();
58 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/verify.tpl'), [
59 '$form_security_token' => self::getFormSecurityToken('twofactor_verify'),
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'),