Search code examples
lumenlaravel-passportguzzle

Why my postman hang forever with with GuzzleHttp\Client request?


Working with lumen-passport in lumen 8 I got client_secret and client_id and make a request with GuzzleHttp\Client and my postman hang forever:

My postman : https://i.sstatic.net/sYlNK.jpg

In routes/web.php :

$router->group(['prefix'=>'api/v1'], function() use($router){

$router->post('/register','AuthController@register');
$router->post('/login', 'AuthController@login');

In my control :

$client = new Client();

    try {
         $clientResponse = $client->post( 'http://localhost:8000/api/v1/oauth/token' /* config('service.passport.login_endpoint' ) */ , [
            "form_params" => [
                "client_secret" => 'DfiXey63ABDjgX7upuNaGmGvvASzhGq9kjZTV9nm', //config('service.passport.client_secret'),
                "client_id" => 2, //config('service.passport.client_id'),
                "grant_type" => "client_credentials",
                "username" => $request->email,
                "password" => $request->password
            ]
        ]);
        \Log::info(  varDump(-3, ' -3 login $clientResponse::') );
        return $clientResponse;
    } catch (BadResponseException $e) {
        \Log::info(  varDump(-4, ' -4 login ::') );
        \Log::info(  varDump($e->getMessage(), ' -4 login $e->getMessage()::') );
        return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
    }

In bootstrap/app.php I have :

// Enable auth middleware (shipped with Lumen)
$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);

//$app->configure('app');
$app->register(App\Providers\AppServiceProvider::class);

// Enable auth middleware (shipped with Lumen)
$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);

//$app->configure('app');
$app->register(App\Providers\AppServiceProvider::class);


// Finally register two service providers - original one and Lumen adapter
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);

// Finally register two service providers - original one and Lumen adapter
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);

...
\Dusterio\LumenPassport\LumenPassport::routes($app, ['prefix' => 'api/v1/oauth']);

Under console I see :

ProjectName$ php -S localhost:8000 -t public
[Tue Jun  1 07:27:23 2021] PHP 7.4.18 Development Server (http://localhost:8000) started
[Tue Jun  1 07:27:29 2021] 127.0.0.1:45940 Accepted
[Tue Jun  1 07:27:29 2021] 127.0.0.1:45944 Accepted
[Tue Jun  1 07:27:29 2021] 127.0.0.1:45940 [200]: GET /
[Tue Jun  1 07:27:29 2021] 127.0.0.1:45940 Closing
[Tue Jun  1 07:27:31 2021] 127.0.0.1:45948 Accepted
[Tue Jun  1 07:27:31 2021] 127.0.0.1:45944 [200]: GET /
[Tue Jun  1 07:27:31 2021] 127.0.0.1:45944 Closing
[Tue Jun  1 07:27:41 2021] 127.0.0.1:45956 Accepted

I tried to debug guzzle source and found that it hang in file /vendor/guzzlehttp/guzzle/src/Client.php, in method:

private function transfer(RequestInterface $request, array $options): PromiseInterface
{
    $request = $this->applyOptions($request, $options);
    /** @var HandlerStack $handler */
    $handler = $options['handler'];
    \Log::info(  varDump(-21, ' -21 transfer::') );
    \Log::info(  varDump($handler, ' $handler transfer::') );
    try {
        return P\Create::promiseFor($handler($request, $options)); // I suppose it hang forever here
    } catch (\Exception $e) {
        return P\Create::rejectionFor($e);
    }
}

Checking content of $handler in log file I see :

[2021-06-01 04:54:47] local.INFO:  (Object of GuzzleHttp\HandlerStack) : $handler transfer:: : Array
(
[ GuzzleHttp\HandlerStack handler] => Closure Object
(
[static] => Array
(
[default] => Closure Object
(
[static] => Array
(
[default] => GuzzleHttp\Handler\CurlMultiHandler Object
(
[factory:GuzzleHttp\Handler\CurlMultiHandler:private] => GuzzleHttp\Handler\CurlFactory Object
(
[handles:GuzzleHttp\Handler\CurlFactory:private] => Array
(
)

[maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 50
)

[selectTimeout:GuzzleHttp\Handler\CurlMultiHandler:private] => 1
[active:GuzzleHttp\Handler\CurlMultiHandler:private] =>
[handles:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)

[delays:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)

[options:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)

)

[sync] => GuzzleHttp\Handler\CurlHandler Object
(
[factory:GuzzleHttp\Handler\CurlHandler:private] => GuzzleHttp\Handler\CurlFactory Object
(
[handles:GuzzleHttp\Handler\CurlFactory:private] => Array
(
)

[maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 3
)

)

)

[parameter] => Array
(
[$request] => <required>
    [$options] => <required>
        )

        )

        [streaming] => GuzzleHttp\Handler\StreamHandler Object
        (
        [lastHeaders:GuzzleHttp\Handler\StreamHandler:private] => Array
        (
        )

        )

        )

        [parameter] => Array
        (
        [$request] => <required>
            [$options] => <required>
                )

                )

                [ GuzzleHttp\HandlerStack stack] => Array
                (
                [0] => Array
                (
                [0] => Closure Object
                (
                [static] => Array
                (
                [bodySummarizer] =>
                )

                [parameter] => Array
                (
                [$handler] => <required>
                    )

                    )

                    [1] => http_errors
                    )

                    [1] => Array
                    (
                    [0] => Closure Object
                    (
                    [parameter] => Array
                    (
                    [$handler] => <required>
                        )

                        )

                        [1] => allow_redirects
                        )

                        [2] => Array
                        (
                        [0] => Closure Object
                        (
                        [parameter] => Array
                        (
                        [$handler] => <required>
                            )

                            )

                            [1] => cookies
                            )

                            [3] => Array
                            (
                            [0] => Closure Object
                            (
                            [parameter] => Array
                            (
                            [$handler] => <required>
                                )

                                )

                                [1] => prepare_body
                                )

                                )

                                [ GuzzleHttp\HandlerStack cached] =>
                                )

Any idea what is wrong? Local server misconfig ?

PHP 7.4.18 under kubuntu
"dusterio/lumen-passport": "^0.3.4",
"guzzlehttp/guzzle": "^7.3",
"laravel/lumen-framework": "^8.0",

    

Thanks in advance!


Solution

  • I don't have so experience in lumen, but I am using in environment develop this solution without Guzzle.

    public function login(Request $request){
        $email = $request->email;
        $password = $request->password;
        
        //Check if field is not empty
        if (empty($email) or empty($password)) {
            return response()->json(['status' => 'error', 'message' => 'You must fill all fields']);
        }       
        $user = User::where('email', '=', $email)->exists();
        if ($user === false) {
            return response()->json(['status' => 'error', 'message' => 'User doesnt exist']);
        }                               
        //$client = new \GuzzleHttp\Client();
        
        try{
            $tokenRequest = $request->create(           
                env('PASSPORT_LOGIN_ENDPOINT'),
                'POST'
            );
    
            $tokenRequest->request->add([
                "grant_type" => "password",
                "username" => $request->email,              
                "password" => $request->password,
                "client_id" => env('PASSPORT_CLIENT_ID'),
                "client_secret" => env('PASSPORT_CLIENT_SECRET'),
            ]);
    
            $response = app()->handle($tokenRequest);
            return $response;
        } catch (\Exception $e) {
            return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
        }
    }