首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php创建google双因素认证?

如何使用php创建google双因素认证?
EN

Stack Overflow用户
提问于 2017-02-17 14:30:37
回答 2查看 17K关注 0票数 9

我想在我的PHP项目中使用Google 2FA。用户需要在登录时输入6位2fa代码。

你能给我一些关于走哪一个方向的建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-17 14:57:08

步骤1)创建长度为16个字符的唯一密码。PHPGangsta为Google Authenticator提供了包装器类。您可以使用composer进行下载。

代码语言:javascript
复制
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。

代码语言:javascript
复制
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

代码语言:javascript
复制
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/
票数 14
EN

Stack Overflow用户

发布于 2021-11-21 09:40:27

使用哪个包是非常重要的,因为您对控制该包的人有很大的信任。我宁可不使用名为PHPGangsta的应用程序,而使用this one

更新

我在下面留下了我最初的答案,但是sonata解决方案的问题是它查询api.qrserver.com。他们在here上提供了这些文档。他们提供这项服务非常好,但我不认识他们,所以我不能信任他们。

取而代之的是,我选择了this solution,它受到PHPGangsta的启发,看起来也是一个改进。一般来说,这似乎是一个更值得信赖的解决方案。我这样使用它:

首先使用composer添加它:

代码语言:javascript
复制
composer require robthree/twofactorauth

然后,您可以打印出QR图像:

代码语言:javascript
复制
$tfa = new TwoFactorAuth(env('APP_NAME'));
$secret = $tfa->createSecret();
$qrCodeImage = $tfa->getQRCodeImageAsDataUri($user->email, $secret);
echo '<img src='.$qrCodeImage.' />';

然后验证:

代码语言:javascript
复制
$tfa = new TwoFactorAuth();
$result = $tfa->verifyCode($secret, $code);
if (empty($result)) print 'Sorry!';
else print 'Yes!';

下面的是我以前的回答,但我不推荐使用这个解决方案

使用composer添加它:

代码语言:javascript
复制
composer require sonata-project/google-authenticator

生成新代码:

代码语言:javascript
复制
$g = new \Google\Authenticator\GoogleAuthenticator();
$salt = '7WAO342QFANY6IKBF7L7SWEUU79WL3VMT920VB5NQMW';
$secret = $username.$salt;
echo '<img src="'.$g->getURL($username, 'example.com', $secret).'" />';

然后对其进行验证:

代码语言:javascript
复制
$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';
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42290885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档