<?php
require_once "./vendor/autoload.php";
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
class Authenticator
{
protected $googleClient;
protected $secret;
public function __construct()
{
$this->googleClient = new PHPGangsta_GoogleAuthenticator();
}
public function createSecret()
{
$secret = $this->googleClient->createSecret();
return $secret;
}
public function createQr($url)
{
$secret = $this->createSecret();
$this->InsertSecret(1, $secret);
$qr = $this->googleClient->getQRCodeGoogleUrl($url, $secret);
echo "秘钥:$secret" . PHP_EOL;
return "<img src='$qr'/>";
}
public function InsertSecret($id, $secret)
{
//将服务器生成的secret入库与用户绑定
}
public function getSecret($id)
{
//将对应用户的secret提取出来并返回
return 'GBYTMLVRNQKAKJVD';
}
public function verify($id, $verify)
{
$checkResult = $this->googleClient->verifyCode($this->getSecret(1), (string)$verify, 0);
if ($checkResult) {
return true;
} else {
return false;
}
}
}
$client = new Authenticator();
echo $client->createQr("www.zhangshuxian.top");
echo $client->verify(1, 436270) ? "通过" : "未通过";