Passport: Personal Access Token expiration can't be set and it fails to check the Y2K38 bug.

Created on 30 Oct 2016  ·  14Comments  ·  Source: laravel/passport

Using the Passport facade to set the Expiration time for Authorization Code and Password Grant you can avoid the dreaded Y2K38 bug but Personal Access Tokens are hardcoded to last another hundred years from now, which puts it well beyond 2038.

This is a problem because on most Windows environments the tokens will never match and never authenticate the user.

The only way to bypass this is to modify the vendor files and do it every time Passport is upgraded.

Most helpful comment

To change the expiration date of a PersonalAccessToken could create a ServiceProvider that extends from the Passport Service Provider. for example: To add 1 hour edit method registerAuthorizationServer() and change this

```
$server->enableGrantType(
new PersonalAccessGrant, new DateInterval('P1Y')
);

for this 

$server->enableGrantType(
new PersonalAccessGrant, new DateInterval('PT1H')
);


namespace AppProviders;

use DateInterval;
use LaravelPassportBridgePersonalAccessGrant;
use LaravelPassportPassport;
use LaravelPassportPassportServiceProvider;
use LeagueOAuth2ServerAuthorizationServer;
use LeagueOAuth2ServerGrantClientCredentialsGrant;

class YourPassportServiceProvider extends PassportServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
parent::boot();
}

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    parent::register();
}

/**
 * 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, new DateInterval('PT1H')
            );

            $server->enableGrantType(
                new ClientCredentialsGrant, Passport::tokensExpireIn()
            );

            if (Passport::$implicitGrantEnabled) {
                $server->enableGrantType(
                    $this->makeImplicitGrant(), Passport::tokensExpireIn()
                );
            }
        });
    });
}

}

```

All 14 comments

Yeah I always get negative value of expires_in ( -1477799197 ) and the only way to fix it is to modify the vendor files.

Yes, I just wasted one day to find this problem, It happend only happend on 32bit php.

For now, maybe we need modify passport/src/Passport.php @ 190 line, new DateInterval('P100Y') --> new DateInterval('P1Y');.

100 Years is really too long :-D

Yeah I always get negative value of expires_in ( -1477799197 ) and the only way to fix it is to modify the Vendor files.

@nugrahawahyu you can simply change the token-lifetime - it solved my problems

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
        Passport::tokensExpireIn(Carbon::now()->addDays(30));
    }

TokensExpireIn 'only' works for Authorization Code and Password Grant tokens, not for personal access tokens.

a PR was submitted to solve the issue: https://github.com/laravel/passport/pull/185

To change the expiration date of a PersonalAccessToken could create a ServiceProvider that extends from the Passport Service Provider. for example: To add 1 hour edit method registerAuthorizationServer() and change this

```
$server->enableGrantType(
new PersonalAccessGrant, new DateInterval('P1Y')
);

for this 

$server->enableGrantType(
new PersonalAccessGrant, new DateInterval('PT1H')
);


namespace AppProviders;

use DateInterval;
use LaravelPassportBridgePersonalAccessGrant;
use LaravelPassportPassport;
use LaravelPassportPassportServiceProvider;
use LeagueOAuth2ServerAuthorizationServer;
use LeagueOAuth2ServerGrantClientCredentialsGrant;

class YourPassportServiceProvider extends PassportServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
parent::boot();
}

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    parent::register();
}

/**
 * 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, new DateInterval('PT1H')
            );

            $server->enableGrantType(
                new ClientCredentialsGrant, Passport::tokensExpireIn()
            );

            if (Passport::$implicitGrantEnabled) {
                $server->enableGrantType(
                    $this->makeImplicitGrant(), Passport::tokensExpireIn()
                );
            }
        });
    });
}

}

```

Hi guys, I've set the expires_at date in database to one year before now. But it seems Passport isn't checking expiration dates on personal access tokens. That's a default behavior? Thanks.

@diazemiliano in my case it doesn't validated at all, no matter which type of client I'm used.

My password granted tokens seem to be expiring much more quickly than Carbon::now()->addDays(1)... they seem to last about 2 hours max

$authorizarionServer = app()->make(LeagueOAuth2ServerAuthorizationServer::class);
$authorizationServer->enableGrantType(
new PersonalAccessGrant, new DateInterval('PT1M')
);

    return JsonResponse::create(
        $user->createToken(
            $user->getAttribute('name') . " " . Carbon::now(),
            $scopes = ""
        ),
        JsonResponse::HTTP_OK
    );

十分感谢 @DiegoGutman 的方案完美解决了我的问题 ,向你致敬

Thanks @dgutman10 !
This works :)

One can simply update the expiry time for Personal token using personalAccessTokensExpireIn method in AuthServiceProvider's boot method.
Check https://stackoverflow.com/a/54196090/3535399

You are trying to create Personal Access Token.

// Passport::tokensExpireIn(now()->addDays(15));
// Passport::refreshTokensExpireIn(now()->addDays(30));

Get or set when personal access tokens expire.

Passport::personalAccessTokensExpireIn(now()->addHour(1));
Result :

array:2 [
"token" => "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...."
"ExpireTime" => "59 minutes from now"
]

Was this page helpful?
0 / 5 - 0 ratings