Laravel uses laravel Google authenticator to develop a dynamic mobile token for your website through Google authenticator

Google authenticator, in combination with two-step authentication, can provide you with a second level of security when you log in to your Google account. After enabling two-step authentication, you need to provide a password and the authentication code generated by this app when you log in to your account. After configuration, the verification code can be obtained without network connection or cellular connection. Reference link [expansion package: laravel Google authenticator] create a dynamic mobile token for your website through Google authenticator

Why use two-step authentication

  1. Compared with the verification code, it is much safer; there is almost no way to crack it
  2. Sometimes the verification code can not be recognized and is not easy to operate
  3. One machine, one code, no account theft
  4. Dynamic verification, one verification code every 30 seconds, more security

Preparation before development

  1. Install Laravel
  2. Install QR code generator. It is OK if it is not installed. Next, install

Installation expansion

1. Run the following code to install the expansion pack:

composer require "earnp/laravel-google-authenticator:dev-master"
### Install QR code generator
composer require simplesoftwareio/simple-qrcode 1.3.*

2. Wait for the download and installation to complete. It needs to be done in config/app.php At the same time, the service provider registered in the

'providers' => [
    //........
    Earnp\GoogleAuthenticator\GoogleAuthenticatorServiceprovider::class,
    SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
],

'aliases' => [
     //..........
    'Google' => Earnp\GoogleAuthenticator\Facades\GoogleAuthenticator::class,
    'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
],

3. After service injection, if you want to use the custom configuration, you can also publish the configuration file to the config/views Directory:

php artisan vendor:publish

use

The use method is very simple, mainly for generating verification code and teaching and research verification code

1. Production verification code

Use CreateSecret to generate the production verification code. You need to generate a QR code for mobile APP to scan. The specific content is shown in google.blade.php Successfully configured in

// Create Google verification code
$createSecret = Google::CreateSecret();
// Your custom parameters, return with the form
$parameter = [["name"=>"usename","value"=>"123"]];
return view('login.google.google', ['createSecret' => $createSecret,"parameter" => $parameter]);

2. Verification code

The verification code is generally used for binding. In login authentication, you can use the CheckCode method. You need to pass in secrect and onecode to verify. The first one is secrect and returns true or false

if(Google::CheckCode($google,$request->onecode)) {
    // Binding scenario: successfully bind, insert google parameters into the database, jump to the login interface and let the user log in
    // Login authentication scenario: authentication succeeded, execute authentication operation
    dd("Authentication successful");
}
else
{
    // Binding scenario: authentication failed, return to rebind, refresh new QR code
    return back()->with('msg','Please input the phone correctly google Verification Code!')->withInput();
    // Login authentication scenario: authentication failed, return to rebind, refresh new QR code
    return back()->with('msg','The verification code is wrong. Please input the correct verification code!')->withInput();
}

Here is a specific practical example:

use Google;


if ($request->isMethod('post')) {
    if (empty($request->onecode) && strlen($request->onecode) != 6) return back()->with('msg','Please input the phone correctly google Verification Code!')->withInput();
    // google key is the generated key when binding. If you log in after binding, get the previously bound key from the database
    $google = $request->google;
    // Verify that the verification code and key are the same
    if(Google::CheckCode($google,$request->onecode)) {
        // Binding scenario: successfully bind, insert google parameters into the database, jump to the login interface and let the user log in
        // Login authentication scenario: authentication succeeded, execute authentication operation
        dd("Authentication successful");
    }
    else
    {
        // Binding scenario: authentication failed, return to rebind, refresh new QR code
        return back()->with('msg','Please input the phone correctly google Verification Code!')->withInput();
        // Login authentication scenario: authentication failed, return to rebind, refresh new QR code
        return back()->with('msg','The verification code is wrong. Please input the correct verification code!')->withInput();
    }
}
else
{
    // Create Google verification code
    $createSecret = Google::CreateSecret();
    // Your custom parameters, return with the form
    $parameter = [["name"=>"usename","value"=>"123"]];
    return view('login.google.google', ['createSecret' => $createSecret,"parameter" => $parameter]);
}

 

Tags: Google QRCode Laravel PHP

Posted on Fri, 12 Jun 2020 05:25:07 -0400 by gabeanderson