Laravel Passport token lifetime

前端 未结 10 1345
轮回少年
轮回少年 2021-01-04 01:12

I don\'t get what I\'m doing wrong. I can\'t set token expiration time.



        
相关标签:
10条回答
  • 2021-01-04 01:37

    Ah, figured out the personal tokens are always long-lived and this cannot be configured :(

    0 讨论(0)
  • 2021-01-04 01:37

    if you do

    $token->expires_at =
            Carbon::now()->addDays(env('PERSONAL_ACCESS_TOKEN_EXPIRY__DAYS'));
    

    then the expiration date is not checked at any request, so I think it's not a valid option for personal tokens.

    0 讨论(0)
  • 2021-01-04 01:38

    The createToken() method creates a Personal Access Token. By default, these tokens expire after 1 year (or 100 years, if created by laravel/passport <= 1.0.11). The expiration time for this type of token is not modified by the Passport::tokensExpireIn() or Passport::refreshTokensExpireIn() methods.

    laravel/passport >= 7.0.4

    Passport version 7.0.4 added a new method Passport::personalAccessTokensExpireIn() that allows you to update the expiration time for personal access tokens. If you are on this version or later, you can add this method call to your AuthServiceProvider::boot() method.

    Passport::personalAccessTokensExpireIn(Carbon::now()->addDays(1));
    

    laravel/passport < 7.0.4

    If you are not yet on passport version 7.0.4, you can still modify the personal access token expiration time, but it is more manual. You will need to enable a new instance of the personal access grant with your desired expiration time. This can also be done in your AuthServiceProvider::boot() method.

    $server = $this->app->make(\League\OAuth2\Server\AuthorizationServer::class);
    $server->enableGrantType(new \Laravel\Passport\Bridge\PersonalAccessGrant(), new \DateInterval('P100Y'));
    

    Note

    Modifying the expires_at field in the database will not do anything. The real expiration date is stored inside the token itself. Also, attempting to modify the exp claim inside the JWT token will not work, since the token is signed and any modification to it will invalidate it. So, all your existing tokens will have their original expiration times, and there is no way to change that. If needed, you will need to regenerate new tokens.

    0 讨论(0)
  • 2021-01-04 01:39

    Please see this implementation, and here how to replace PassportServiceProvider by your's. It worked for me with Laravel 5.5

    0 讨论(0)
  • 2021-01-04 01:44

    I was able to extend the lifetime of the Personal access token by creating a PassportServiceProvider in my project that extends the PassportServiceProvider from the laravel-passport package. Then I just added this method to override the one from the PassportServiceProvider.

    
    
        /**
         * Register the authorization server.
         *
         * @return void
         */
        protected function registerAuthorizationServer()
        {
            $this->app->singleton(AuthorizationServer::class, function () {
                return tap($this->makeAuthorizationServer(), function ($server) {
                    $server->enableGrantType(
                        $this->makeAuthCodeGrant(), Passport::tokensExpireIn()
                    );
    
                    $server->enableGrantType(
                        $this->makeRefreshTokenGrant(), Passport::tokensExpireIn()
                    );
    
                    $server->enableGrantType(
                        $this->makePasswordGrant(), Passport::tokensExpireIn()
                    );
    
                    $server->enableGrantType(
                        new PersonalAccessGrant(), Passport::tokensExpireIn() // this is the line that was updated from the original method
                    );
    
                    $server->enableGrantType(
                        new ClientCredentialsGrant(), Passport::tokensExpireIn()
                    );
    
                    if (Passport::$implicitGrantEnabled) {
                        $server->enableGrantType(
                            $this->makeImplicitGrant(), Passport::tokensExpireIn()
                        );
                    }
                });
            });
        }
    

    Then I just updated the provider in the app.php config file to use the one from my project.

    0 讨论(0)
  • 2021-01-04 01:45

    The Passport docs seem to answer this question

    https://laravel.com/docs/5.6/passport#token-lifetimes

    In the boot method of AuthServiceProvider call Passport::tokenExpiresIn()

    public function boot()
    {
        $this->registerPolicies();
    
        Passport::routes();
    
        Passport::tokensExpireIn(now()->addDays(15));
    
        Passport::refreshTokensExpireIn(now()->addDays(30));
    }
    
    0 讨论(0)
提交回复
热议问题