Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supabase signInWithIdToken not working: Nonces mismatch #1176

Open
saadHeisentech opened this issue Jul 5, 2023 · 23 comments
Open

supabase signInWithIdToken not working: Nonces mismatch #1176

saadHeisentech opened this issue Jul 5, 2023 · 23 comments

Comments

@saadHeisentech
Copy link

saadHeisentech commented Jul 5, 2023

// using this library for decoding
import jwtDecode from 'jwt-decode'

const handleLogin = async () => {
    setLoading(true);
    try {
      await GoogleSignin.hasPlayServices();
      const u = await GoogleSignin.signIn();
      // get the nonce value
      const nonce = jwtDecode<any>(u.idToken!);
      const { nonce: nonceVal } = nonce!;
      // login to supabase
      const { data, error } = await supabase.auth.signInWithIdToken({
        provider: 'google',
        token: u.idToken!,
        nonce: nonceVal,
      });
      console.log('supabase data:', data);
      if (error) {
        console.log('Supabase error:', error);
      }
      if (data.user) {
        // do something
      }
    } catch (err) {
      console.log(err);
    } finally {
      setLoading(false);
    }
  };
@jason-dubon
Copy link

Did you try to also pass the access token in the signInWithIdToken function?

@He1nr1chK
Copy link

Did you try to also pass the access token in the signInWithIdToken function?

Passing access_token along with id_token does not solve the error: [AuthApiError: Passed nonce and nonce in id_token should either both exist or not.]

@sunnyysetia
Copy link

+1 having the same issue :(

@furkand
Copy link

furkand commented Jul 14, 2023

is there any solution I am having exactly same issue

@timdobranski
Copy link

timdobranski commented Jul 18, 2023

I've been blocked on this for a while now. My code is nearly identical to the OP. I'm getting the nonce from jwt-decode, but passing it in the signInWithIdToken() call only changes the error from 'Passed nonce and nonce in id_token should either both exist or not' to nonces mismatch. Anyone have any suggestions or solutions?

It seems like the signInWithIdToken() isn't behaving as expected, or I'm missing something.

@Eirmas
Copy link

Eirmas commented Jul 20, 2023

To my understanding Supabase expects the nonce to be the decoded version of the nonce present in the ID token received from google, hence why passing the nonce from the ID token doesnt work as it will get its hash and match them for equality.
Unfortunately I cannot find any way to get the nonce before its encoded in the ID token. Any ideas?

/** If the ID token contains a `nonce` claim, then the hash of this value is compared to the value in the ID token. */
nonce?: string

@timdobranski
Copy link

Hate to say it, but it doesn't look like this will work currently. Supabase requires a nonce if it's in the token, and react native doesn't expose the nonce in its current implementation. Unless I've got something wrong, native signin on iOS with supabase and react-native isn't possible at this time. Either supabase has to modify the endpoint (unlikely) or react-native has to expose the nonce (unlikely any time soon, I assume).

@vonovak
Copy link
Member

vonovak commented Aug 11, 2023

Hello and thanks for reporting,
I did not test this, and it may not work, but the next version of the google sign in library obtainable for sponsors here allows passing custom nonce. Maybe that will work.
edit: it only allows that on Android

edit2: I tested this and it works, can be used like this:

 const nonce = Crypto.randomUUID()
 const hashedNonce = await digestStringAsync(CryptoDigestAlgorithm.SHA256, nonce)
 const userInfo = await GoogleOneTapSignIn.signIn({
      webClientId,
      nonce: hashedNonce,
    })

Thank you 🙂

@vonovak
Copy link
Member

vonovak commented Sep 22, 2023

quick update: passing custom nonce works as outlined in the post above, for Android.

For iOS, I confirm that a valid idToken for supabase can be obtained using https://docs.expo.dev/versions/latest/sdk/auth-session/ (edit: no need to use any of the deprecated APIs)

edit2: please note rn-google sign in does not expose nonce option because the underlying native sdk doesn't expose it either

related issue: supabase/auth#1205 (comment)

@vonovak vonovak changed the title How do I get the nonce value out of the idToken from GoogleSignin, I am trying to pass it into supabase signInWithIdToken but it return this error. Supabase error: [AuthApiError: Nonces mismatch]. That's my code below supabase signInWithIdToken not working: Nonces mismatch Sep 22, 2023
@sonipranjal
Copy link

Facing the same issue AuthApiError: Passed nonce and nonce in id_token should either both exist or not.

Any workaround?

@sonipranjal
Copy link

sonipranjal commented Sep 24, 2023

Expo auth session for Google auth is deprecated: https://docs.expo.dev/versions/latest/sdk/auth-session/#googleauthrequestconfig

@sonipranjal
Copy link

Hi everyone, I got it working by using the browser

Made a gist for everyone who is struggling to solve this:

https://gist.github.com/sonipranjal/f4a66f35924ede2e2f4a8d5b66199857

@codypl
Copy link

codypl commented Sep 26, 2023

I am having the same problem, it seems like @react-native-google-signin/google-signin and supabase are not compatible because there is currently no way to get the nonce value expected by supabase signInWithIdToken method

@alexandreandriesse
Copy link

Same problem here, the library is useless if we can't get, or pass, the nonce value...

@erdemgonul
Copy link

same problem here...

@vonovak
Copy link
Member

vonovak commented Oct 3, 2023

hello everyone, I'm going to lock this discussion because there's nothing new that can be added to it at this point.

To summarize (basically repeating this comment): to provide custom nonce:

on Android, you need to use one-tap sign in which is available for sponsors.
on iOS, it can be implemented using expo's authSession (it's not trivial, but can be done - you can hire me if you're unsure how to do it), or using the workaround posted above.

Thank you! 🙂

@vonovak
Copy link
Member

vonovak commented Feb 21, 2024

Hello, if anyone's interested in how to do this properly, including custom nonce, with expo-auth-session, the code is here. However, please note it's not publicly available, but you can get access to it if you sponsor this project, as described in the docs.

My PR to allow custom nonce in AppAuth is merged, but now we need to wait for the work to be released (by AppAuth maintainers) and integrated into the Google Sign In SDK (I'll do that part). openid/AppAuth-iOS#788

@davidivad96
Copy link

There's an option in the Google provider configuration in Supabase that allows to skip these nonce checks:

Screenshot 2024-02-24 at 18 14 18

This is indeed useful for this specific situation where we don't have access to the nonce used (iOS app)

@bjollans
Copy link

bjollans commented Feb 25, 2024

I got this working for myself using react-native-app-auth. See this gist for a minimal working solution.

Maintainer edit: please note this approach skips the nonce. Not using nonce is not recommended for security reasons.

@danielkv
Copy link

There's an option in the Google provider configuration in Supabase that allows to skip these nonce checks:

Screenshot 2024-02-24 at 18 14 18

This is indeed useful for this specific situation where we don't have access to the nonce used (iOS app)

That works in production, but locally we don't have this option.

@abhishekmg
Copy link

Any solutions without skipping nonce checks ?

@jonassvalin
Copy link

@vonovak Hi, thanks for all the work 👋

It is my understanding that AppAuth-iOS v1.7.X has been released which includes the custom nonce fix: https://github.com/openid/AppAuth-iOS/releases

Is there anything blocking that from being integrated now or is it just a matter of time/prioritisation?

@vonovak
Copy link
Member

vonovak commented Apr 29, 2024

hi @jonassvalin,
You're welcome! It's the matter of getting this google/GoogleSignIn-iOS#402
reviewed (I expect I'll be asked to do some changes), merged and released. Then I'll be able to integrate it into this lib.

In the mean time, the best workaround is in #1176 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests