Multi Auth with Laravel 5.4 and Passport

后端 未结 7 2126
猫巷女王i
猫巷女王i 2020-12-31 20:46

I am trying to setup multi auth with Laravel Passport, but it doesn\'t seem to support it. I am using the Password Grant to issue tokens which requires me to pass username/p

7条回答
  •  萌比男神i
    2020-12-31 21:26

    If you still need.

    I prefer go with roles, there is an amazing plugin for that: https://github.com/larapacks/authorization

    But if you somehow needs that, you will be able to use following the steps bellow.

    For multi guards, you will have to overwrite some code.

    Instead of loading PassportServiceProvider, you create your own and extends the PassportServiceProvider and overwrites the method makePasswordGrant. On this method, you will change the Passport UserRepository for your own repository extended. On user repository you must to change the static model config for a dynamic one (I load from request attributes, but you can get from anywhere).

    You may have to overwrite something else, but I made a test and works.

    Ex:

    PassportServiceProvider

    namespace App\Providers;
    
    use League\OAuth2\Server\AuthorizationServer;
    use League\OAuth2\Server\Grant\PasswordGrant;
    use Laravel\Passport\PassportServiceProvider as BasePassportServiceProvider;
    use Laravel\Passport\Passport;
    
    class PassportServiceProvider extends BasePassportServiceProvider
    {
        /**
         * Create and configure a Password grant instance.
         *
         * @return PasswordGrant
         */
        protected function makePasswordGrant()
        {
            $grant = new PasswordGrant(
                $this->app->make(\App\Repositories\PassportUserRepository::class),
                $this->app->make(\Laravel\Passport\Bridge\RefreshTokenRepository::class)
            );
    
            $grant->setRefreshTokenTTL(Passport::refreshTokensExpireIn());
    
            return $grant;
        }
    
    }
    

    UserRepository

    namespace App\Repositories;
    
    use App;
    use Illuminate\Http\Request;
    use League\OAuth2\Server\Entities\ClientEntityInterface;
    use Laravel\Passport\Bridge\UserRepository;
    use Laravel\Passport\Bridge\User;
    use RuntimeException;
    
    class PassportUserRepository extends UserRepository
    {
        /**
         * {@inheritdoc}
         */
        public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
        {
            $guard = App::make(Request::class)->attributes->get('guard') ?: 'api';
            $provider = config("auth.guards.{$guard}.provider");
    
    
            if (is_null($model = config("auth.providers.{$provider}.model"))) {
                throw new RuntimeException('Unable to determine user model from configuration.');
            }
    
    
            if (method_exists($model, 'findForPassport')) {
                $user = (new $model)->findForPassport($username);
            } else {
                $user = (new $model)->where('email', $username)->first();
            }
    
    
            if (! $user ) {
                return;
            } elseif (method_exists($user, 'validateForPassportPasswordGrant')) {
                if (! $user->validateForPassportPasswordGrant($password)) {
                    return;
                }
            } elseif (! $this->hasher->check($password, $user->password)) {
                return;
            }
    
            return new User($user->getAuthIdentifier());
        }
    }
    

    PS: Sorry my bad english.

提交回复
热议问题