The PHPGangsta_GoogleAuthenticator class implements TOTP (RFC6238) to facilitate 2-factor authentication. It provides methods to:
- Generate a new secret.
- Generate a QR-Code URL for scanning with the Google Authenticator app.
- Generate a current code.
- Verify a provided code against a secret.
Security Note: To prevent replay attacks, ensure that used codes cannot be reused. To prevent brute-force attacks, implement rate limiting (e.g., limiting verification attempts per IP address).
<?php
require_once 'PHPGangsta/GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
$secret = $ga->createSecret();
// Generate QR Code URL
$qrCodeUrl = $ga->getQRCodeGoogleUrl('Blog', $secret);
// Generate and verify a code
$oneCode = $ga->getCode($secret);
$checkResult = $ga->verifyCode($secret, $oneCode, 2); // 2 = 2*30sec clock tolerance
if ($checkResult) {
echo 'OK';
} else {
echo 'FAILED';
}