React-native-apple-authentication: Invalid Authorization Code from Auth0 integration

Created on 17 Jul 2020  Â·  8Comments  Â·  Source: invertase/react-native-apple-authentication

Hello,
I am trying to implement sign in with apple. I am using the package as documented, however when i pass the authorization code to auth0 so that i can exchange with a token, it says that my authorization code is invalid. Is there any special decoding i need to do or something else?

question waiting for feedback

Most helpful comment

@pantelispanayiotou
When adding Auth0 sign in for native app you need to use different request parameters to /oauth/token, including grant_type.

It will be something like this:

{
    "client_id": "AUTH0_CLIENT_ID",
    "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
    "subject_token_type": "http://auth0.com/oauth/token-type/apple-authz-code",
    "subject_token": "APPLE authorizationCode"
}

You can find all the information in this section of Auth0 docs. Also, make sure you did all the steps from this article.

And in the end, in order it to work, you need to add nonceEnabled: false to appleAuth.performRequest() config. Otherwise, you will have another issue.

All 8 comments

Hello!
If you had a subordinate come to you with that question, what would you need to know to help?
I'd need things like auth0 API documentation, versions of everything, exact error message and the exact API called that returned the error, I'd want to know what you tried already, and ideally you'd have a quick reproduction to demonstrate the problem.

My guess is that they might have something needed like the nonce that we carry between this module and react-native-firebase, you can see it if you look at the related react-native-firebase change https://github.com/invertase/react-native-firebase/pull/2979/files

@mikehardy you are right. I am using the authorizationCode that is returned from the request. To exchange tokens with auth0 i had to pass the authorizationCode to another auth0 request which is documented here: https://auth0.com/docs/api/authentication#authorization-code-flow45

I followed the steps on the configuration of Apple Sign in and used the settings to my auth0 application. Auth0 says that the connection is okay but when i do the request on auth0 for an exchange it always returns with the two following errors:

When i use the headers it returns a 401 error access denied, and when i don't it returns a 403 error: invalid grant, invalid authorization code.

`try {
 // performs login request
  const appleAuthRequestResponse = await appleAuth.performRequest({
    requestedOperation: AppleAuthRequestOperation.LOGIN,
    requestedScopes: [
      AppleAuthRequestScope.EMAIL,
      AppleAuthRequestScope.FULL_NAME,
    ],
  })


  console.log(appleAuthRequestResponse);
  const data = {
    "grant_type":"authorization_code",
    "client_id":"AUTH0_CLIENT_ID",
    "client_secret":"AUTH0_CLIENT_SECRET",
    "code": appleAuthRequestResponse.authorizationCode,
    "redirect_uri":"https://www.example.com/callback"
   }

  const config = {
            //headers: { ‘content-type’: ‘application/x-www-form-urlencoded’ },
             method:"post",
             url: "https://example.eu.auth0.com/oauth/token",
             data
         };
   axios.request(config)
     .then(res=>{

       console.log("RES", res);
})
}
catch (err) {
console.log(err);

}`

I wish I knew more, but this looks like an integration issue - it's "between the packages" yes, but as near as I can tell there are no active issues with the way this module does the apple auth. I understand it is not the easiest to then integrate it with another provider (Auth0 perhaps, firebase was also a little touchy when I did it) but it's not anything wrong with this module. It is just that integrating things is hard.

Perhaps someone that has succeeded with Auth0 can comment.

Wish i could find someone who did this implementation. Auth0 is not responsive at all..

@pantelispanayiotou
When adding Auth0 sign in for native app you need to use different request parameters to /oauth/token, including grant_type.

It will be something like this:

{
    "client_id": "AUTH0_CLIENT_ID",
    "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
    "subject_token_type": "http://auth0.com/oauth/token-type/apple-authz-code",
    "subject_token": "APPLE authorizationCode"
}

You can find all the information in this section of Auth0 docs. Also, make sure you did all the steps from this article.

And in the end, in order it to work, you need to add nonceEnabled: false to appleAuth.performRequest() config. Otherwise, you will have another issue.

@xineman this is fantastic info! If there is anything to add to the README (maybe an "auth0 integration" block?) you can just hit the edit button on the README.md file and the github web UI makes the PR process trivial

@xineman great! one thing that i also missed out was that i had to stringify the above parameters while using axios for my request. Also, if you want to implement facebook login with auth0 you have to use http://auth0.com/oauth/token-type/facebook-info-session-access-token as the subject_token_type

Hi guys, I'm trying to validate authorizationCode from apple (as provided by our client mobile app ) via server api call to auth0 using the above parameters and I'm getting this issue [another issue] (https://community.auth0.com/t/error-from-apple-connection-no-description-undefined/44978). could you please help ?
Following are request params and end point to call auth0 api from a nestjs server app

const oAuthTokenURL = this.authenticator.fullUrl + '/oauth/token'
const requestParams = {
        grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',
        subject_token_type: 'http://auth0.com/oauth/token-type/apple-authz-code',
        audience: this.authenticator.audience,
        scope: 'openid profile offline_access',
        subject_token: appleAuthDto.appleAuthorizationCode,
        client_id: this.authenticator.authentication.id,
        user_profile: JSON.stringify({
          "name": {
            "firstName": "xxxx",
            "lastName": "xxxx"
          },
          "email": "[email protected]"
        })
      }
Was this page helpful?
0 / 5 - 0 ratings