qiutuleng/laravel-passport-phone-verification-code-grant

Laravel Passport 的资源所有者手机验证码凭据授权

v2.0.3 2020-02-27 23:03 UTC

This package is auto-updated.

Last update: 2024-08-28 08:38:29 UTC


README

简介

Laravel Passport 的资源所有者手机验证码凭据授权

中文文档 / 中文文档

安装

在您的项目工作目录下,并在终端运行以下命令

composer require qiutuleng/laravel-passport-phone-verification-code-grant

设置

Laravel

如果您的 Laravel 版本大于或等于 5.5,服务提供程序将自动附加。

其他版本,您必须在 config/app.phpproviders 数组中添加 \QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class

'providers' => [
    /*
     * Package Service Providers...
     */
     ...
     \QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class,
]

Lumen

$app->register(\QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class);

如何使用?

配置

  1. 您必须在您的 User 模型中实现 \QiuTuleng\PhoneVerificationCodeGrant\Interfaces\PhoneVerificationCodeGrantUserInterface 接口。

    <?php
    
    namespace App;
    
    use Laravel\Passport\HasApiTokens;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use QiuTuleng\PhoneVerificationCodeGrant\Interfaces\PhoneVerificationCodeGrantUserInterface;
    
    class User extends Authenticatable implements PhoneVerificationCodeGrantUserInterface
    {
        use HasApiTokens, Notifiable;
    }
  2. findOrNewForPassportVerifyCodeGrantvalidateForPassportVerifyCodeGrant 方法添加到您的 User 模型。

    /**
     * Find or create a user by phone number
     *
     * @param $phoneNumber
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function findOrCreateForPassportVerifyCodeGrant($phoneNumber)
    {
        // If you need to automatically register the user.
        return static::firstOrCreate(['mobile' => $phoneNumber]);
    
        // If the phone number is not exists in users table, will be fail to authenticate.
        // return static::where('mobile', '=', $phoneNumber)->first();
    }
    
    /**
     * Check the verification code is valid.
     *
     * @param $verificationCode
     * @return boolean
     */
    public function validateForPassportVerifyCodeGrant($verificationCode)
    {
        // Check verification code is valid.
        // return \App\Code::where('mobile', $this->mobile)->where('code', '=', $verificationCode)->where('expired_at', '>', now()->toDatetimeString())->exists();
        return true;
    }
  3. (可选) 您还可以在配置文件中重命名 phone_numberverification_code 字段

    为此,在 config/passport.php 中添加键,例如

    //...
        'phone_verification' => [
            'phone_number_request_key' => 'phone',
            'verification_code_request_key' => 'verification_code',
        ],
    //...

请求令牌

您可以通过向 /oauth/token 路径发出带有用户手机号码和验证码的 POST 请求来请求访问令牌。

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'phone_verification_code',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'phone_number' => '+8613416292625',
        'verification_code' => 927068,
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);

更多

您可以查看 Laravel/Passport 官方文档以了解更多信息

贡献

您可以为这个存储库创建 pull requests

欢迎您的想法或代码。

问题

如果您有任何问题,请在 问题 中提出,我会尽力帮助您。