Skip to content

Instantly share code, notes, and snippets.

@binzram
Last active March 20, 2018 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binzram/e63c54e5f6b1752e4f7c75ef58da4b5c to your computer and use it in GitHub Desktop.
Save binzram/e63c54e5f6b1752e4f7c75ef58da4b5c to your computer and use it in GitHub Desktop.
PHP Google Auth 2FA (student example, bbc)

Installing

composer require pragmarx/google2fa
composer require bacon/bacon-qr-code
<?php
use PragmaRX\Google2FA\Google2FA;
$google2fa = new Google2FA();
// save userSecret in db
$userSecret = $google2fa->generateSecretKey();
// link to img src
$inlineUrl = $google2fa->getQRCodeInline(
'TestOrganisation',
'test@organisation.com',
$secret
);
<?php
use PragmaRX\Google2FA\Google2FA;
$google2fa = new Google2FA();
// saved userSecret from db
$userSecret = $user->get2faSecret();
// entered code
$validationCode = $input->getData();
// only current code is valid
$numberOfAllowedByGoneCodes = 0;
// (in)valid
var_dump(
$google2fa->verifyKey(
$userSecret,
$validationCode,
$numberOfAllowedByGoneCodes
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment