我想在我的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/发布于 2021-11-21 09:40:27
使用哪个包是非常重要的,因为您对控制该包的人有很大的信任。我宁可不使用名为PHPGangsta的应用程序,而使用this one。
更新
我在下面留下了我最初的答案,但是sonata解决方案的问题是它查询api.qrserver.com。他们在here上提供了这些文档。他们提供这项服务非常好,但我不认识他们,所以我不能信任他们。
取而代之的是,我选择了this solution,它受到PHPGangsta的启发,看起来也是一个改进。一般来说,这似乎是一个更值得信赖的解决方案。我这样使用它:
首先使用composer添加它:
composer require robthree/twofactorauth然后,您可以打印出QR图像:
$tfa = new TwoFactorAuth(env('APP_NAME'));
$secret = $tfa->createSecret();
$qrCodeImage = $tfa->getQRCodeImageAsDataUri($user->email, $secret);
echo '<img src='.$qrCodeImage.' />';然后验证:
$tfa = new TwoFactorAuth();
$result = $tfa->verifyCode($secret, $code);
if (empty($result)) print 'Sorry!';
else print 'Yes!';下面的是我以前的回答,但我不推荐使用这个解决方案
使用composer添加它:
composer require sonata-project/google-authenticator生成新代码:
$g = new \Google\Authenticator\GoogleAuthenticator();
$salt = '7WAO342QFANY6IKBF7L7SWEUU79WL3VMT920VB5NQMW';
$secret = $username.$salt;
echo '<img src="'.$g->getURL($username, 'example.com', $secret).'" />';然后对其进行验证:
$g = new \Google\Authenticator\GoogleAuthenticator();
$salt = '7WAO342QFANY6IKBF7L7SWEUU79WL3VMT920VB5NQMW';
$secret = $username.$salt;
$check_this_code = $_POST['code'];
if ($g->checkCode($secret, $check_this_code)) {
echo 'Success!';
} else {
echo 'Invalid login';
}https://stackoverflow.com/questions/42290885
复制相似问题