habil/googleauthenticator

来自 phpgangsta/googleauthenticator 仓库的 Google Authenticator 双因素认证

v1.0.3 2018-10-25 09:38 UTC

This package is auto-updated.

Last update: 2024-09-25 23:09:34 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

待办事项

  • ??? 您需要什么?

注意事项

如果您喜欢此脚本或想添加一些功能:联系我,访问我的博客,分支此项目,发送拉取请求,您知道如何操作。