3 namespace Friendica\Module\TwoFactor;
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;
13 * Page 1: Authenticator code verification
15 * @package Friendica\Module\TwoFactor
17 class Verify extends BaseModule
19 public static function post()
25 if (defaults($_POST, 'action', null) == 'verify') {
26 self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_verify');
30 $code = defaults($_POST, 'verify_code', '');
32 $valid = (new Google2FA())->verifyKey(PConfig::get(local_user(), '2fa', 'secret'), $code);
34 // The same code can't be used twice even if it's valid
35 if ($valid && Session::get('2fa') !== $code) {
36 Session::set('2fa', $code);
38 // Resume normal login workflow
39 Session::setAuthenticatedForUser($a, $a->user, true, true);
41 notice(L10n::t('Invalid code, please retry.'));
46 public static function content()
49 self::getApp()->internalRedirect();
52 // Already authenticated with 2FA token
53 if (Session::get('2fa')) {
54 self::getApp()->internalRedirect();
57 return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/verify.tpl'), [
58 '$form_security_token' => self::getFormSecurityToken('twofactor_verify'),
60 '$title' => L10n::t('Two-factor authentication'),
61 '$message' => L10n::t('<p>Open the two-factor authentication app on your device to get an authentication code and verify your identity.</p>'),
62 '$recovery_message' => L10n::t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
63 '$verify_code' => ['verify_code', L10n::t('Please enter a code from your authentication app'), '', '', 'required', 'autofocus placeholder="000000"'],
64 '$verify_label' => L10n::t('Verify code and complete login'),