phpgangsta/googleauthenticator

Google Authenticator 二步验证

dev-master 2019-03-20 00:55 UTC

This package is not auto-updated.

Last update: 2024-09-14 05:52:03 UTC


README

Build Status

此PHP类可以用于与Google Authenticator移动应用进行交互以实现二步验证。此类可以生成密钥、生成代码、验证代码以及展示用于扫描密钥的二维码。它根据RFC6238实现了TOTP。

为了安全安装,您需要确保使用的代码不会被重用(重放攻击)。您还需要限制验证次数,以抵御暴力攻击。例如,您可以将一个IP地址(或IPv6块)在10分钟内的验证次数限制为10次。这取决于您的环境。

用法

请参阅以下示例

<?php
require_once 'PHPGangsta/GoogleAuthenticator.php';

$ga = new PHPGangsta_GoogleAuthenticator();
$secret = $ga->createSecret();
echo "Secret is: ".$secret."\n\n";

$qrCodeUrl = $ga->getQRCodeGoogleUrl('Blog', $secret);
echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."\n\n";

$oneCode = $ga->getCode($secret);
echo "Checking Code '$oneCode' and Secret '$secret':\n";

$checkResult = $ga->verifyCode($secret, $oneCode, 2);    // 2 = 2*30sec clock tolerance
if ($checkResult) {
    echo 'OK';
} else {
    echo 'FAILED';
}

运行脚本将提供以下输出

Secret is: OQB6ZZGYHCPSX4AK

Google Charts URL for the QR-Code: https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/infoATphpgangsta.de%3Fsecret%3DOQB6ZZGYHCPSX4AK

Checking Code '848634' and Secret 'OQB6ZZGYHCPSX4AK':
OK

安装

  • 使用Composer安装此包

  • 从项目根目录执行以下命令

composer install

  • Composer将处理库的自动加载。只需在文件顶部包含以下内容

    require_once __DIR__ . '/../vendor/autoload.php';

运行测试

  • 所有测试都在tests文件夹内。
  • 在项目根目录中执行composer install,然后从项目根目录运行测试
  • 从项目根目录运行phpunit tests

待办事项

  • ??? 你需要什么?

备注

如果你喜欢这个脚本或者想添加一些功能:请联系我,访问我的博客,fork这个项目,发送pull requests,你知道如何做。