Thursday, 17 January 2019

Different Token expiration depending of Client Laravel Passport

I have an app built in Laravel with Passport authentication.

I have my Login function like this:

public function authenticate(Request $request)
    {
        $params = [
            'grant_type'=>'password',
            'client_id'=> 1,
            'client_secret'=>"secret",
            'username'=>request('username'),
            'password'=>request('password'),
            'active'=>1,
            'scope'=>'*'
        ];

        $request->request->add($params);
        // verify the credentials and create a token for the user
        $proxy = Request::create('oauth/token', 'POST');

        return Route::dispatch($proxy);
    }

And I have setted the expiration on AuthServiceProvider :

Passport::routes(function ($router) {
   $router->forAccessTokens();
});
Passport::tokensExpireIn(now()->addMinute(1));
Passport::refreshTokensExpireIn(now()->addDays(30));

And it works, after 1 minute the token expires. Now I want a different expiration date for token depending on where I'm trying to make login because I have a web site, desktop app and Android app. So for example:

  • web app: 8 hours
  • desktop app: 1 Year
  • android app: 5 months

I was thinking send me from where i'm trying to make a login, but it is a good way? Are there any other ways and it's possible to do it?

For now I have tried this:

-) deleted From AuthServiceProvider:

Passport::tokensExpireIn(now()->addMinute(1));

And added in Login function:

.
.
.
if(request('from') == 'something'){
 Passport::tokensExpireIn(now()->addYears(1));
}else{
 Passport::tokensExpireIn(now()->addHours(8));
}

$proxy = Request::create('oauth/token', 'POST');



from Different Token expiration depending of Client Laravel Passport

No comments:

Post a Comment