php使用goole authenticator谷歌验证码pragmarx/google2fa
php使用goole authenticator谷歌验证码pragmarx/google2fa
1.引入包
composer require pragmarx/google2fa
2.绑定谷歌验证码生成二维码
$google2fa = new Google2FA();
// 生成一个新的密钥
$google2fa_secret = $google2fa->generateSecretKey();
// 生成二维码
$companyName = 'owen';
$email = 'owen@qq.com';
$qrCodeUrl = $google2fa->getQRCodeUrl($companyName, $email, $google2fa_secret);
$google2faUrl = 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=' . $qrCodeUrl . '&ecc=M';
echo '';
3.goole authenticator绑定二维码
4.验证码验证
$google2fa = new Google2FA();
$isValid = $google2fa->verifyKey($google2fa_secret, $post['google2fa_code']);
if (!$isValid) {
return $this->json(2, '谷歌验证码错误');
}