Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a token for a Password Grant Client using Laravel Passport?

I am building an API and I am using Laravel Passport for authentication.

The API is being used for our mobile app so we're using the Password Grant Client.

Everything works great, and a user can login to get an access token. We have created a register endpoint which allows a user to sign up. We need the API to return an access token at this point too.

Looking through the docs there is no way to create an access token programmatically.

How can I create an access token for a Password Grant Client in my controller? I obviously don't want to do a HTTP request to my own API to get it.

I know I can use a Personal Access Grant Client and call createToken on the user model, but that means the access token is associated with a different Client. This doesn't seem right to me.

like image 506
Mike Avatar asked Jul 10 '17 11:07

Mike


People also ask

How can I get token in Laravel Passport?

Requesting Tokens Once you have created a password grant client, you may request an access token by issuing a POST request to the /oauth/token route with the user's email address and password. Remember, this route is already registered by the Passport::routes method so there is no need to define it manually.

Does Laravel Passport use JWT?

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

What is a Passport token?

PASSporT is sort of an acronym for Personal ASSertion Token. It's sometimes called an Identity token. It contains the information that STIR/SHAKEN needs for authentication and verification of calls. PASSporTs are formatted as JSON Web Tokens.


2 Answers

I've been toying with Passport for a couple of weeks now and from what I've seen in the documentation it doesn't expose many of the methods it uses for creating tokens. While you may not easily be able to "create an access token for a Password Grant Client in my controller" - what you can do is use Route::dispatch to forward the request for a token to your Passport Password Grant route.

To do this in the controller you are using to issue tokens, use the AuthenticatesUsers trait so you have access to the Password Grant route, create a request, and dispatch that request to the Password Grant route:

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class IssueTokensController extends Controller
{

    use AuthenticatesUsers;

    protected function issueApiToken(Request $request)
    {
        // forward the request to the oauth token request endpoint
        $tokenRequest = Request::create(
            '/oauth/token',
            'post'
        );
        return Route::dispatch($tokenRequest);
    }

}

This method of course requires you to have set up Passport and a Password Grant Client.

This answer is based off of another answer to a similar question by Raymond Lagonda - see https://stackoverflow.com/a/40433000/4991377

like image 114
LeviJames Avatar answered Sep 16 '22 16:09

LeviJames


Try something like this

class UserController extends Controller
{
    protected function login(Request $request)
    {

         $request->request->add([
            'grant_type'    => 'password',
            'client_id'     => '3',
            'client_secret' => '6BHCRpB4tpXnQvC1DmpT7CXCSz7ukdw7IeZofiKn',
            'scope' => '*'
        ]);

        // forward the request to the oauth token request endpoint
        $tokenRequest = Request::create('/oauth/token','post');
        return Route::dispatch($tokenRequest);
    }

}
like image 27
Sanju Kaniyamattam Avatar answered Sep 18 '22 16:09

Sanju Kaniyamattam