Laravel 5.6 pass oauth/token hanging

2019-03-06 07:19发布

问题:

I have set up a new laravel 5.6 install with Laravel Passport install. If I make a post request to http://127.0.0.1/oauth/token with Postman I get a valid token back:

Request

POST /oauth/token HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Cache-Control: no-cache
Postman-Token: 99529c07-0fe3-38a8-54cf-8b80a9dd5fbd

{ 
    "grant_type" : "password",
    "client_id" : 4, 
    "client_secret" : "Ib1UOS7BK12tFxOilqwea1XGJhrExbVYe8B7r8wK",
    "username" : "mail@mail.com",
    "password" : "password"
}

Response:

{
    "token_type": "Bearer",
    "expires_in": 31536000,
    "access_token": "eyJ0eXAiOiJKV1.....",
    "refresh_token": "def5020026dfeb6f91f6a9....."
}

I don't want my users accessing this directly so I have set up a route in the routes/web.php file:

Route::post('login', 'API\AuthController@login');

My AuthController looks like:

<?php

namespace App\Http\Controllers\API;

use App\OAuth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as Controller;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\User;

class AuthController extends Controller
{

public function __construct()
{
    $this->middleware('auth:api', ['except' => ['login']]);
}

/**
 * Login user and create token
 *
 * @param  [string] email
 * @param  [string] password
 * @param  [boolean] remember_me
 * @return [string] access_token
 * @return [string] token_type
 * @return [string] expires_at
 */
public function login(Request $request)
{

    $request->validate([
        'username' => 'required|string|email',
        'password' => 'required|string'
    ]);

    $credentials = request(['username', 'password']);

    return OAuth::login($credentials['username'], $credentials['password']);

}

This calls the login method of my OAuth class:

namespace App;

use GuzzleHttp;

class OAuth
{

public static function login($username, $password)
{
    $http = new GuzzleHttp\Client;

    $response = $http->post('http://127.0.0.1:8000/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => 4,
            'client_secret' => 'Ib1UOS7BK12tFxOilqwea1XGJhrExbVYe8B7r8wK',
            'username' => $username,
            'password' => $password
        ],
    ]);

    return json_decode((string) $response->getBody(), true);
}

}

When I use Postman make a post request to get a token:

POST /login HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Cache-Control: no-cache
Postman-Token: 94469bd8-a011-7e04-5f20-b835a5b1cac4

{
    "username" : "mail@mail.com",
    "password" : "password"
}

The request just hangs. It doesn't come back with any sort of response or timeout, it just seems to load forever. I'm not sure what is going wrong. If I make a post request to oauth/token from postman it works, but if I make a post request to a controller method which in turn makes a post request to oauth/token, it doesn't work. I'm not getting anything back.

回答1:

This is because the built-in PHP server is single threaded. Hence your second (internal) request to the OAuth server is killing the first one.

See my comment here.



回答2:

I have also tried to generate the token from curl or guzzle. try to use this code it works for me.

function tokenRequest(Request $request){

  $request->request->add([
                "grant_type" => "password",
                "username" => $request->username,
                "password" => $request->password,
                "client_id"     => "2",
                "client_secret" => "VE5S97DfjNciEWJOL1gEZhVLEx2VAGJQEX1dK6cq",
        ]);

        $tokenRequest = $request->create(
                env('APP_URL').'/oauth/token',
                'post'
        );

        $instance = Route::dispatch($tokenRequest);

        return json_decode($instance->getContent());

}