【问题标题】:Custom grant type for PassportServiceProvider (Laravel)PassportServiceProvider (Laravel) 的自定义授权类型
【发布时间】:2017-02-03 13:15:15
【问题描述】:

尝试根据本手册实现自定义授权服务提供者:

https://github.com/mikemclin/passport-custom-request-grant

register method 中,我们获取授权服务器实例(使用辅助方法),然后使用 AuthServer 添加我们的授权:

public function register()
    {
        app(AuthorizationServer::class)->enableGrantType($this->makeCustomRequestGrant(), Passport::tokensExpireIn());
    }

这对我不起作用。我尝试以另一种方式注册我的赠款:

$this->app->singleton(AuthorizationServer::class, function () {
            return tap($this->makeAuthorizationServer(), function ($server) {
                $server->enableGrantType(
                    $this->makeCustomRequestGrant(), Passport::tokensExpireIn()
                );
            });
        });

如何使用另一个授权“扩展”我的单例服务器实例?就我而言,我只是实例化了新的,因此以前的授权类型变得不受支持。

主要目标是创建将使用另一种模型的授权 - 客户(而非用户)和授权激活码。用户将尝试使用 client_credentials 获取代码,然后他可以使用激活码授权进行 api 查询 - 使用另一个范围。

【问题讨论】:

    标签: laravel laravel-5.3 laravel-passport


    【解决方案1】:

    我知道这个答案可能会迟到,但我找到了解决这个要求的方法。

    我只是在我的 AuthServiceProvider 中将授权添加到服务器,并将授权逻辑提取到授权类以保持干净。

    您可以检查 PasswordGrant 类以作为基础。

    您好!

    namespace App\Providers;
    
    use App\Auth\Grants\FacebookGrant;
    use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
    use Laravel\Passport\Bridge\RefreshTokenRepository;
    use Laravel\Passport\Passport;
    use League\OAuth2\Server\AuthorizationServer;
    
    class AuthServiceProvider extends ServiceProvider
    {
        /**
         * The policy mappings for the application.
         *
         * @var array
         */
        protected $policies = [
            'App\Model' => 'App\Policies\ModelPolicy',
        ];
    
        /**
         * Register any authentication / authorization services.
         *
         * @return void
         */
        public function boot()
        {
            $this->registerPolicies();
    
            app(AuthorizationServer::class)->enableGrantType(
                $this->makeFacebookGrant(), Passport::tokensExpireIn()
            );
    
            Passport::routes();
    
            //
        }
    
        /**
         * Create and configure a Facebook grant instance.
         *
         * @return FacebookGrant
         */
        protected function makeFacebookGrant()
        {
            $grant = new FacebookGrant(
                $this->app->make(RefreshTokenRepository::class)
            );
    
            $grant->setRefreshTokenTTL(Passport::refreshTokensExpireIn());
    
            return $grant;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-13
      • 2021-10-01
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2015-01-30
      • 1970-01-01
      相关资源
      最近更新 更多