Search code examples
phpgoogle-oauthjwt

Decode JWT encoded string from Google in PHP


I am upgrading my Google based login system, which requires me to decode id_token strings supplied by Google. The strings are valid, and I can decode them via: https://developers.google.com/wallet/digital/docs/jwtdecoder

But I want my server to do this on the fly in PHP.

I found both: https://github.com/firebase/php-jwt/tree/master and https://github.com/luciferous/jwt

But I cannot figure out how to work with PEAR packages. Simple PHP scripts to copy to my server I can do, but I find the documentation given with these 2 packages very limited. Does anybody have sample code on how to decode such a string?

Any help would be much appreciated.


Solution

  • If I understood you correctly, you're trying to decode JWT's with a PHP script? If so maybe the following PHP code (taken from https://developers.google.com/wallet/instant-buy/about-jwts#jwt_decoding) can help you using the script from luciferous.

    $mwr = array(
      'iat' => $now,
      'exp' => $now + 3600,
      'typ' => 'google/wallet/online/masked/v2/request',
      'aud' => 'Google',
      'iss' => MERCHANT_ID,
      'request'=> array(
        'clientId' =>  CLIENT_ID,
        'merchantName'=> MERCHANT_NAME,
        'origin'=> ORIGIN,
         'pay'=> array (
           'estimatedTotalPrice'=> $estimated_total_price,
           'currencyCode'=> $input['currencyCode'],
          ),
          'ship'=> new stdClass(),
      ),
    );
    if (isset($input['googleTransactionId'])) {
      $mwr['request']['googleTransactionId'] = $input['googleTransactionId'];
    }
    WalletUtil::encode_send_jwt($mwr);