mkd/laravel-otp
Composer 安装命令:
composer require mkd/laravel-otp
包简介
Laravel package for generating and verifying One-Time Passwords (OTPs) using TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) algorithms.
关键字:
README 文档
README
A simple Laravel package for generating and verifying One-Time Passwords (OTPs) using TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) algorithms.
Installation
You can install the package via Composer:
composer require mkd/laravel-otp
Example
Scan This QR Code using Authenticator app
Here's a simple example demonstrating how to generate and verify an OTP:
use MKD\LaravelOTP\LaravelOTP; $secret = 'WSRNGQX4J57FL2POVHDAMBI6ZTK3CYUE'; // Base32 encoded secret $otpService = LaravelOTP::make($secret); // Generate current OTP $currentOTP = $otpService->now(); echo "Current OTP: $currentOTP\n"; // Verify the OTP $isValid = $otpService->verifyTOTP($currentOTP); echo $isValid ? "OTP is valid!" : "OTP is invalid!";
Usage
Creating an Instance
You can create an instance of the LaravelOTP class by providing a secret key:
use MKD\LaravelOTP\LaravelOTP; $otpService = LaravelOTP::make('your-secret-key');
Public Methods
now(): string
Returns the current TOTP for the current timeframe.
$otp = $otpService->now();
last(): string
Returns the TOTP for the previous timeframe (30 seconds earlier).
$otp = $otpService->last();
next(): string
Returns the TOTP for the next timeframe (30 seconds later).
$otp = $otpService->next();
at(int $offset = 0): string
Returns the TOTP for a custom timeframe based on the provided offset. An offset of 0 returns the current TOTP, -1 returns the last OTP, and 1 returns the next OTP.
$otp = $otpService->at(-1); // Last TOTP $otp = $otpService->at(1); // Next TOTP
generateSecretKey(): string
Generates a new secret key.
$secretKey = $otpService->generateSecretKey();
atCounter(int $counter): string
Returns the HOTP for a specific counter value.
$otp = $otpService->atCounter(1); // OTP for counter 1
verifyTOTP(string $otp, string|null $secret = null): bool
Verifies a given TOTP against the current, previous, and next timeframes. If a secret key is provided, it overrides the current secret.
$isValid = $otpService->verifyTOTP('123456'); // Validate TOTP
verifyHOTP(string $otp, int $counter, string|null $secret = null): bool
Verifies a given HOTP against a specific counter. If a secret key is provided, it overrides the current secret.
$isValid = $otpService->verifyHOTP('123456', 1); // Validate HOTP for counter 1
Google Authenticator Compatible
generateUrl(string $label, string $issuer, string $secretKey = null, int $counter = null): string
Generates an OTP Auth URL for use in generating a QR code. This URL can be used by OTP apps like Google Authenticator.
- Parameters:
string $label: A unique identifier for the OTP (usually the user's email or username).string $issuer: The name of your application (used as the issuer for the OTP).string|null $secretKey: An optional secret key. If not provided, the instance's secret key will be used.int|null $counter: An optional counter for HOTP. If provided, the method will generate an HOTP URL instead of TOTP.
Usage Example:
$label = 'user@example.com'; $issuer = 'YourAppName'; $otpUrl = $otpService->generateUrl($label, $issuer); echo "OTP Auth URL: $otpUrl" // otpauth://totp/Name?secret=WSRNGQX4J57FL2POVHDAMBI6ZTK3CYUE&issuer=APP;
License
This package is licensed under the MIT License. See the LICENSE file for more information.
mkd/laravel-otp 适用场景与选型建议
mkd/laravel-otp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 18, 最近一次更新时间为 2024 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Authentication」 「laravel」 「otp」 「one-time password」 「one-time-password」 「2fa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mkd/laravel-otp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mkd/laravel-otp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mkd/laravel-otp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
Email Toolkit Plugin for CakePHP
OTP generating package
A list of dangerous usernames
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-14