我想在我的PHP项目中使用Google 2FA。用户需要在登录时输入6位2fa代码。
你能给我一些关于走哪一个方向的建议吗?
发布于 2017-02-17 14:57:08
步骤1)创建长度为16个字符的唯一密码。PHPGangsta为Google Authenticator提供了包装器类。您可以使用composer进行下载。
curl -sS https://getcomposer.org/installer | php
php composer.phar require phpgangsta/googleauthenticator:dev-master
Use the below code to generate the secret code.
<?php
require 'vendor/autoload.php';
$authenticator = new PHPGangsta_GoogleAuthenticator();
$secret = $authenticator->createSecret();
echo "Secret: ".$secret;
?>
步骤2)使用生成的密钥创建二维码。
我们需要用这个秘密准备一个二维码。如果你想了解更多关于Google Authenticator二维码生成的信息。Github Wiki你可以使用任何二维码生成器来生成二维码,在这个演示中,我使用的是Google charts。
require 'vendor/autoload.php';
$authenticator = new PHPGangsta_GoogleAuthenticator();
$secret = $authenticator->createSecret();
echo "Secret: ".$secret."\n"; //save this at server side
$website = 'http://hayageek.com'; //Your Website
$title= 'Hayageek';
$qrCodeUrl = $authenticator->getQRCodeGoogleUrl($title, $secret,$website);
echo $qrCodeUrl;
步骤3)使用Google Authenticator App生成TOTP (基于时间的一次性密码)
从Google Play或AppStore下载谷歌验证器应用程序
打开该应用程序并单击“+”按钮,然后扫描使用Google Charts生成的二维码。验证器应用程序为您的网站生成TOTP。TOTP将每30秒更改一次。
使用Google Authenticator进行双因素身份验证
第4步)在服务器端验证OTP
require 'vendor/autoload.php';
$authenticator = new PHPGangsta_GoogleAuthenticator();
$secret = '3JMZE4ASZRIISJRI'; //This is used to generate QR code
$otp = '183036' ;//Generated by Authenticator.
$tolerance = 0;
//Every otp is valid for 30 sec.
// If somebody provides OTP at 29th sec, by the time it reaches the server OTP is expired.
//So we can give tolerance =1, it will check current & previous OTP.
// tolerance =2, verifies current and last two OTPS
$checkResult = $authenticator->verifyCode($secret, $otp, $tolerance);
if ($checkResult)
{
echo 'OTP is Validated Succesfully';
} else {
echo 'FAILED';
}
source code refer this link : http://hayageek.com/two-factor-authentication-with-google-authenticator-php/
https://stackoverflow.com/questions/42290885
复制相似问题