How can I verify a Google authentication API access token?

前端 未结 10 488
长发绾君心
长发绾君心 2020-11-30 17:19

How can I verify a Google authentication access token?

I need to somehow query Google and ask: Is [given access token] valid for the [exampl

相关标签:
10条回答
  • 2020-11-30 17:49
    function authenticate_google_OAuthtoken($user_id)
    {
        $access_token   = google_get_user_token($user_id); // get existing token from DB
        $redirecturl    = $Google_Permissions->redirecturl;
        $client_id      = $Google_Permissions->client_id;
        $client_secret  = $Google_Permissions->client_secret;
        $redirect_uri   = $Google_Permissions->redirect_uri;
        $max_results    = $Google_Permissions->max_results;
    
        $url = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.$access_token;
        $response_contacts  =  curl_get_responce_contents($url);
        $response   =   (json_decode($response_contacts));
    
        if(isset($response->issued_to))
        {
            return true;
        }
        else if(isset($response->error))
        {
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 17:52

    Google oauth code flow response in addition to access_token also returns id_token that contains useful for validation info in encrypted form.

    One thing that makes ID tokens useful is that fact that you can pass them around different components of your app. These components can use an ID token as a lightweight authentication mechanism authenticating the app and the user. But before you can use the information in the ID token or rely on it as an assertion that the user has authenticated, you must validate it.

    Validation of an ID token requires several steps:

    • Verify that the ID token is a JWT which is properly signed with an appropriate Google public key.
    • Verify that the value of aud in the ID token is equal to your app’s client ID.
    • Verify that the value of iss in the ID token is equal to accounts.google.com or https://accounts.google.com.
    • Verify that the expiry time (exp) of the ID token has not passed.
    • If you passed a hd parameter in the request, verify that the ID token has a hd claim that matches your Google Apps hosted domain.

    https://developers.google.com/identity/protocols/OpenIDConnect#validatinganidtoken link has code samples for validation of ID tokens.

    See also https://security.stackexchange.com/questions/37818/why-use-openid-connect-instead-of-plain-oauth.

    0 讨论(0)
  • 2020-11-30 17:55

    Ok, most answers are valid but not quite right. The idea of JWT is that you can validate the token without the need to contact the issuer everytime. You must check the id and verify the signature of the token with the known public key of the certificate google used to sign the token.

    See the next post why and how to do this.

    http://ncona.com/2015/02/consuming-a-google-id-token-from-a-server/

    0 讨论(0)
  • 2020-11-30 17:55

    I need to somehow query Google and ask: Is this access token valid for example@example.com?

    No. All you need is request standard login with Federated Login for Google Account Users from your API domain. And only after that you could compare "persistent user ID" with one you have from 'public interface'.

    The value of realm is used on the Google Federated Login page to identify the requesting site to the user. It is also used to determine the value of the persistent user ID returned by Google.

    So you need be from same domain as 'public interface'.

    And do not forget that user needs to be sure that your API could be trusted ;) So Google will ask user if it allows you to check for his identity.

    0 讨论(0)
  • 2020-11-30 17:57

    For user check, just post get the access token as accessToken and post it and get the response

    https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=accessToken
    

    you can try in address bar in browsers too, use httppost and response in java also

    response will be like

    {
         "issued_to": "xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
         "audience": "xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
         "user_id": "xxxxxxxxxxxxxxxxxxxxxxx",
         "scope": "https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com",
         "expires_in": 3340,
         "access_type": "offline"
        }
    

    The scope is the given permission of the accessToken. you can check the scope ids in this link

    Update: New API post as below

    https://oauth2.googleapis.com/tokeninfo?id_token=XYZ123
    

    Response will be as

     {
     // These six fields are included in all Google ID Tokens.
     "iss": "https://accounts.google.com",
     "sub": "110169484474386276334",
     "azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
     "aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
     "iat": "1433978353",
     "exp": "1433981953",
    
     // These seven fields are only included when the user has granted the "profile" and
     // "email" OAuth scopes to the application.
     "email": "testuser@gmail.com",
     "email_verified": "true",
     "name" : "Test User",
     "picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",
     "given_name": "Test",
     "family_name": "User",
     "locale": "en"
    }
    

    For more info, https://developers.google.com/identity/sign-in/android/backend-auth

    0 讨论(0)
  • 2020-11-30 18:01
    1. As per Google's documentation, you should use Google's AP Client Library that makes this (token verification, claim extraction etc.) much easier than writing your own custom code.

    2. From a performance perspective, the token should be parsed locally without making a call to Google again. Off-course Google's public key is needed and retrieval of that key is done using a caching strategy, implemented in the Google's client library from #1 above.

    3. FYI only. Google also uses a JWT token. See image below for reference.

    0 讨论(0)
提交回复
热议问题