API Authentication not working?
I'm having trouble wrapping my head around the use of API token authentication in Laravel.
I've configured Passport, and I'm aiming for OAuth2 authentication. Right now, in my route, I have this simple call:
Route::get('/c', 'IndexController@courseInfo');
This takes me inside the courseInfo controller method, where the first thing I do is call call a route redirect to another route:
public function courseInfo(Request $request) {
$callback = self::getCallback();
dd($callback); //for testing purposes
...
}
...
public function getCallback() {
echo "entered getCallback()";
return redirect('/callback');
}
Which takes me to what I have basically copied-and-pasted from Passport documentation, here:
Route::get('/callback', function (Request $request) {
$state = $request->session()->pull('state');
throw_unless(
strlen($state) > 0 && $state === $request->state,
InvalidArgumentException::class
);
$http = new GuzzleHttp\Client;
$response = $http->post('http://my.test.instrucutre.com/login/oauth2/auth?', [
'headers' => [
'Accept' => 'application/json',
],
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => '12',
'client_secret' => 'ZOW9MBR86IBlkrAGnYzw6MTfMgCiSdzaAMkhhEsZ7', //example
'redirect_uri' => 'http://www.google.com', //for testing
'code' => $request->code,
],
]);
return json_decode((string) $response->getBody(), true);
});
I'll note that I am using a Canvas URL here. I'm aware the documentation example is http://your-app.com/oauth/token, but I'm guessing that since my application is Canvas-based, I should use the GET line as per the Canvas documentation here: https://canvas.instructure.com/doc/api/file.oauth.html#oauth2-flow-1
GET https://<canvas-install-url>/login/oauth2/auth?client_id=XXX&response_type=code&state=YYY&redirect_uri=https://example.com/oauth_complete
Since the next line after the method call is a die-and-dump, I get the screencapped results here: https://imgur.com/a/CjZIoZU
But it's missing something, according to this quote from the Passport documentation: "This /oauth/token route will return a JSON response containing access_token, refresh_token, and expires_in attributes. The expires_in attribute contains the number of seconds until the access token expires."
It says this, but when I examine my DD's results, I don't see these three attributes anywhere. In fact, the DD is identical to the results of a simple dd($request). Why is this happening?
TL;DR, the Passport API authentication code is giving me the results of my redirect to the route, not the authentication values themselves that I want.
If anyone could offer me some clarity, it would go a long way for me and I'd be very thankful. I'm only a week into learning middleware and Passport, and it's quite a lot to take in.
Please or to participate in this conversation.