How do I validate a client-side google+ login?

Viewed 2077

So, following the basic tutorial here (https://developers.google.com/+/web/signin/javascript-flow) you can easily add a client-side login for google accounts.

By modifying the code to query /people/me, as follows:

function signinCallback(authResult) {
  if (authResult['status']['signed_in']) {
    gapi.auth.setToken(authResult);
    gapi.client.load('plus','v1', function(){
      var request = gapi.client.plus.people.get({
        'userId': 'me'
      });
      request.execute(function(resp) {
        console.log(resp);
      });
  });
}

You can gain access to the basic account information; user id, image, name, etc.

You also have a valid access token from the oauth login.

Now, unless you have an entirely client-side application, as some point you're going to have to notify the server that the user is logged in, to get application data for that user.

What I want to do is POST to /login/gauth {'access_token': ..., 'user_id': ....} which will respond with an auth cookie and redirect back; except now we have a persistent local auth token which we can use to identify the user.

On the server I need to do is access the same REST api (/people/me) using the access token, and validate the user id it returns; any other provided information is forge-able on the client side.

The problem is, I can't find any way of validating/using an access token on the server side.

I can setup a new OAuth login on the server, and do a server only login process, but that's fairly involved, and seems wasteful considering I already have a valid access token.

How do I use it?

3 Answers

You can skip making a network request to the tokeninfo endpoint by verifying the authenticity of the users JWT id_token on your server. This can easily be done by using one of Google's API Client Libraries.

This also allows you to avoid needed to request a offline access code if not needed.

This is the method recommended in Google's documentation.

Check there for more detailed instructions.

Related