Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Token Expiration Depending of Client Laravel Passport

I have a Laravel application that uses Passport authentication.

Login

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);
}

I have settled the expiration on AuthServiceProvider:

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

It works but after 1 minute the token expires. I want a different expiration date for token depending on where I'm trying to make login because I have a website, desktop app and an Android app.

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 the login, but is that a good way? Are there any other possible ways 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');
like image 716
LorenzoBerti Avatar asked Jan 07 '19 10:01

LorenzoBerti


People also ask

How can I get expired token in Laravel passport?

we can increase personal access token expire time of access token using personalAccessTokensExpireIn(). Let's see bellow example to set longer time of expire access token in laravel 5 application. * The policy mappings for the application. * Register any authentication / authorization services.

How can I check my passport token is valid or not in Laravel?

If you don't want to use the Passport middleware in the project where you want to validate the tokens, you would have to create an endpoint in the Laravel Passport server that can accept the token, perform the usual Passport validation and return a response to your service.

Does Laravel passport use JWT?

Passport uses JWT authentication as standard but also implements full OAuth 2.0 authorization.

How does Laravel Passport authentication work?

Laravel Passport is an easy way to set up an authentication system for your API. As a Laravel package, it uses an OAuth2 server to perform authentication, creating tokens for user applications that request to interface with the API it protects, and only granting them access if their tokens are validated.


1 Answers

You can get help from below link please find

For getting user agent detail and apply condition base on agent

for example you can use code like below

if ( Agent::isDesktop() ) {
    Passport::tokensExpireIn(now()->addYears(1));
} else if(Agent::isMobile()){
    Passport::tokensExpireIn(now()->addMonth(5));
}else{
    Passport::tokensExpireIn(now()->addHours(8));
}
like image 170
Adarsh Bhatt Avatar answered Sep 27 '22 22:09

Adarsh Bhatt