Laravel Passport - 使用自定义 UserProvider 针对 api 验证 Web 应用程序

Laravel Passport - Authenticating web app against api with Custom UserProvider

我真的是新构建 laravel 应用程序,我有一个 restful laravel API 和一个网络应用程序,我希望客户端网络应用程序进行身份验证API 并将用户存储在会话中,我已经注册了一个新的 UserProvider 并将其设置在配置的身份验证上,如下所示

服务提供商

public function boot()
{
    $this->registerPolicies();

    Auth::provider('apiAuthServiceProvider', function ($app, $config) {
        return new UserProvider(new ApiUserService());
    });
}

Config/Auth

'providers' => [
    'users' => [
        'driver' => 'apiAuthServiceProvider',
    ],
],

用户提供者Class

    <?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;

    class UserProvider implements IlluminateUserProvider
    {
        private $userService;

        public function __construct($userService)
        {
            $this->userService = $userService;
        }

        /**
         * @param  mixed  $identifier
         * @return \Illuminate\Contracts\Auth\Authenticatable|null
         */
        public function retrieveById($identifier)
        {
            // Get and return a user by their unique identifier
        }

        /**
         * @param  mixed   $identifier
         * @param  string  $token
         * @return \Illuminate\Contracts\Auth\Authenticatable|null
         */
        public function retrieveByToken($identifier, $token)
        {
            // Get and return a user by their unique identifier and "remember me" token
        }

        /**
         * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
         * @param  string  $token
         * @return void
         */
        public function updateRememberToken(Authenticatable $user, $token)
        {
            // Save the given "remember me" token for the given user
        }

        /**
         * Retrieve a user by the given credentials.
         *
         * @param  array  $credentials
         * @return \Illuminate\Contracts\Auth\Authenticatable|null
         */
        public function retrieveByCredentials(array $credentials)
        {
            // Get and return a user by looking up the given credentials
        }

        /**
         * Validate a user against the given credentials.
         *
         * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
         * @param  array  $credentials
         * @return bool
         */
        public function validateCredentials(Authenticatable $user, array $credentials)
        {
            // Check that given credentials belong to the given user
        }
    }

Custom UserProvider 注入一个 UserService class,负责向 API 和 return 用户发出请求...

我很迷茫,我应该从 "UserProvider" 接口重写哪些 UserProvider 方法? "retrieveById"、"retrieveByToken"、"updateRememberToken"、"retrieveByCredentials" 和 "validateCredentials"?还是我应该覆盖所有这些?考虑到客户端 Web 应用程序将有一个登录表单,并且用户将验证发送电子邮件和密码(grant_type = 密码),我也对令牌感到困惑,我应该如何存储令牌和刷新令牌在会话中?是否可以将会话超时设置为与令牌过期时间相同?我在哪里调用 retrieveByCredentials 的 UserProvider 来传递身份验证参数?提前致谢....

您应该只覆盖您需要的功能。大多数 standard 功能应该已经在您继承的用户提供程序中定义。我仅从 Illuminate\Auth\EloquentUserProvider(此处为 Laravel 5.4)继承了我的自定义用户提供程序,因此请仔细检查您从中继承的 class 是如何工作的。例如,如果您需要通过不同于默认 ID 字段的 ID 检索您的用户,您应该覆盖 retrieveById