How to Include Google Authenticator in our admin modules

It is sometimes necessary to include Authenticators to restrict some access to our admin panels or website

Google Authenticator: an app that adds an extra layer, securing your accounts with time-based one-time passcodes. In this discussion we will see how one can include Google Authenticator

Step1: Download Google Authenticator library from the link https://github.com/PHPGangsta/GoogleAuthenticator

Step2: Include this library folder in your controller/ core file as

require_once '<path to your library folder>';
$ga = new PHPGangsta_GoogleAuthenticator();

Step3: Generate a secret key

$secret = $ga->createSecret();  

Step4: Store this secret key and go to Google Authenticator APP and create a new authentication using this key

Step5: Create a html form from where we can enter this code

<html>
<body>
    <form  method="post">
        Code: <input type="number" placeholder="Enter Code" name="code"><br>
        <input type="submit" name="submit">
    </form>
</body>
</html>

Step6: Now upon submission of this form we need to verify the code entered

$checkResult = $ga->verifyCode($secret, $_POST["code"], 2);    // 2 = 2*30sec clock tolerance

Upon successful verification this $checkResult will return 1 else 0

Step7: Upon success or failure of code verification adjust your redirects accordingly

if ($checkResult) {
    header("Location: ");
} else {
    echo '<script>alert("Invalid Code !")</script>';
}

Alternatively to avoid entering key in Google Authenticator APP to generetae TOTPs we can use getQRCodeGoogleUrl function to generate a QR Code Now directly scan this QR Code and it will automatically start generating TOTPs

By following these steps we can successfully add one more security feature.

Tagged:
Sign In or Register to comment.