谷歌令牌对接文档教程

本文档详细介绍了如何使用谷歌身份令牌(Google Authenticator)进行接口对接,包括对接的适用场景,提供了对接PHP代码示例,适用于thinkphp和Laravel框架,并给出了数据表结构和修改指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

谷歌身份令牌介绍

Google Authenticator

谷歌动态口令

Google Authenticator,谷歌动态口令,Google身份验证器Google Authenticator是谷歌推出的基于时间的一次性密码(Time-based One-time Password,简称TOTP),只需要在手机上安装该APP,就可以生成一个随着时间变化的一次性密码,用于帐户验证。

此接口适用介绍

目前已成功对接

thinkphp框架

Laravel框架

其他框架请自测

接口源码

https://t.zsxq.com/13IuoKyBw

数据表结构

名字

类型 

长度/值 

默认 

排序规则

属性

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此源码加在网站源码里面

放置参考图

谷歌验证令牌接口源码

修改参考图

修改完毕可以测试看看是否正常使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尊缘阁-林尊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值