Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Passport Queries time of authenticating

For every request I found that 4 queries are fired to validate the user and the token. Among them one is to fetch the user (select * from user) based on the user id. This queries are fired by Passport/Laravel But what I want is to modify this query to add one status field check also to check if any user become invalid during the token validity period. If we only check with the id then if any user become inactive(By changing status then also we will not be able to stop the user as deleting the token for the user is not a good solution for me).

Queries Fired on every request by Passport Laravel:

select * from oauth_access_tokens where id = ? 
select * from user where id = ? limit 1 ["2"] 
select * from oauth_access_tokens where id = ? 
select * from oauth_clients where id = ?

So, can anyone tell me how to change the 'select * from user where id' query in passport at time of Token validation.

like image 445
Souvik Avatar asked Dec 12 '18 09:12

Souvik


People also ask

What is Passport authentication in laravel?

Laravel Passport is an OAuth 2.0 server implementation for API authentication using Laravel. Since tokens are generally used in API authentication, Laravel Passport provides an easy and secure way to implement token authorization on an OAuth 2.0 server.

Does laravel Passport use JWT?

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

What is Passport API?

Introduction. 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.

How can I expire my Passport token in laravel?

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.


2 Answers

You can add this method on your User model (or any model you're authenticating with passport)

    ...
    public function findForPassport($username)
    {
        return $user = (new self)->where('email', $username)->where('is_active', 1)->first();
    }
    ...

of course you can modify is_active by whichever column you are using (and/or any query constraint for that matter), as long as it returns Illuminate\Contracts\Auth\Authenticatable contract.

like image 88
veelasky Avatar answered Sep 28 '22 00:09

veelasky


I wouldn't try and modify passports default behaviour as I have no idea what else it might impact both now and in future upgrades.

Your best bet might be to hook into the passport events and apply you business logic to a listener that is called when the events are fired

like image 44
Souvik Avatar answered Sep 28 '22 02:09

Souvik