google-api-php-client icon indicating copy to clipboard operation
google-api-php-client copied to clipboard

Google_Client::verifyIdToken return invalid token signature

Open jbduzan opened this issue 12 years ago • 18 comments

I'm trying to work with the php client of google Oauth2 api to validate an id_token. The id_token is provided by a javascript app on which the user login his google account, and i'm givin this token to my php api server, in order to validate it and retrieve the right informations from my bdd.

The login in javascript is successfull and the access and id_token seems right, but when I try to verify it with Google_Client->verifyIdToken()

$client = new Google_Client(); $ticket = $client->verifyIdToken(myToken); , it return an invalid token signature exception (fail when trying to use the google cert to valid the token).

My first assumption was to think the token is not valid, so to make sur it is, i submited it to google validation url https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=token

it return user info, so my token seems to be valid :

{ issuer: "accounts.google.com", issued_to: "id", audience: "id", user_id: "id", expires_in: 2149, issued_at: 1397636222, email: "mail", verified_email: true } Tried everything i can think of, but can't solve this one. can someone help me ?

Question asked on stackoverflow too : http://stackoverflow.com/questions/23104721/google-oauth2-verifyidtoken-return-invalid-token-signature-with-valid-id-token

jbduzan avatar Apr 17 '14 07:04 jbduzan

Odd. Can you step through the validate function to see which part it is failing on?

ianbarber avatar Apr 24 '14 13:04 ianbarber

it fail inside OAuth2:verifySignedJwtWithCerts. Line 500 of OAuth 2.php

// Check signature $verified = false; foreach ($certs as $keyName => $pem) { $public_key = new Google_Verifier_Pem($pem); if ($public_key->verify($signed, $signature)) { $verified = true; break; } }

jbduzan avatar Apr 24 '14 13:04 jbduzan

Could you see where in the verify function it is hitting an issue? There are multiple checks in there, and might clarify things to see what it is failing on.

ianbarber avatar Apr 24 '14 14:04 ianbarber

Seems to happen when Pem::verify call openssl_verify, it always return 0 (not -1, never get the exception thrown)

jbduzan avatar Apr 24 '14 14:04 jbduzan

Can you check the date and time on the machine is right? I'm wondering if there is a bad cached cert or similar.

ianbarber avatar Apr 24 '14 14:04 ianbarber

seems to be good, is there a way to flush this cache ?

jbduzan avatar Apr 24 '14 14:04 jbduzan

Just delete it - it should be in your system temp file directory.

ianbarber avatar Apr 24 '14 14:04 ianbarber

deleted the google_client folder from temp directory, error still here

jbduzan avatar Apr 24 '14 14:04 jbduzan

Hmm, just to check you are using an up to date id token for your testing? The Google certificates are rotated every few hours, so an old id token wont be valid any more.

ianbarber avatar Apr 24 '14 15:04 ianbarber

i just did a new authentification. The token look like the same than last week for me. But https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=token return valid informations

jbduzan avatar Apr 24 '14 16:04 jbduzan

Hmm, the token really should not look (exactly) the same week to week - there is a timestamp in there for one. How are you retrieving the id token?

ianbarber avatar Apr 30 '14 15:04 ianbarber

retrieving it from the callback function of my signin button in javascript. It's a property of my access token

jbduzan avatar Apr 30 '14 15:04 jbduzan

Only thing I can guess is that its an incompatibility with the versions of openssl on the machine. Do you know which version of libopenssl or similar you have?

ianbarber avatar May 07 '14 17:05 ianbarber

seems to have a really old version : OpenSSL 0.9.8q 2 Dec 2010

jbduzan avatar May 15 '14 09:05 jbduzan

I am guessing that is the issue. Not totally sure what might be incompatible, but I think moving to a newer openssl should solve it.

ianbarber avatar Jul 01 '14 17:07 ianbarber

It's timezone fault! in my case it's work(day), not work(night after 12AM), work again(tomorrow)! when it's stop working, just set timezone to America/New_York and its work, remove timezone then stop working. And its happened because of some country have future date window rather than usA. you know that if you change your system date time then SSL stop working. I think it's the same problem. google sign it for it's own today but actually your yesterday!

php 7.1 macOS Sierra Timezone 3:30

ZeroNilZero avatar Aug 11 '17 23:08 ZeroNilZero

@NexProME Are you sure ? i just used both date_default_timezone_set('America/Los_Angeles'); and date_default_timezone_set('America/New_York'); and it's still not working. However, i can confirm that it does work during the day and stops working as darkness conquers the sky.

doubleOrt avatar Sep 01 '17 22:09 doubleOrt

@ianbarber Moving to OpenSSL/1.0.2l (this is the version of OpenSSL that is printed out by phpinfo()) didn't fix the issue. What should i do now ?

doubleOrt avatar Sep 01 '17 23:09 doubleOrt