alexgeno/phone-verification

一个可扩展和可配置的PHP手机验证库

v1.0.6 2023-09-12 18:35 UTC

This package is auto-updated.

Last update: 2024-09-20 18:03:20 UTC


README

Build Status Build Status Build Status Coverage Status

在现代网站或移动应用程序上注册或登录通常遵循以下步骤

  • 用户通过提交电话号码来启动验证
  • 用户收到带有一次性密码(OTP)的短信或电话
  • 用户通过提交OTP来完成验证

这个可扩展和可配置的库允许用几行代码来设置

需求

安装

composer require alexgeno/phone-verification

注意:所有支持的存储客户端和发送者SDK都在 require-dev 部分。在生产环境中,您必须手动安装您所使用的组件。

基本用法

实例化

使用 Predis 作为 存储 Twilio 作为 发送者 进行演示

use AlexGeno\PhoneVerification\Storage\Redis;
use AlexGeno\PhoneVerification\Sender\Twilio;
use AlexGeno\PhoneVerification\Manager;

$storage = new Redis(new \Predis\Client('tcp://127.0.0.1:6379'));
$sender = new Twilio(new \Twilio\Rest\Client('ACXXXXXX', 'YYYYYY'), ['from' => '+442077206312']);
$manager = new Manager($storage);

验证过程有两个阶段

启动 - 此阶段需要 存储发送者。用户提交电话号码并接收 otp

$manager->sender($sender)->initiate('+15417543010');

完成 - 此阶段只需要 存储。用户提交 otp 以验证电话

$manager->complete('+15417543010', 1234);

基本就是这样。更高级的用法包括 otp长度定制速率限制器消息定制,您可以从以下部分了解。

演示

启动

php example/initiate.php --storage redis --sender messageBird --to +15417543010

完成

php example/complete.php --storage redis --to +15417543010 --otp 1111

注意:有关如何设置开发环境的选项,请参阅 DEVELOPMENT.md

扩展

要添加新的 发送者,只需创建一个新的类

namespace AlexGeno\PhoneVerification\Sender;

class Plivo implements I
{ 
    //...
}

要添加新的 存储,只需创建一个新的类

namespace AlexGeno\PhoneVerification\Storage;

class DynamoDb implements I
{ 
    //...
}

高级用法

可以自定义速率限制参数和OTP参数

启动

use AlexGeno\PhoneVerification\Storage\Redis;
use AlexGeno\PhoneVerification\Sender\Twilio;
use AlexGeno\PhoneVerification\Manager;
use AlexGeno\PhoneVerification\Exception\RateLimit;

$config = [
    'rate_limits' => [
        'initiate' => [
            'period_secs' => 86400,
            'count' => 10,
            'message' =>
                fn($phone, $periodSecs, $count) =>
                    sprintf('You can send only %d sms in %d hours.', $count, $periodSecs / 60 / 60)
        ]
    ],
    'otp' => [
        'length' => 4, // 1000..9999
        'message' =>  fn($otp) => sprintf('Your code is %d', $otp) // The text a user receives
    ]
];

$storage = new Redis(new \Predis\Client('tcp://127.0.0.1:6379'));
$sender = new Twilio(new \Twilio\Rest\Client('ACXXXXXX', 'YYYYYY'), ['from' => '+442077206312']);

try {
    (new Manager($storage, $config))->sender($sender)->initiate('+15417543010');
} catch (RateLimit $e) {
    echo $e->getMessage(); // 'You can send only 10 sms in 24 hours'
}

完成

use AlexGeno\PhoneVerification\Storage\Redis;
use AlexGeno\PhoneVerification\Manager;
use AlexGeno\PhoneVerification\Exception\RateLimit;
use AlexGeno\PhoneVerification\Exception\Otp;

$config = [
    'rate_limits' => [
        'complete' => [
            'period_secs' => 300,
            'count' => 5,
            'message' =>
                fn($phone, $periodSecs, $count) =>
                    sprintf('You are trying to use an incorrect code %d times in %d minutes', $count, $periodSecs / 60)
        ]
    ],
    'otp' => [
        'message_expired' =>
            fn($periodSecs, $otp) =>
                sprintf('Code is expired. You have only %d minutes to use it.', $periodSecs / 60),
        'message_incorrect' =>  fn($otp) => 'Code is incorrect'
    ]
];
$storage = new Redis(new \Predis\Client('tcp://127.0.0.1:6379'));

try {
    (new Manager($storage, $config))->complete('+15417543010', 1234);
} catch (RateLimit | Otp $e) {
    // 'Code is incorrect' ||
    // 'Code is expired. You have only 5 minutes to use it.' ||
    // 'You are trying to use an incorrect code 5 times in 5 minutes'
    echo $e->getMessage();
}

注意:当然,您可以在代码的同一位置定义所有 $config 选项并实例化所有类。这里将其分开只是为了更清楚地说明哪些属于 启动阶段,哪些属于 完成阶段
注意:每个 $config 选项都有一个默认值。您只需重新定义您需要的选项。

MongoDb索引

如果您使用MongoDb作为 存储,您可能会注意到过期功能基于索引。它们可以自动创建。但建议仅在非生产环境中使用此选项。默认情况下是禁用的。

use AlexGeno\PhoneVerification\Storage\MongoDb;

$storage = new MongoDb(new \MongoDB\Client('mongodb://127.0.0.1:27017'), ['indexes'=> true]);

贡献

请参阅 CONTRIBUTING.md

开发

请参阅DEVELOPMENT.md,了解如何设置开发环境

许可证

电话验证的代码在MIT许可证的条款下分发。