谷歌身份令牌介绍
Google Authenticator
谷歌动态口令
Google Authenticator,谷歌动态口令,Google身份验证器Google Authenticator是谷歌推出的基于时间的一次性密码(Time-based One-time Password,简称TOTP),只需要在手机上安装该APP,就可以生成一个随着时间变化的一次性密码,用于帐户验证。
此接口适用介绍
目前已成功对接
thinkphp框架
Laravel框架
其他框架请自测
接口源码
数据表结构
名字 | 类型 | 长度/值 | 默认 | 排序规则 | 属性 | 空 | A_I | 注释 | 移动字段 |
d_googleid | varchar | 200 | NULL | utf8_unicode_ci | 打钩 | 谷歌二维码 | |||
d_googlekey | varchar | 200 | NULL | utf8_unicode_ci | 打钩 | 谷歌key |
谷歌对接PHP代码
PHP源码顶部第三行
增加代码
use Common\Ext\GoogleAuthenticator;
参考图
登录接口php对接代码
$admin = M('Admin')->where(array('username' => $username))->find();
if($admin['d_googlekey'])
{
// 验证谷歌验证码
$googleCode = $_POST['google_verify'];
$authenticator = new GoogleAuthenticator();
if (!$authenticator->verifyCode($admin['d_googlekey'], $googleCode)) {
$this->error('谷歌验证码错误');
}
}
参考图
GoogleAuthenticator.class.php文件代码
<?php
namespace app\admin\service;
use googleAuth\GoogleAuthenticator;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Request;
/**
* 谷歌令牌
* Class Users
* @package app\admin\controller
*/
class GoogleService
{
/**
* 指定当前数据表
* @var string
*/
//public $table = 'system_user';
public $table = 'SystemUser';
public static function instance(): self
{
return new self();
}
/**
* 检测是否绑定令牌
* @param int $uid 账号ID
* @return boolean
*/
public function isBind(int $uid): bool
{
$google_is_bind = Db::name($this->table)->where(['id' => $uid])->value('google_is_bind');
return $google_is_bind === 1;
}
/**
* 绑定谷歌验证
* @param int $uid 账号ID
* @return boolean|array
* @throws Exception
* @throws DataNotFoundException
* @throws ModelNotFoundException
* @throws DbException
* @throws PDOException
*/
public function getBindUrl(int $uid)
{
$googleAuth = new GoogleAuthenticator();
$secret = Db::name($this->table)->where(['id' => $uid])
->field('google_secret,google_url,username')->find();
$re = [];
$uname = $secret['username'];
if (!$secret['google_secret']) {
$secret = $googleAuth->createSecret(); //谷歌密钥
if ($secret) {
$qrCodeUrl = $googleAuth->getQRCodeGoogleUrl('HZW@' . \request()->rootDomain() . '@' . $uname, $secret); //谷歌二维码
//$oneCode = $googleAuth->getCode($secret);
Db::name($this->table)->where(['id' => $uid])->update([
'google_secret' => $secret,
'google_url' => $qrCodeUrl,
'google_is_bind' => 0
]);
$re['google_secret'] = $secret;
$re['google_url'] = $qrCodeUrl;
} else {
return false;
}
} else {
$re['google_secret'] = $secret['google_secret'];
$re['google_url'] = $secret['google_url'];
}
return $re;
}
/**
* 设置为已绑定
* @param int $uid 账号ID
*/
public function setBind(int $uid)
{
return Db::name($this->table)->where(['id' => $uid])->update([
'google_is_bind' => 1
]);
}
/**
* 谷歌验证
* @param int $uid 账号ID
* @param string $code 谷歌验证码
* @return boolean
*/
public function checkCode(int $uid, string $code): bool
{
$googleAuth = new GoogleAuthenticator();
$secret = Db::name($this->table)->where(['id' => $uid])->value('google_secret');
// 2 = 2*30sec clock tolerance
return $googleAuth->verifyCode($secret, $code, 2);
}
}
目录Common\Ext\文件GoogleAuthenticator.class.php
参考图
GoogleAuthenticator-master此源码加在网站源码里面
放置参考图
谷歌验证令牌接口源码