Skip to content

Instantly share code, notes, and snippets.

@cballou
Last active March 8, 2017 11:31
Show Gist options
  • Save cballou/46c6f5471a7fd51b24d879e5510f5c91 to your computer and use it in GitHub Desktop.
Save cballou/46c6f5471a7fd51b24d879e5510f5c91 to your computer and use it in GitHub Desktop.
Documentation regarding GoogleAuthenticatorRedux. Official documentation of the library can be found at https://www.craftblue.com/projects/php/google-authenticator-2fa
<?php
// if you are using composer, which is the preferred method of autoloading
require_once('./vendor/autoload.php');
// create a new secret for a user wishing to enable 2FA
// you will need to store this securely
$secret = $ga->createSecret();
// example of generating a QR code to display to the end user
// note that you need to generate a unique label for each user
// in the format of your application or vendor name (a namespace/prefix)
// followed by a colon, followed by a unique identifier for the user such
// as their login email address to your app or their name
$qrCodeUrl = $ga->getQRCodeUrl('MyVendorPrefix:userlogin@gmail.com', $secret);
echo '<img src="' . $qrCodeUrl . '" />';
// retrieve an example valid code
// (usually the user would supply this for you from the Google Authenticator app)
$code = $ga->getCode($secret);
// example of verifying that a code is valid for the secret at this given time
if ($ga->verifyCode($secret, $code, 2)) {
echo 'VERIFIED';
} else {
echo 'VERIFICATION FAILED';
}
<?php
require 'vendor/autoload.php';
$client = new CraftBlue\GoogleAuthenticator();
# install composer if you don't already have it on your machine
curl -sS https://getcomposer.org/installer | php
# from your project's base directory, run the composer command to install GoogleAuthenticator
php composer.phar require craftblue/google-authenticator-redux
# if you have phpunit globally, run this from the base project directory:
phpunit
# after updating composer, run this from the base project directory:
vendor/bin/phpunit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment