Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chatbot: how to validate incoming requests from google hangouts

We've put together a google hangouts chat bot to provide some convenient functionality for our team. The bot is of the 'bot URL' variety, meaning that hangouts sends requests to an app endpoint and our app responds appropriately. At the moment, we're struggling to now validate the incoming requests from google. Each request has a bearer token in the Authentication header, but that JWT token does not validate. Both the php client library [https://github.com/googleapis/google-api-php-client] and the online validator [https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=] return the error 'invalid signature'

The google client php library's Google_AccessToken_Verify class has a verifyIdToken method which we use as described here in this example [https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/auth/src/auth_cloud_explicit.php]. We pass the path of our service account key file and the project ID into the google client constructor. Then we pass the incoming request's bearer token into the verifyIdToken method.

use Google_Client;

// inside a laravel controller with $request in scope

$bearer_token = $request->bearerToken();
$keyPath = FILE_LOCATION

$client = new Google_Client([
               'keyFilePath' => $keyPath,
               'projectId' => GCP_CLIENT_ID
           ]);

$payload = $client->verifyIdToken($bearer_token);

if(!empty($payload)){

    return $this->call(ParseGoogleChatRequest::class, [$request]);

}else{

     \Log::debug('bad token');

}

I expect the google client library to be able to validate a google JWT. This github issue [https://github.com/firebase/php-jwt/issues/175] reflects our experience implementing this approach. I would like to get some general guidance on which approach we should be using.

like image 356
mrwingfield Avatar asked Nov 06 '22 16:11

mrwingfield


1 Answers

I figured out an acceptable solution with the help of another SO question. The google client library was already importing firebase/php-jwt, so I followed along the same lines as Jed from the question I linked to. Extracting the KID from the token, I used it to identify the correct public key from this url. Then I instantiated the php-jwt library and called the decode method on it, passing the required arguments. The decode method also verifies the signature and returns the components of the JWT on success.

like image 159
mrwingfield Avatar answered Dec 16 '22 02:12

mrwingfield