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

id_token not an index in OAuth2.php

Open simoncast opened this issue 12 years ago • 15 comments

I keep seeing this error in my logs:

Undefined index: id_token in GoogleOauth2/src/Google/Auth/OAuth2.php on line 446

I've updated to the latest code but the errors are continuing.

simoncast avatar May 01 '14 10:05 simoncast

Odd. How are you authenticating - could you include a snippet of how you're using the library?

ianbarber avatar May 07 '14 17:05 ianbarber

I am presuming you are calling verifyIdToken somewhere, but the token is not present. I'll close this for now, but if you have test code that reproduces the issue let us know! I suspect it's probably a case of calling verifyIdtoken when users aren't signed in though.

ianbarber avatar May 14 '14 17:05 ianbarber

Sorry, I’ve not had a chance to investigate further and I’ve also not seen it happen recently. I’ll keep an eye on it.

On 14 May 2014 at 18:51:31, Ian Barber ([email protected]) wrote:

I am presuming you are calling verifyIdToken somewhere, but the token is not present. I'll close this for now, but if you have test code that reproduces the issue let us know! I suspect it's probably a case of calling verifyIdtoken when users aren't signed in though.

— Reply to this email directly or view it on GitHub.

simoncast avatar May 16 '14 07:05 simoncast

I'm having the same problem. I need to know if you have the solution to the problem yet. Thanks.

petersowah avatar Aug 07 '14 13:08 petersowah

@bonty233 - could you add some sample code that demonstrates the problem and reopen? thanks!

ianbarber avatar Aug 07 '14 14:08 ianbarber

@ianbarber My code is posted below;

<?php

use \Google_Client;

class GoogleController extends \BaseController {

    protected $client;

    public function __construct(Google_Client $googleClient = null)
    {
        $this->client = $googleClient;
        if($this->client)
        {
            $this->client->setApplicationName('APP_NAME');
            $this->client->setClientId('CLIENT_ID');
            $this->client->setClientSecret('CLIENT_SECRET');
            $this->client->setRedirectUri('REDIRECT_URI');
            $this->client->setScopes('email profile');
        }
    }

    public function isLoggedIn()
    {
        return Session::get('access_token');
    }

    public function getAuthUrl()
    {
        return Redirect::to($this->client->createAuthUrl());
    }

    public function checkRedirectCode()
    {
        if (Input::get('code')) {

            $this->client->authenticate(Input::get('code'));

            $this->setToken($this->client->getAccessToken());

            $payload = $this->getPayload();

            echo 'pre', print_r($payload), '</pre>';

            return true;
        }

        return false;
    }

    public function setToken($token)
    {
        Session::put('access_token',$token);

        $this->client->setAccessToken($token);
    }

    public function googleLoginCallback()
    {
    //  $code = Input::get('code');
        $this->setToken($this->client->getAccessToken());
        $payload = $this->getPayload();

    }

    public function getPayload()
    {
        $payload = $this->client->verifyIdToken()->getAttributes();

        return $payload;
    }

}

and that returns the error Undefined index:id_token. I'm using the laravel framework. Thanks so much.

petersowah avatar Aug 07 '14 15:08 petersowah

Yeah, that code looks wrong. googleLoginCallback looks like its trying to get the code, which implies the code hasn't been exchanged yet. Make sure that before you call the getPayload function there you have either exchanged a code, or loaded the accessToken from the session or similar.

ianbarber avatar Aug 07 '14 15:08 ianbarber

I found a solution to my problem. I called the checkRedirectCode() in googleLoginCallback()

petersowah avatar Aug 24 '14 19:08 petersowah

Having the same issue here. Auth has been working for months and then decided to pack up today with this error. Still investigating.

mikehayesuk avatar Sep 11 '14 19:09 mikehayesuk

So if I revoke access to the app from my account, then authorize again, the id_token is there and everything works fine. Second login onwards and the id_token is missing again, until I deauthorize and start again. I have stepped through (and actually ended up rewriting some of) both my code and the Google API code and cannot find any issues.

Docs (e.g. https://developers.google.com/api-client-library/php/guide/aaa_idtoken) suggest that the id_token should be there just as it always has been. I have a bunch of apps where Google auth is failing all of a sudden today due to this id_token being missing. Does anybody have any pointers? My setup is similar to the token_id example: https://github.com/google/google-api-php-client/blob/master/examples/idtoken.php

Edit: For now I am going to start making a call to https://www.googleapis.com/oauth2/v3/userinfo?access_token= (I'm not sure how this is done with the library or if it can be, if not I will just use curl). I would love to know why the id_token has gone missing today though as it has caused me and my users a lot of grief. The userinfo request should do for now though, at least they will be able to login.

Edit 2: Typically, just as I was about to apply the fix mentioned in my edit, everything is working as normal now and the id_token is being returned. Google issue? I was hesitant to believe it was a Google issue originally because 1) I could not find any kind of API status anywhere mentioning this and 2) Google... reliability problems?? Never!

Cheers

mikehayesuk avatar Sep 11 '14 20:09 mikehayesuk

BlueHayes: We are experiencing the same problem. It looks like Google may have changed things so your OAuth2 scope needs to contain openid email whereas before it worked without this. See https://developers.google.com/accounts/docs/OAuth2Login#sendauthrequest for more details of the scope you need to provide.

robinmessage avatar Sep 11 '14 21:09 robinmessage

@robinmessage That's nuts. It looks like you're correct. It says in another section (https://developers.google.com/accounts/docs/OAuth2Login#scope-param) "The scope value must begin with the string openid and then include profile or email or both.". Why would they change something like that without backwards compatability? Surely the PHP library should be handling prepending the "openid" part anyway.

Strangely everything has just started magically working again right now, not sure if it will stay this way though after having issues all day long so I'm still a bit on edge.

mikehayesuk avatar Sep 11 '14 21:09 mikehayesuk

Have confirmation via someone at Stackoverflow that there was a Google issue however it has now been resolved.

http://stackoverflow.com/questions/25790135/occasionally-missing-return-parameter-id-token http://stackoverflow.com/questions/25796344/missing-parameter-from-return-id-token

Now I have to try and catch up on the lost time which I spent messing around with this :dash:

mikehayesuk avatar Sep 11 '14 21:09 mikehayesuk

i had the same problem, but i resolved to by passing id_token with verifyIdToken() function. Below is the complete code for reference.

if (Session::has('access_token')){ $token = Session::get('access_token');

     $idToken = json_decode($token);

     $token_data = $client->verifyIdToken($idToken->id_token)->getAttributes();
     $user_id = $token_data['payload']['sub'];

     dd($user_id);

     $client->setAccessToken($token);

}

i'm using laravel framework.

mzmmlds avatar Jan 10 '15 06:01 mzmmlds

@mzmmlds Is this still working for you? The $client->verifyIdToken($idToken->id_token) statement for me is returning boolean false rather than an object. I'm guessing it isn't verifying for whatever reason, but https://jwt.io/ does decode the idToken correctly. Is there a way to decode it without verifying it?

Or maybe I need to find out why it is not verifying correctly? Edit: ha, correct secret but wrong client_id. I seemed to have only copied half the client ID into my app, it was was still authorising users, but failing on the idToken signature verification.

However, both $client->verifyIdToken($idToken->id_token) and $client->verifyIdToken() are returning the array of JWT details, not an object. The ->getAttributes() must be dropped completely. This is mentioned as a change to the API client here

judgej avatar Jun 19 '17 13:06 judgej