vendredi 9 octobre 2020

How do we fix the "Secret key is too short. Must be at least 16 base32 characters" error when trying to incorporate google authenticator in Laravel?

I am trying to incorporate a Google Authenticator in my application using Laravel. It has a QR code that you could scan from a phone and then displays the code in Google Authenticator. When I try to input the 6 digit code in the Authenticate form I get a "Secret key is too short. Must be at least 16 base32 characters" error.

Error Image

PasswordSecurityController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use PragmaRX\Google2FA\Google2FA;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;
use App\PasswordSecurity;
use Imagick;

class PasswordSecurityController extends Controller
{
    public function show2faForm(){
        if(Auth::guest()){
            return;
        }
        $user=Auth::user();
        $google2FaUrl="";
        if(count($user->passwordSecurity)){
            $google2Fa= new Google2FA();
            $google2FaUrl = $google2Fa->getQRCodeUrl(
                'CFS',
                $user->email,
                $user->PasswordSecurity->google2fa_secret
            );
        }

        //dd( $user->PasswordSecurity->google2fa_secret);

        $inlineURL='http://chart.apis.google.com/chart?chs=100x100&chld=M|0&cht=qr&chl='.$google2FaUrl;

        //$qrcode_image = base64_encode($writer->writeString($google2FaUrl));

   

        $data=array(
            'user'=>$user,
            'google2FaUrl'=>$inlineURL,

        );
        
        return view('auth.google2fa')->with('data',$data);
    }

    public function generate2faSecretCode(Request $request){
        $user=Auth::user();
        $google2Fa= new Google2FA();

        PasswordSecurity::create([
            'user_id'=>$user->id,
            'google2fa_enable'=>0,
            'google2fa_secret'=> $google2Fa->generateSecretKey()
        ]);

        return redirect('/2fa')->with('Success your secret key has been generated. Please verify to enable');

    }

    public function enable2fa(Request $request){
        $user=Auth::user();
        $google2Fa=new Google2FA();
        $secret=$request->input('verifyCode');
        $valid = $google2Fa->verifyKey($user->google2fa_secret, $secret,4);
        if($valid){
            $user->passwordSecurity->google2fa_enable=1;
            $user->passwordSecurity->save();
            return redirect('/2fa')->with('success','Success 2FA is enabled');
        }
        else{
            return redirect('/2fa')->with('error','Invalid Code, Please try again');
        }

    }
}

I looked at a couple of forums but couldn't find anything. Is there any fix for this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire