Skip to content

Latest commit

 

History

History
142 lines (102 loc) · 3.95 KB

File metadata and controls

142 lines (102 loc) · 3.95 KB

Laravel Passport Phone Verification Code Grant

介绍

基于Laravel Passport的手机验证码授权

安装

进入到你的项目目录,然后运行以下命令

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

设置

Laravel

如果你的Laravel版本大于等于5.5,服务提供者将会自动注册到程序中。

其他版本你需要把\QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class添加到config/app.php中的providers属性中。

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

Lumen

你可以在app/Providers/AppServiceProvider.phpregister函数中添加以下代码注册服务提供者。

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

如何使用?

配置

  1. 你必须在 User Model中实现 \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. User Model中实现接口定义的 findOrNewForPassportVerifyCodeGrant 方法和 validateForPassportVerifyCodeGrant 方法。

    /**
     * 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',
        ],
    //...

请求Token

你可以使用POST方式访问/oautn/token接口来获取Token,具体请求参数参照以下代码。

$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

期待你的想法或代码。

Issues

如果你有任何问题,请在 Issues 中提出,我将会尽力为你解决。