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

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

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

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

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

EN

回答 1

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提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42290885

复制
相关文章

相似问题

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