How to identify a Google OAuth2 user?

后端 未结 6 865
误落风尘
误落风尘 2020-11-27 10:48

I used Facebook login to identify users. When a new user comes, I store their userID in my database. Next time they come, I recognized their Facebook ID and I know which use

相关标签:
6条回答
  • 2020-11-27 11:15

    "Who is this?" is essentially a service; you have to request access to it as a scope and then make a request to the Google profile resource server to get the identity. See OAuth 2.0 for Login for the details.

    0 讨论(0)
  • 2020-11-27 11:17

    It should be mentioned, that the OpenID Connect API returns no id attribute anymore.

    It's now the sub attribute which serves as a unique user identification.

    See Google Dev OpenID Connect UserInfo

    0 讨论(0)
  • 2020-11-27 11:19

    I inserted this method into google-api-php-client/src/apiClient.php:

    public function getUserInfo() 
    {
        $req = new apiHttpRequest('https://www.googleapis.com/oauth2/v1/userinfo');
        // XXX error handling missing, this is just a rough draft
        $req = $this->auth->sign($req);
        $resp = $this->io->makeRequest($req)->getResponseBody();
        return json_decode($resp, 1);  
    }
    

    Now I can call:

    $client->setAccessToken($_SESSION[ 'token' ]);
    $userinfo = $client->getUserInfo();
    

    It returns an array like this (plus e-mail if that scope has been requested):

    Array
    (
        [id] => 1045636599999999999
        [name] => Tim Strehle
        [given_name] => Tim
        [family_name] => Strehle
        [locale] => de
    )
    

    The solution originated from this thread: https://groups.google.com/forum/#!msg/google-api-php-client/o1BRsQ9NvUQ/xa532MxegFIJ

    0 讨论(0)
  • 2020-11-27 11:22

    Java version

    OAuth2Sample.java

    0 讨论(0)
  • 2020-11-27 11:25

    Altough JWTs can be validated locally with the public key, (Google APIs Client Library downloads and caches they public keys automatically) checking the token on Google's side via the https://www.googleapis.com/oauth2/v1/tokeninfo endpoint is necessary to check if the access for the applicaton has been revoked since the creation of the token.

    0 讨论(0)
  • 2020-11-27 11:26

    As others have mentioned, you can send a GET to https://www.googleapis.com/oauth2/v3/userinfo, using the OAuth2 bearer token you just received, and you will get a response with some information about the user (id, name, etc.).

    It's also worth mentioning that Google implements OpenID Connect and that this user info endpoint is just one part of it.

    OpenID Connect is an authentication layer on top of OAuth2. When exchanging a authorization code at Google's token endpoint, you get an access token (the access_token parameter) as well as an OpenID Connect ID token (the id_token parameter).

    Both these tokens are JWT (JSON Web Token, http://tools.ietf.org/html/draft-ietf-oauth-json-web-token).

    If you decode them, you'll get some assertions, including the id of the user. If you link this ID to a user in your DB, you can immediately identify them without having to do an extra userinfo GET (saves time).

    As mentioned in the comments, these tokens are signed with Google's private key and you may want to verify the signature using Google's public key (https://www.googleapis.com/oauth2/v3/certs) to make sure they are authentic.

    You can see what's in a JWT by pasting it at https://jwt.io/ (scroll down for the JWT debugger). The assertions look something like:

    {
        "iss":"accounts.google.com",
        "id":"1625346125341653",
        "cid":"8932346534566-hoaf42fgdfgie1lm5nnl5675g7f167ovk8.apps.googleusercontent.com",
        "aud":"8932346534566-hoaf42fgdfgie1lm5nnl5675g7f167ovk8.apps.googleusercontent.com",
        "token_hash":"WQfLjdG1mDJHgJutmkjhKDCdA",
        "iat":1567923785,
        "exp":1350926995
    }
    

    There are also libraries for various programming languages to programatically decode JWTs.

    PS: to get an up to date list of URLs and features supported by Google's OpenID Connect provider you can check that URL: https://accounts.google.com/.well-known/openid-configuration.

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