定制 kayon-ariel/totp-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kayon-ariel/totp-php

Composer 安装命令:

composer require kayon-ariel/totp-php

包简介

Google Authenticator 2-factor authentication

README 文档

README

PHP Version License Packagist Downloads

Introduction

TOTP-PHP is a PHP library for generating Time-based One-Time Passwords (TOTP) for two-factor authentication. This library is easy to integrate into your existing PHP applications, allowing you to enhance your security measures effectively.

TOTP-PHP is compatible with Google Authenticator and other TOTP applications, making it a great choice for implementing two-factor authentication in your projects.

For a secure installation you have to make sure that used codes cannot be reused (replay-attack). You also need to limit the number of verifications, to fight against brute-force attacks. For example you could limit the amount of verifications to 10 tries within 10 minutes for one IP address (or IPv6 block). It depends on your environment.

Features

  • Generate TOTP codes compliant with RFC 6238.
  • Simple for use.
  • Built-in validation methods.
  • Secret key generation for TOTP.
  • Compatible with Google Authenticator and similar apps.
  • Generate QR code payloads for easy integration with TOTP applications.

Installation

You can install the totp-php library via Composer. Run the following command in your terminal:

composer require kayon-ariel/totp-php

Usage

Here is a simple example of how to use the library:

Generating a TOTP Code

require 'vendor/autoload.php';

use TotpPhp\Totp;

// Create a new TOTP instance
$ga = new Totp();

// Generate a secret key
$secret = $ga->createSecret();
echo "Secret is: " . $secret . "\n\n";

// Generate a TOTP code based on the secret
$oneCode = $ga->getCode($secret);
echo "Checking Code '$oneCode' and Secret '$secret':\n";

// Verify the TOTP code against the secret
$checkResult = $ga->verifyCode($secret, $oneCode);

if ($checkResult) {
    echo 'OK';
} else {
    echo 'FAILED';
}

Generating a Secret Key

You can generate a random secret key using the createSecret method. This is useful for initializing a new user or session.

$secret = $ga->createSecret();
echo "Your secret key is: " . $secret;

Generating a QR Code Payload

To generate a QR code payload for a TOTP secret, use the getQrCodePayload function:

$label = 'user@example.com'; // The user's email or username
$issuer = 'MyApp'; // Optional issuer name

$qrcodePayload = $totp->getQrCodePayload($secret, $label, $issuer);
echo "QR Code Payload: " . $qrcodePayload . "\n";

Validating a TOTP Code

You can validate a TOTP code using the verifyCode method:

$userInputCode = '123456'; // Example user input
$isValid = $ga->verifyCode($secret, $userInputCode);

if ($isValid) {
    echo "The TOTP code is valid!";
} else {
    echo "The TOTP code is invalid!";
}

Code Generation and Verification Logic

The library uses the following methods:

  • createSecret(int $secretLength = 16): string: Generates a new secret key with a specified length (minimum 16 characters).
  • getCode(string $secret, ?int $timeSlice = null): string: Calculates the TOTP code for a given secret key and time slice (defaults to the current time).
  • verifyCode(string $secret, string $code, int $discrepancy = 1): bool: Checks if the provided TOTP code matches the expected code for the secret, allowing for some time discrepancy.

License

This library incorporates code from the original PHPGangsta/GoogleAuthenticator project:

Current: Copyright (c) 2024 Kayon Ariel, provided under the MIT License.

Contributions

Contributions are welcome! Feel free to submit issues, fork the repository, and submit pull requests.

Contact

For any inquiries or feedback, you can reach out to me at [kayonariel@gmail.com].

kayon-ariel/totp-php 适用场景与选型建议

kayon-ariel/totp-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 268 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「otp」 「totp」 「googleauthenticator」 「rfc6238」 「2-Factor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kayon-ariel/totp-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kayon-ariel/totp-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 kayon-ariel/totp-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 268
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 724
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-13