pragmarx/google2fa-laravel verifyKey always returns false

1.4k Views Asked by At

PHP v7.4, Laravel v7.28.3 I use pragmarx/google2fa-laravel

QR code generating:

$google2fa = app('pragmarx.google2fa');
$google2fa->setAlgorithm(Constants::SHA512);
$secret = $google2fa->generateSecretKey(64);
$user->google2fa_secret = $secret;
$user->save();

$QR_Image = $google2fa->getQRCodeInline(
    config('app.name'),
    $user->email,
    $user->google2fa_secret
);

Then I render the QR on frontend. Qr code looks perfectly, I scan it by Google 2fa application and code generator successfully adds to app.

When i try to verify codes from app it always returns false. Verification code:

 $code = 'I paste here the code from the application manually';
 $google2fa = app('pragmarx.google2fa');
 var_dump($google2fa->verifyKey($user->google2fa_secret, $code, 10));

BUT when I add code generator to mobile app manually using $user->google2fa_secret (i can check it in database) it works perfectly, all codes from this generator pass validation. Seems like a problem is in generated QR image...

Update after 2 years. I see that many people have the same problem, but unfortunately I do not remember how I solved it. I can provide an example of my implementation that works:

QR code generating:

$google2fa              = app('pragmarx.google2fa');
$secret                 = $google2fa->generateSecretKey();
$user->google2fa_secret = $secret;

$qrCode = $google2fa->getQRCodeInline(
    config('app.name'),
    $user->email,
    $user->google2fa_secret
);

Verification:

$google2fa->verifyKey($user->google2fa_secret, {{code provided by user from application }}); //returns bool

google2fa.php config:

'qrcode_image_backend' => \PragmaRX\Google2FALaravel\Support\Constants::QRCODE_IMAGE_BACKEND_SVG,
0

There are 0 best solutions below