如何使用 php 创建 google 双因素身份验证?
How to create google two factor authentication using php?
我想在我的 PHP 项目中使用 Google 2FA。用户登录时需要输入6位2fa码。
你能给点建议吗?
步骤 1) 创建一个长度为 16 个字符的唯一密码。
PHPGangsta 为 Google 验证器提供包装器 class。可以使用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 维基百科
您可以使用任何 QR 码生成器生成 QR 码,对于这个演示,我使用 Google 个图表。
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 身份验证器应用程序
生成 TOTP(Time-Based 一次性密码)
从 Google Play 或 AppStore
下载 Google 身份验证器应用程序
打开应用程序并点击“+”按钮,然后扫描使用Google图表生成的二维码。 Authenticator 应用程序为您的网站生成 TOTP。 TOTP 将每 30 秒更改一次。
使用 Google 身份验证器
进行双因素身份验证
步骤 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/
您使用哪个包非常重要,因为您非常信任控制该包的人。我宁愿不使用名为 PHPGangsta 的软件,而是使用 this one.
更新
我在下面留下我原来的答案,但是奏鸣曲解决方案的问题是它查询 api.qrserver.com。他们提供了这些文档 here。他们提供这项服务非常好,但我不认识他们,所以我不能相信他们。
我转而使用 this solution,它受到 PHPGangsta 的启发,似乎也是一种改进。一般来说,这似乎是一个更值得信赖的解决方案。我是这样使用的:
首先使用composer添加:
composer require robthree/twofactorauth
然后就可以打印二维码了:
$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 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';
}
我想在我的 PHP 项目中使用 Google 2FA。用户登录时需要输入6位2fa码。
你能给点建议吗?
步骤 1) 创建一个长度为 16 个字符的唯一密码。 PHPGangsta 为 Google 验证器提供包装器 class。可以使用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 维基百科 您可以使用任何 QR 码生成器生成 QR 码,对于这个演示,我使用 Google 个图表。
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 身份验证器应用程序
生成 TOTP(Time-Based 一次性密码)从 Google Play 或 AppStore
下载 Google 身份验证器应用程序打开应用程序并点击“+”按钮,然后扫描使用Google图表生成的二维码。 Authenticator 应用程序为您的网站生成 TOTP。 TOTP 将每 30 秒更改一次。
使用 Google 身份验证器
进行双因素身份验证步骤 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/
您使用哪个包非常重要,因为您非常信任控制该包的人。我宁愿不使用名为 PHPGangsta 的软件,而是使用 this one.
更新
我在下面留下我原来的答案,但是奏鸣曲解决方案的问题是它查询 api.qrserver.com。他们提供了这些文档 here。他们提供这项服务非常好,但我不认识他们,所以我不能相信他们。
我转而使用 this solution,它受到 PHPGangsta 的启发,似乎也是一种改进。一般来说,这似乎是一个更值得信赖的解决方案。我是这样使用的:
首先使用composer添加:
composer require robthree/twofactorauth
然后就可以打印二维码了:
$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 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';
}