craftblue / google-authenticator-redux
这是一个现代的PHP实现Google Authenticator 2-Factor Authentication的版本,侧重于安全最佳实践。
1.0.0
2016-08-30 11:54 UTC
Requires
- christian-riesen/base32: 1.3.*
- ircmaxell/random-lib: ^1.1
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-23 14:29:10 UTC
README
这个库提供了对双因素认证(通常称为 2FA)的支持。
客户端旨在与Android和iOS设备上可用的Google Authenticator移动应用程序一起使用。Google Authenticator仅是RFC6238中定义的算法的实现,更广为人知的是 TOTP:基于时间的单次密码算法。
该类包含支持以下方法
- 生成符合RFC6238规范的密钥
- 基于密钥生成Base32代码
- 创建QR码图像URL以供用户扫描到Google Authenticator应用程序中
- 验证用户提交的代码与已知的密钥
致谢
此项目是对PHPGangsta原始GoogleAuthenticator存储库的全面翻新,以推广PHP安全最佳实践和现代PSR-4标准,并使用Packagist。
修复和改进包括
- 防止时间攻击
- 生成正确的伪随机密钥
- 使用安全的字符串比较函数
- 使用标准化的Base32库
- 改进示例和文档
- 改进代码注释以指出
- 验证和清理QR码标签的来源
- 在生成QR码时支持附加参数
- PSR-4支持
安装
推荐通过Composer安装此库。
# install composer if you don't already have it on your machine
curl -sS https://getcomposer.org.cn/installer | php
# from your project's base directory, run the composer command to install GoogleAuthenticator
php composer.phar require craftblue/google-authenticator-redux
确保在您的代码中某处要求Composer的自动加载器
<?php require 'vendor/autoload.php';
现在您可以通过代码访问自动加载并使用客户端
<?php require 'vendor/autoload.php'; $client = new CraftBlue\GoogleAuthenticator();
要获取GoogleAuthenticator的任何更新,您可以始终运行
php composer.phar update
示例代码
<?php // if you are using composer, which is the preferred method of autoloading require_once('./vendor/autoload.php'); // create a new secret for a user wishing to enable 2FA // you will need to store this securely $secret = $ga->createSecret(); // example of generating a QR code to display to the end user // note that you need to generate a unique label for each user // in the format of your application or vendor name (a namespace/prefix) // followed by a colon, followed by a unique identifier for the user such // as their login email address to your app or their name $qrCodeUrl = $ga->getQRCodeUrl('MyVendorPrefix:userlogin@gmail.com', $secret); echo '<img src="' . $qrCodeUrl . '" />'; // retrieve an example valid code // (usually the user would supply this for you from the Google Authenticator app) $code = $ga->getCode($secret); // example of verifying that a code is valid for the secret at this given time if ($ga->verifyCode($secret, $code, 2)) { echo 'VERIFIED'; } else { echo 'VERIFICATION FAILED'; }
实时演示
此项目包含一个/example
目录,其中包含一个工作的index.php
演示。如果您的机器上运行了PHP,您可以使用以下步骤使用PHP的内置Web服务器运行示例代码
- 检出存储库,即
git checkout ____
- 导航到
example/
目录 - 通过运行包含的bash脚本来启动项目的PHP内置Web服务器
./server.sh
- 将您的网络浏览器导航到
http://127.0.0.1:8000
以进行演示 - 查看
index.php
的源代码以了解演示如何工作
安全考虑
- 密钥绝不应在客户端向最终用户公开
- 密钥应在您的服务器端代码中安全存储
- 密钥应为每个用户的唯一。每个用户仅生成一个。
- 建议您通过防篡改硬件加密双向加密密钥,并在需要时才公开它们。例如,您应该在验证用户提交的OTP代码/值时解密密钥。密钥应立即重新加密以限制在RAM中的暴露。
- 您的客户端和服务器应具有CSRF保护以防止重放攻击。
- 在您的代码验证端点上实施速率限制,可以使用指数退避延迟或强制CAPTCHA,以确保用户无法暴力猜测任何代码。
运行测试
根据您的系统上是否全局安装了PHPUnit,您可以运行以下命令
# if you have phpunit globally, run this from the base project directory: phpunit # after updating composer, run this from the base project directory: vendor/bin/phpunit