2017-02-17 53 views
-1

我想在我的PHP項目中使用谷歌雙因素身份驗證。如何使用PHP創建谷歌雙因素認證?

用戶需要在登錄屏幕上輸入6位密碼。

請指出這一點,併爲我提供最佳解決方案。

+0

你有什麼迄今所做? – Aj334

+0

我仍然沒有開始。因爲我不知道這是怎麼回事? –

+0

你可能想看到這個答案http://stackoverflow.com/questions/16908124/google-two-factor-authentication-tutorial-for-codeigniter –

回答

0
Step 1) Create a unique secret code of length 16 characters. 
PHPGangsta provides wrapper class for Google Authenticator. You can download using 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; 

?> 


Step 2) Create a QR code withe the generated secret. 

We need to prepare a QR code using the secret. If you want to read more about QR code generation for Google Authenticator. Github Wiki 
You can use any QR code generator to generate the QR code, For this demo I am using Google charts. 

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
<?php 
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; 

?> 


Step 3) Generate TOTP (Time-Based One time password) using Google Authenticator App 

Download the Google Authenticator app from Google Play or AppStore 

Open the app and Click on ‘+’ Button, and scan the QR code generated using Google Charts. Authenticator app generates the TOTP for your website. TOTP will change for every 30 secs. 

Two factor authentication with Google Authenticator 

Step 4) Verifying OTP at server side 

<?php 
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'; 
} 

?> 

源代碼參考以下鏈接:http://hayageek.com/two-factor-authentication-with-google-authenticator-php/

+0

謝謝你的代碼..我會試試這個,如果有任何澄清,請回復你。 –