coderberg / google-authenticator
Google Authenticator 二步验证
v1.2.1
2023-05-22 14:24 UTC
Requires
- php: >=8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.16
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
- rector/rector: ^0.16.0
README
- 版权所有 (c) 2012-2016,https://www.phpgangsta.de
- 作者:Michael Kliewe,@PHPGangsta 和 贡献者
- 基于BSD许可证。
这个PHP类可以用来与Google Authenticator手机应用进行交互,用于二步验证。这个类可以生成密钥,生成验证码,验证验证码,并显示一个用于扫描密钥的二维码。它根据RFC6238实现了TOTP。
为了安全安装,您需要确保使用的验证码不能被重用(重放攻击)。您还需要限制验证次数,以抵御暴力攻击。例如,您可以将一个IP地址(或IPv6块)的验证次数限制为10分钟内的10次尝试。这取决于您的环境。
支持的PHP版本
- PHP 8.0
- PHP 8.1
- PHP 8.2
安装
composer req coderberg/google-authenticator
使用方法
<?php use Coderberg\GoogleAuthenticator; $ga = new 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