parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

for google auth, the access_token is not being recognised. It only recognises id_token?

Open elimau opened this issue 3 years ago • 9 comments

New Issue Checklist

Issue Description

I perform the Parse.user's linkWith request await user.linkWith('google', payload) where the payload is const payload = { authData: { id, access_token } }

It fails with id token is invalid for this user error message

From this documentation, it seems access_token is supported for google. https://docs.parseplatform.org/parse-server/guide/#google-authdata Also, reading this issue, it seems access_token is supported. https://github.com/parse-community/parse-server/issues/4698

Yet, when i read the code of the google auth adapter, it seems it only supports id_token. There is no reference to access_token in that file. https://github.com/parse-community/parse-server/blob/1594afec6421bea31e732dff9b21eb099898bc3a/src/Adapters/Auth/google.js#L64

Was it something that used to be supported but no longer supported? Am I reading something wrong?

Environment

Server

  • Parse Server version: 4.5.0
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): back4app

elimau avatar Jul 22 '21 13:07 elimau

Thanks for reporting. Is this related to https://github.com/parse-community/parse-server/issues/6849?

mtrezza avatar Jul 22 '21 16:07 mtrezza

Hi, yes, it is the same problem. But i'd like to further explain the problem I am experiencing.

My two constraints are:

  • Parse.User.linkWith('google', payload) requires id_token and id to be called successfully
  • I can get either the access_token or the id_token from google oauth process. However for some reason (even if I put responseType="token id_token" when the oauth authentication happens), I am unable to get both. Hence I have a choice here to get one or the other.

So there are two paths of progress.

  1. If I get access_token:
  • I have to create a backend parse function endpoint which queries google to get the id of the user.
  • i.e.
const oauth2 = google.oauth2('v2')
const userInfo = await oauth2.userinfo.get({}). <-- This userinfo contains `id`

Now i have the id but I do not have id_token so I can not use linkWith('google', payload) successfully.

  1. If I get id_token:
  • How can I get id?
  • Because I do not have id, I still can not use linkWith('google', payload) successfully.

firebase When I look at the firebase implementation, I do not have to supply id to login. e.g.

import firebase from 'firebase'
const oAuthCredential = firebase.auth.GoogleAuthProvider.credential(id_token, access_token)   <-- one or the other is required. Both works.
const userCredential = await firebase.auth().signInWithCredential(oAuthCredential). // success.

So there is a few resulting questions:

  • Is it possible to make linkWith('google', payload), not require the id field (like how firebase works)?
  • Is there a way I can get id using the id_token from google?
  • Is it possible to make linkWith('google', payload) work with access_token and also it does not require id?

Thanks.

elimau avatar Jul 23 '21 02:07 elimau

@SebC99 Do you have any idea how we could address this, and maybe close it together with https://github.com/parse-community/parse-server/issues/6849?

mtrezza avatar Jul 23 '21 11:07 mtrezza

I don't know anything about using google signing on the web, but a quick look at the docs it is said:

if (auth2.isSignedIn.get()) {
  var profile = auth2.currentUser.get().getBasicProfile();
  console.log('ID: ' + profile.getId());
  console.log('Full Name: ' + profile.getName());
  console.log('Given Name: ' + profile.getGivenName());
  console.log('Family Name: ' + profile.getFamilyName());
  console.log('Image URL: ' + profile.getImageUrl());
  console.log('Email: ' + profile.getEmail());
}

So I guess the user id is in its profile.

But otherwise, it's quite easy to remove the server side check on that id, as we only need to remove these lines:

  if (jwtClaims.sub !== id) {
    throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, `auth data is invalid for this user.`);
  }

or at least add a case where the id is undefined.

I still have no clue on why #6849 isn't passing the tests, but otherwise I'm happy to include this in the PR

SebC99 avatar Jul 23 '21 12:07 SebC99

@SebC99 Look here: https://auth0.com/docs/tokens/id-tokens/validate-id-tokens It tells what are the important things to check for in the id_token.

It looks like the google adapter is doing the important checks against the JWT for alg, iss and aud.

So I also agree that the id is not necessary. Sounds good to me!

Regarding your comment about the tests not passing, I could not see any pull requests against that #6849 . Do you have a link to the test you mention that is failing?

elimau avatar Jul 26 '21 06:07 elimau

@elimau sorry the PR was #6992

SebC99 avatar Jul 26 '21 10:07 SebC99

Seeing the same issue, had old code that passes a google access_token to linkWith in 4.2.0, now on 4.10.3 and the google access_token is not being accepted by linkWith. switching to the id_token works fine.

andrewalc avatar Sep 20 '21 19:09 andrewalc

Change de Auth data, not to { id, access_token } try with { id, id_token }, works for me!!!!

PavelBT avatar Feb 19 '23 23:02 PavelBT

The user gets created fine with {id, id_token}, it doesn't with access_token However I don't get any sessionToken using linkWith or loginWith I even tried with Postman, so it's not a client issue, no sessionToken gets sent so I can't log in to the User that was just created... Anyone got the same problem?

R3D347HR4Y avatar Dec 28 '23 22:12 R3D347HR4Y