Skip to content

yoolo-mine/GoogleAuthenticator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google Authenticator PHP Class / PHP 谷歌身份验证器类

License PHP Version

A PHP library to interact with Google Authenticator for 2FA.
本 PHP 库用于与 Google Authenticator 移动应用交互,实现双因素认证 (2FA)。

It can generate secrets, generate and verify codes, and provide QR-Code URIs. Implements TOTP according to RFC6238.
它可以生成密钥、生成和验证验证码,并提供二维码 URI。遵循 TOTP 标准 RFC6238


Features / 功能

  • Generate and verify TOTP codes / 生成和验证 TOTP 验证码
  • QR-Code URI generation / 生成二维码 URI
  • Customizable code length (default: 6) / 可自定义验证码长度(默认 6 位)
  • Compatible with Google Authenticator, Authy, 1Password, etc. / 兼容 Google Authenticator、Authy、1Password 等 2FA 应用
  • Includes PHPUnit test cases / 内置 PHPUnit 单元测试

Installation / 安装

Use Composer:
使用 Composer 安装:

composer require yoolo-mine/google-authenticator

QR Code Example / 二维码示例

You can generate a QR Code for your secret and display it in HTML using Google Chart API: 可以使用 Google Chart API 为密钥生成二维码,并在 HTML 中显示:

<?php
use YooloMine\GoogleAuthenticator\GoogleAuthenticator;

$ga = new GoogleAuthenticator();
$secret = $ga->createSecret();
$uri = $ga->getOtpAuthUri('YourApp', $secret, 'MyCompany');

// Generate QR Code URL using Google Chart API
$qrCodeUrl = 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=' . urlencode($uri);

echo "QR Code URL: " . $qrCodeUrl;

HTML preview / 在网页中显示二维码:

<img src="<?php echo $qrCodeUrl; ?>" alt="Scan QR Code to add to Google Authenticator">

This will render a QR code that can be scanned by Google Authenticator or other TOTP apps.

这将生成一个二维码,可被 Google Authenticator 或其他 TOTP 应用扫描添加。

Configuration / 配置

Set OTP code length / 设置验证码长度:

$ga->setCodeLength(8); // 8-digit code / 8 位验证码

Notes / 注意事项

Make sure that used codes cannot be reused to prevent replay attacks. 确保验证码不可重复使用,防止重放攻击。

Limit verification attempts to prevent brute-force attacks. 限制验证次数以防止暴力破解。

Compatible with standard 2FA apps (Google Authenticator, Authy, 1Password, etc.) 兼容标准 2FA 应用(Google Authenticator、Authy、1Password 等)

About

PHP class to generate and verify Google Authenticator 2-factor authentication

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%