njoguamos/laravel-otp

为Laravel 11+生成和验证一次性密码(OTP)的Composer包。

v1.0.9 2024-09-23 13:57 UTC

README

Latest Version on Packagist run-tests Fix PHP code style issues Total Downloads

为Laravel 11+生成和验证一次性密码(OTP)的Composer包。

安装

您可以通过composer安装此包

composer require njoguamos/laravel-otp

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="otp-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="otp-config"
这是已发布配置文件的内容
return [

    /*
    |--------------------------------------------------------------------------
    |  OTP Length
    |--------------------------------------------------------------------------
    |
    | This is the length of the generated OTP token. By default it is set to
    | 6 digits. The length of the OTP must be at least 4 digits to ensure
    | that the OTP is not easily guessable
    |
    */

    'length' => env(key: 'OPT_LENGTH', default: 6),

    /*
    |--------------------------------------------------------------------------
    | OTP Validity time by minutes
    |--------------------------------------------------------------------------
    |
    | This is the validity time of the generated OTP token. By default it is
    | set to 10 minutes. This means that the OTP will be valid for 10 minutes
    | after it is generated. You can change this value to suit your needs.
    |
    */

    'validity' => env(key: 'OTP_VALIDITY', default: 10),

    /*
    |--------------------------------------------------------------------------
    | Digits Only
    |-------------------------------------------------------------------------
    |
    | When set to true, the generated OTP will only contain digits. When set
    | to false, the generated OTP will contain both digits and alphanumeric
    | characters which makes it more difficult to guess the OTP.
    |
    */

    'digits_only' => env(key: 'OTP_DIGITS_ONLY', default: true),
];

使用方法

生成OTP

要生成OTP,您可以在Otp类上使用generate()方法。该方法需要一个标识符作为参数。标识符可以是电子邮件地址、电话号码或任何其他您想用来识别用户的唯一标识符。

use NjoguAmos\Otp\Otp;

$otp = Otp::generate(identifier: 'example@gmail.com');

$otp->identifier; # example@gmail.com
$otp->token; # 123456
$otp->expires_in; # 10 minutes

generate()方法返回\NjoguAmos\Otp\Models\Otp Eloquent Models类的实例。您可以访问Otp类的identifier、token和expires_at属性。

例如:您可以使用token属性将OTP发送到用户的电子邮件地址。

use NjoguAmos\Otp\Otp;
use App\Mail\OTPMail;
use Illuminate\Support\Facades\Mail;

$email = 'example@gmail.com';

$otp = Otp::generate(identifier: $email);

Mail::to($email)->send(new OTPMail($otp));

验证OTP

要验证OTP,您可以在Otp类上使用validate()方法。该方法需要标识符和token作为参数。

如果OTP有效,该方法将返回true。否则,它将返回false。

use NjoguAmos\Otp\Otp;

$email = 'example@gmail.com';
$otp = '123456';

$validated = Otp::validate(identifier: $email, token: $otp->token);

$validated // True or False

注意

建议不要让用户知道OTP不匹配、不存在或已过期。您可以返回一个通用的消息给用户。

删除过期的OTP

要定期删除过期的OTP,您可以使用model:prune Artisan命令。此命令将从数据库中删除所有过期的OTP。

要这样做,将model:prune添加到您的routes/console.php文件中

use Illuminate\Support\Facades\Schedule;
use NjoguAmos\Otp\Models\Otp as OtpModel;

Schedule::command('model:prune', ['--model' => [OtpModel::class]])->everyFiveMinutes();

提示

请确保持续时间大于OTP的有效期。

安全

提示

为了防止暴力攻击,对生成或验证OTP令牌的尝试次数进行速率限制。这可以通过使用Laravel的RateLimit中间件来完成。只要令牌有效,用户就可以验证令牌。这意味着用户可以有多个有效的令牌。一旦令牌过期,即使它仍然存在于数据库中,它也不能再是有效的。计划每5分钟运行一次model:prune命令来删除过期的令牌。

测试

composer test

变更日志

请参阅RELEASE获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请审查我们的安全策略以了解如何报告安全漏洞。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件