yossivic/otp
Composer 安装命令:
composer require yossivic/otp
包简介
A simple package for generating and validating OTP (One Time Password) for Laravel
README 文档
README
A lightweight and flexible Laravel package for generating and validating one-time passwords (OTP).
Supports storage drivers (cache and database), secure hashing, configurable expiry, and customizable OTP format.
Features
- Secure hashing (no plain OTP stored)
- Supports cache and database storage drivers
- Configurable OTP length and type (numeric / alphanumeric / hex)
- Expiry control (minutes or seconds)
- Pluggable repository pattern (swap storage without changing code)
- Returns structured validation response (
['valid' => bool, 'message' => string]) - Optional Facade:
Otp::generate()/Otp::validate() - Publishable config and (optional) migrations
Installation
Install via Composer:
composer require yossivic/otp
Publish Configuration
php artisan vendor:publish --tag=otp-config
This copies the package config to:
config/otp.php
Database Storage Setup (optional)
If you want persistent OTP storage (database) instead of cache (is default value):
- Publish migrations (if you included them)
php artisan vendor:publish --tag=otp-migrations
This should copy migration file(s) into:
database/migrations/2025_xx_xx_xxxxxx_create_otps_table.php
2.Migrate
php artisan migrate
Usage
You can use the service either by resolving from the container (dependency injection / app()) or via the facade.
The service returns OTP (string) on generation and a structured array on validation:
[
'valid' => true|false,
'message' => '...'
]
- Using Dependency Injection (recommended)
namespace App\Http\Controllers; use Illuminate\Http\Request; use Yossivic\Otp\Services\OtpService; class OtpController extends Controller { protected OtpService $otpService; public function __construct(OtpService $otpService) { $this->otpService = $otpService; } public function generate(Request $request) { $identifier = $request->input('identifier'); // e.g. user id, email, phone $otp = $this->otpService->generate($identifier); // Send OTP to user via your chosen channel (outside this package) return response()->json(['otp' => $otp]); } public function validate(Request $request) { $identifier = $request->input('identifier'); $code = $request->input('otp'); $result = $this->otpService->validate($identifier, $code); if ($result['valid']) { return response()->json(['message' => 'OTP valid']); } return response()->json(['message' => $result['message']], 422); } }
Manual resolution (anywhere in code):
$otpService = app(\Yossivic\Otp\Services\OtpService::class); $otp = $otpService->generate('user_123'); // returns generated OTP (string) $result = $otpService->validate('user_123', '123456'); // returns ['valid'=>..., 'message'=>...]
- Using the Facade (shortcut)
use Otp; $otp = Otp::generate('user_123'); $result = Otp::validate('user_123', '123456'); if ($result['valid']) { // success } else { // $result['message'] explains the failure (expired, not found, max attempts, invalid) }
Repository switching (cache vs db)
Switch storage by editing
config/otp.php:
// cache (fast, recommended)
'repository' => 'cache',
// or for persistent storage
'repository' => 'db',
Contributing
I am not perfect. please open an issue to discuss major features or you have any suggestion to improve.
yossivic/otp 适用场景与选型建议
yossivic/otp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「Authentication」 「laravel」 「otp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yossivic/otp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yossivic/otp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yossivic/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.
Provide a way to secure accesses to all routes of an symfony application.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
It's a barebone security class written on PHP
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-23