xsuchy09 / googleauthenticator
Google Authenticator 二步验证,fork自 phpgangsta/googleauthenticator。
Requires
- php: >=7.2
Requires (Dev)
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-08-29 05:00:48 UTC
README
- 版权 (c) 2019, https://www.wamos.cz
- 作者: Petr Suchy, suchy@wamos.cz
- 许可协议:BSD License。
源自
phpgangsta/googleauthenticator
原始版权信息
- 版权 (c) 2012-2016, http://www.phpgangsta.de
- 作者: Michael Kliewe, @PHPGangsta 和 贡献者
- 许可协议:BSD License。
描述
这个PHP类可以用于与Google Authenticator移动应用进行交互以进行二步验证。该类可以生成密钥、生成代码、验证代码,并提供一个用于扫描密钥的二维码。它根据 RFC6238 实现TOTP。
为了安全安装,您必须确保使用的代码不能被重复使用(重放攻击)。您还需要限制验证次数,以抵御暴力攻击。例如,您可以将一个IP地址(或IPv6块)在10分钟内的验证次数限制为10次。这取决于您的环境。
用法
请参阅以下示例
<?php require_once 'GoogleAuthenticator/GoogleAuthenticator.php'; $ga = new GoogleAuthenticator(); $name = 'suchy'; $secret = $ga->createSecret(); $title = 'WAMOS.cz'; echo sprintf('Name is: %s', $name) . PHP_EOL; echo sprintf('Secret is: %s', $secret) . PHP_EOL; echo sprintf('Title is: %s', $title) . PHP_EOL . PHP_EOL; $dataToRender = $ga->getOtpAuthLink($name, $secret, $title); // or getDataToRender method - just alis echo sprintf('Data to render: %s', $dataToRender) . PHP_EOL . PHP_EOL; // don't use this, don't share you security with third parties $qrCodeUrl = $ga->getQRCodeGoogleUrl($name, $secret, $title); echo sprintf('Google Charts URL for the QR-Code: %s', $qrCodeUrl) . PHP_EOL . PHP_EOL; $oneCode = $ga->getCode($secret); echo sprintf('Checking Code %s and Secret %s:', $oneCode, $secret) . PHP_EOL; $checkResult = $ga->verifyCode($secret, $oneCode, 2); // 2 = 2*30sec clock tolerance if (true === $checkResult) { echo 'OK'; } else { echo 'FAILED'; }
运行脚本将提供以下输出
Name is: suchy
Secret is: SECRET
Title is: WAMOS.cz
Data to render: otpauth://totp/suchy?secret=SECRET&issuer=WAMOS.cz
Google Charts URL for the QR-Code: https://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=otpauth%3A%2F%2Ftotp%2Fsuchy%3Fsecret%3DSECRET%26issuer%3DWAMOS.cz&chld=M|0
Checking Code '123456' and Secret 'SECRET':
OK
安装
- 使用 Composer 安装此包
composer require xsuchy09/googleauthenticator
-
Composer 将负责自动加载库。只需在文件顶部包含以下内容即可
require_once __DIR__ . '/../vendor/autoload.php';
运行测试
- 所有测试都在
src/tests
文件夹中。 - 执行
composer install
,然后从项目根目录运行测试。 - 已准备Shell脚本 - 只需从项目根目录运行
phpunit.sh
。 - 它还会在
.phpunit
目录内生成代码覆盖率报告。
安全建议
不要使用方法 GoogleAuthenticator::getQRCodeGoogleUrl
和 GoogleAuthenticator::getQRCodeQRServerUrl
。这只是示例。不要与第三方共享您的密钥。使用您自己的二维码生成。您可以使用如下的库
但也不必过于相信第三方库。对第三方库进行安全审计,并创建自己的分支,或者在不检查更新安全性的情况下不要更新这些库。
待办事项
- 无 ... 如果您需要某些功能,联系我。
注意事项
如果您喜欢此脚本或想添加一些功能:联系我,访问我的网页,fork此项目,发送pull请求,您知道如何操作。