alhaji-aki / laravel-otp-token
为您的应用程序上的用户创建 otp 令牌
1.2.0
2024-03-15 16:58 UTC
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/contracts: ^10.0|^11.0
- illuminate/database: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- ext-pdo_sqlite: *
- larastan/larastan: ^2.0
- laravel/pint: ^1.13
- mockery/mockery: ^1.3
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.5
README
这是一个简单的包,用于帮助生成用户的 otp 令牌。此包遵循与 laravel 的密码重置功能相同的做法。
安装
您可以通过运行以下命令使用 composer 安装此包:
composer require "alhaji-aki/laravel-otp-token"
安装完成后,包将自动注册自己。运行以下命令发布迁移、配置和语言文件:
php artisan vendor:publish --provider="AlhajiAki\OtpToken\OtpTokenServiceProvider"
发布迁移后,您可以通过运行迁移来创建 otp_tokens 表:
php artisan migrate
配置文件的内容如下:
<?php return [ /* |-------------------------------------------------------------------------- | Otp Token Defaults |-------------------------------------------------------------------------- | | This option controls the default authentication "guard" and password | reset options for your application. You may change these defaults | as required, but they're a perfect start for most applications. | */ 'defaults' => [ 'otp_tokens' => 'users', ], /* |-------------------------------------------------------------------------- | Otp Tokens |-------------------------------------------------------------------------- | | You may specify multiple otp token configurations if you have more | than one user table or model in the application and you want to have | separate otp token settings based on the specific user types. | | The expire time is the number of seconds that the token should be | considered valid. This security feature keeps tokens short-lived so | they have less time to be guessed. You may change this as needed. | */ 'otp_tokens' => [ 'users' => [ 'provider' => 'users', 'table' => 'otp_tokens', 'expire' => 60, 'throttle' => 60, ], ], ];
使用此配置,您可以拥有多个 otp 令牌提供者,就像 laravel 的密码和身份验证提供者一样。
发布的语言文件看起来像:
<?php return [ /* |-------------------------------------------------------------------------- | Password Reset Language Lines |-------------------------------------------------------------------------- | | The following language lines are the default lines which match reasons | that are given by the password broker for a password update attempt | has failed, such as for an invalid token or invalid new password. | */ 'completed' => 'We have completed the verification process.', 'sent' => 'We have sent your otp token.', 'throttled' => 'Please wait before retrying.', 'token' => 'This otp token is invalid.', 'user' => "We can't find the user account.", ];
使用此配置,您可以拥有多个 otp 令牌提供者,就像 laravel 的密码和身份验证提供者一样。
使用
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use AlhajiAki\OtpToken\Contracts\CanSendOtpToken; use AlhajiAki\OtpToken\Traits\CanSendOtpToken as CanSendOtpTokenTrait; class YourModel extends Model implements CanSendOtpToken { use CanSendOtpTokenTrait; }
您希望生成 otp 令牌的模型应实现以下接口和特性
使用 getColumnForOtpToken($field)
获取与 otp 令牌相关联的字段值。这在例如您想要在验证过程中生成针对用户的电子邮件和手机号码的不同 otp 令牌时很有用。
OtpToken 门面公开了一个 sendOtpToken()
方法,它期望两个参数。它们是
- 一个包含要使用的字段、与令牌关联的操作以及指定字段的值的数组。
- 第二个参数是一个闭包,在生成 otp 令牌后执行。闭包将获取
CanSendOtpToken
合同的实例和生成的令牌。
此方法返回一个响应,可以是以下常量之一
- OTP_TOKEN_SENT: 当传递的闭包执行成功时。
- INVALID_USER: 当找不到用户时。
- OTP_TOKEN_THROTTLED: 用户必须等待几分钟才能创建新的令牌。
sendOtpToken()
的一个常见用法示例是将 otp 验证令牌发送到用户的手机号码。以下是一个示例实现
<?php namespace App\Http\Controllers; use AlhajiAki\OtpToken\OtpToken; use Illuminate\Http\Request; use Illuminate\Http\Response; class MobileNumberVerificationController extends Controller { public function store(Request $request) { $response = OtpToken->broker()->sendOtpToken([ 'mobile' => '+233248000000', 'action' => 'verify-user', 'field' => 'mobile' ], function ($user, $token) { $user->notify(new VerifyMobile($token)); }); return response()->json([ 'message' => trans($response) ], $response == OtpToken::OTP_TOKEN_SENT ? Response::HTTP_OK : Response::HTTP_UNAUTHORIZED); } }
门面还有一个 performAction()
方法,当提供的 otp 令牌有效时执行操作。此方法也期望两个参数。它们是
- 一个包含要使用的字段、这是在生成令牌时传递的字段以及与令牌关联的操作和指定字段的值的数组。
- 第二个参数是一个闭包,在验证 otp 令牌后执行。闭包将获取
CanSendOtpToken
合同的实例。
此方法返回一个响应,可以是以下常量之一
- ACTION_COMPLETED: 当传递的闭包执行成功时。
- INVALID_USER: 当找不到用户时。
- INVALID_TOKEN: 当提交的令牌无效时。
performAction()
的一个常见用法示例是验证 otp 令牌是否正确。以下是一个示例实现
<?php namespace App\Http\Controllers; use AlhajiAki\OtpToken\OtpToken; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Validation\ValidationException; class MobileNumberVerificationController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function verify(Request $request) { $response = OtpToken::performAction([ 'token' => $request->token, 'mobile' => $request->user()->mobile, 'action' => 'verify-user', 'field' => 'mobile' ], function ($user) { $user->update([ 'mobile_verified_at' => now() ]); } ); if ($response !== OtpToken::ACTION_COMPLETED) { throw ValidationException::withMessages([ 'token' => trans($response) ]); } return response()->json([ 'message' => trans($response) ], Response::HTTP_OK); } }
测试
vendor/bin/phpunit
贡献
有关详细信息,请参阅CONTRIBUTING。
许可
MIT 许可证(MIT)。有关更多信息,请参阅许可文件。