Search code examples
phplaravelguzzlelaravel-passport

php process goes unresponsive when making a HTTP POST method call using GuzzleHttp


I am using guzzle HTTP client to issue a password grant based access token after successful user login. I am using passport package for oauth and I have done all the setup which includes the Password Grant Client it creates. In my login controller I override the sendLoginResponse method of the AuthenticatesUsers trait so as to issue an access token on successful email/password authentication

public function sendLoginResponse(Request $request)
{

    try {
        Log::debug("Auth attempt sucessful, obtaining access_token for user :: ".$request->email);

        $client = new Client();

        $token_response = $client->post(config('app.url').'/oauth/token', [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => config('auth.password_grant.client.id'),
                'client_secret' => config('auth.password_grant.client.secret'),
                'username' => $request->email,
                'password' => $request->password,
                'scope' => '*',
            ],
        ]);

        if($token_response->getStatusCode()!=200) {
            Log:error("Login failed to generate Access Token");
            throw new InvalidCredentialsException();
        }
        $request->session()->regenerate();
        $this->clearLoginAttempts($request);
        $data = json_decode((string) $token_response->getBody(), true);
        Cookie::queue('refresh_token',$data->refresh_token,config('auth.tokens.refresh.expire.days')*1440);

        Log::debug("Adding Bearer token to Authorization header");
        return response()->view('dashboard', [
            'expires_in' => $data->expires_in
        ], 200)->header('Authorization', $data->token_type.' '.$data->access_token);            
    } catch(Exception $e){
        Log::error('Error :: '.$e->getMessage());
        throw $e;
    }
}

The whole PHP process goes unresponsive when I make this post request, and there are no errors in any of the logs. Exactly at this line

$token_response = $client->post($token_url, .......

I ran this in Debug session; and the URL, Client ID and Secret are generated correctly via configuration properties; the only Exception I could see was a FileNoFoundException that occurs when it does find any cache key for throttle login and that all happens much before this call is made and the app proceeds to authenticate the user.

When I make this very request with same parameters through Postman or via artisan tinker I can get a response with access_token, refresh_token and expires_in data.


Solution

  • A couple of hours with 'Hit And Trial' does really save you 10 minutes of going through 'Documentation'.

    Turns out I really don't have to do all this heavy lifting this link shows how we can add the \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class, to web middleware in app/http/Kernel.php which takes care of generating ApiToken for first party apps such as the React JS which I'd be using to consume my own API.

    While that solved the intent of writing all this code, I'm still not sure what was causing the process unresponsiveness making the access_token from within the php code.