Passport: Token Request : Additional User Model Conditions

Created on 31 Dec 2016  路  13Comments  路  Source: laravel/passport

I am building an API for mobile application using _Password Grant Tokens_. When user tries to login to the application, client sends a request for the access token.

It is possible that the user has not verified his account using the link sent to his email. I wish to add an additional condition to the query and provide error response accordingly. Currently, as Passport manages token part, I can't do it directly.

How can this be solved? How can I dive in the token request and send custom response if there are any issues with user account? And continue sending token otherwise.

Most helpful comment

If I understand your problem correctly, I solved this as follows:

I made my own oauth/token route and put it in an oauth.php file within /routes:

Route::post('/oauth/token', [
    'uses' => 'Auth\CustomAccessTokenController@issueUserToken'
]);

Then I have my CustomAccessTokenController.php:

<?php

namespace App\Http\Controllers\Auth;

use Psr\Http\Message\ServerRequestInterface;
use Laravel\Passport\Http\Controllers\AccessTokenController;

class CustomAccessTokenController extends AccessTokenController
{
    /**
     * Hooks in before the AccessTokenController issues a token
     *
     *
     * @param  ServerRequestInterface $request
     * @return mixed
     */
    public function issueUserToken(ServerRequestInterface $request)
    {
        $httpRequest = request();

        // 1.
        if ($httpRequest->grant_type == 'password') {
            // 2.
            $user = \App\User::where('email', $httpRequest->username)->first();

            // Perform your validation here

            // If the validation is successfull:
            return $this->issueToken($request);
        }
    }
}

All 13 comments

If I understand your problem correctly, I solved this as follows:

I made my own oauth/token route and put it in an oauth.php file within /routes:

Route::post('/oauth/token', [
    'uses' => 'Auth\CustomAccessTokenController@issueUserToken'
]);

Then I have my CustomAccessTokenController.php:

<?php

namespace App\Http\Controllers\Auth;

use Psr\Http\Message\ServerRequestInterface;
use Laravel\Passport\Http\Controllers\AccessTokenController;

class CustomAccessTokenController extends AccessTokenController
{
    /**
     * Hooks in before the AccessTokenController issues a token
     *
     *
     * @param  ServerRequestInterface $request
     * @return mixed
     */
    public function issueUserToken(ServerRequestInterface $request)
    {
        $httpRequest = request();

        // 1.
        if ($httpRequest->grant_type == 'password') {
            // 2.
            $user = \App\User::where('email', $httpRequest->username)->first();

            // Perform your validation here

            // If the validation is successfull:
            return $this->issueToken($request);
        }
    }
}

@gauravmak please take a look at @etelford comment, I believe it's a very good solution to your problem.

Yes, thanks @etelford and @themsaid.

@etelford Do I have to name the new route file to be oauth.php? And how to connect that with _Passport_?

And @themsaid, when is the next release schedules? Waiting for few things which are already added to master.

@gauravmak You don't have to, but this is what I chose to do to keep it separate from my web and api routes. You can just as easily put it in web.php.

Thanks.

Hello all, i tested this solution but i get a TokenMismatchException. any solution for this?

Hi
is there way with access token send a extra value or parameter?
What I want is, when I issue a access token I want to send an other value to identify this token belongs to this campaign so only user can access to particular campaign area

Hi
When we use this solution, other grant_types except password grant type should be implemented in issueUserToken function by adding another conditions to it.
Is there any way to extend default passport function?

Route::post('**/oauth/token**', [
    'uses' => 'Auth\CustomAccessTokenController@issueUserToken'
]);

Right, but i think the route has to be like this:
Route::post('token', [
'uses' => 'Auth\CustomAccessTokenController@issueUserToken'
]);

Thank you very very much @etelford, it is the perfect solution!

@gauravmak You need to register the new route file in app/Providers/RouteServiceProvider@map, in the same way that other routes have been added

Hi,
I get this error after calling issueToken function
Call to a member function respondToAccessTokenRequest() on null

Hi,
I get this error after calling issueToken function
Call to a member function respondToAccessTokenRequest() on null

Have you got any solution?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mind-control picture mind-control  路  3Comments

brryfrmnn picture brryfrmnn  路  3Comments

gbgelado picture gbgelado  路  3Comments

soubhikchatterjee picture soubhikchatterjee  路  4Comments

aluferraz picture aluferraz  路  3Comments