承接 ars/laravel-otp-code 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ars/laravel-otp-code

Composer 安装命令:

composer require ars/laravel-otp-code

包简介

A Laravel package for generating, validating, and managing One-Time Passwords (OTPs) to enhance security in authentication workflows.

README 文档

README

Introduction

This Laravel package offers a comprehensive solution for generating, storing, and verifying One-Time Passwords (OTPs). It supports customizable OTP configurations, including type, length, expiration, encryption, and validation rules, ensuring secure and flexible OTP management in your Laravel applications.

Features

  • Generate secure OTP codes with customizable types and lengths.
  • Store and manage OTP codes with configurable expiration times.
  • Optional OTP encryption for enhanced security.
  • Validation rules to ensure OTP integrity.
  • Seamless integration with Laravel applications.

Requirements:

  • Laravel Version: 10 or higher
  • PHP Version: 8.0 or higher

Installation

To install the package, follow these steps:

  1. Require the package via Composer:

    composer require ars/laravel-otp-code
  2. Publish the configuration file and migration:

    php artisan vendor:publish --provider="Ars\Otp\Providers\OtpCodeServiceProvider"
  3. Run the migration:

    php artisan migrate

Configuration

The configuration file otp-code.php includes the following options:

  • table_name: The name of the database table to store OTP codes.
  • code_type: The data type of the OTP code (int for integers or string for alphanumeric).
  • code_length: The length of the OTP code.
  • max_attempts: The maximum number of attempts allowed to validate the OTP (set 0 to disable attempts).
  • expiry_time: The time (in minutes) before the OTP expires.
  • encrypt_code: Whether to encrypt the OTP code before storing it.
  • default_salt: The default salt value to be used if none is provided.

Usage

Repository

Creating an OTP

To create an OTP, use the create method:

$otpRepository = new \Ars\Otp\Repositories\OtpRepository();
$otp = $otpRepository->create('alireza2000sajedi@gmail.com');
$code = $otp->code;

Verifying an OTP

To verify an OTP, use the verify method:

$isValid = $otpRepository->verify('alireza2000sajedi@gmail.com', 1234);

Checking OTP Existence

To check if a valid OTP exists, use the has method:

$exists = $otpRepository->has('alireza2000sajedi@gmail.com');

Facade

The package provides a facade for easy access to the OTP repository:

use Ars\Otp\Facades\OtpCode;

// Create OTP
$otp = OtpCode::create('alireza2000sajedi@gmail.com');
$code = $otp->code;
// Verify OTP
$isValid = OtpCode::verify('alireza2000sajedi@gmail.com', 1234);

Custom Validation Rule

The package includes a validation rule to validate OTP codes:

use Ars\Otp\Rules\OtpCode;

$request->validate([
    'email' => 'alireza2000sajedi@gmail.com',
    'otp_code' => ['required', new OtpCode($request->get('email'))],
]);

Example

use Ars\Otp\Facades\OtpCode;

    //If has salt send parameter after identifier
    public function sendOtp($email, $salt = null)
    {
        if (OtpCode::has($email, $salt)) {
            return Responder::setErrorCode(201)->setMessage(trans('otp_code::otp-code.already_send'))->respond();
        }

        $otp = OtpCode::create($email, $salt);

        Notification::send($email, new OtpNotification($otp->code));
    }

Customization

Error Messages

You can customize the error messages by modifying the language files. Publish the language files using:

php artisan vendor:publish --tag=lang --provider="Ars\Otp\Providers\OtpCodeServiceProvider"

Commands

The package includes an Artisan command to purge expired OTP codes:

php artisan otp:clear-expired

Conclusion

This package provides a robust solution for managing OTP codes in a Laravel application. By leveraging the repository, facade, and validation rules, you can easily integrate OTP functionality into your project.

Contributing

Contributions are welcome! Please adhere to the following guidelines:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/my-new-feature).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/my-new-feature).
  5. Open a Pull Request.

License

This library is licensed under the MIT License.

Contact

For support or questions, please open an issue on GitHub.

ars/laravel-otp-code 适用场景与选型建议

ars/laravel-otp-code 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 55 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 ars/laravel-otp-code 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 55
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-18