Skip to content

v3 - CALLBACK_OAUTH_ERROR (Invalid state...) - Custom Provider Azure AD B2C #468

@BenjaminWFox-Lumedic

Description

@BenjaminWFox-Lumedic

Have moved my initial v2 implementation to v3. It was very easy! Everything works as far as I can tell except for the state parameter.

Describe the bug
Using custom provider Azure AD B2C next-auth gives an error (see below) in the callback after successful authentication with Azure AD B2C. This seems to happen whether or not I set useState: false.

This is a new after moving to v3. The same config had worked in v2.

B2C supports the state parameter. I don't think this is an issue with the authorization server, but I could be mistaken. From the B2C link, state is supported as:

A value included in the request that's also returned in the token response. It can be a string of any content that you want. A randomly generated unique value is typically used for preventing cross-site request forgery attacks. The state is also used to encode information about the user's state in the application before the authentication request occurred, such as the page they were on.

I've confirmed (see below) that the state provided by the authorization request is the same as the state returned from the authorization server.

To Reproduce
If this is not something obvious I might have missed please let me know, and I will set up a minimal reproduction.

Expected behavior
I would expect that if the state provided initially by the client & sent back by the authorization server are the same than I should not get an error.

Screenshots or error logs
The B2C Custom Provider looks like:

    {
      id: 'azureb2c',
      name: 'Azure B2C',
      type: 'oauth',
      version: '2.0',
      debug: true,
      scope: 'offline_access openid',
      // params: {
      //   grant_type: 'authorization_code',
      // },
      accessTokenUrl: `https://${tenantName}.b2clogin.com/${tenantName}.onmicrosoft.com/${userFlow}/oauth2/v2.0/token`,
      // requestTokenUrl: 'https://login.microsoftonline.com/${process.env.DIRECTORY_ID}/oauth2/v2.0/token',
      authorizationUrl: `https://${tenantName}.b2clogin.com/${tenantName}.onmicrosoft.com/${userFlow}/oauth2/v2.0/authorize?response_type=code+id_token&response_mode=form_post`,
      profileUrl: 'https://graph.microsoft.com/oidc/userinfo',
      profile: (profile) => {
        console.log('THE PROFILE', profile)

        return {
          id: profile.oid,
          fName: profile.given_name,
          lName: profile.surname,
          email: profile.emails.length ? profile.emails[0] : null,
        }
      },
      clientId: process.env.AUTH_CLIENT_ID,
      clientSecret: process.env.AUTH_CLIENT_SECRET,
      idToken: true,
      // useState: false,
    },

The B2C authorize url looks like:

https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/{flow}/oauth2/v2.0/authorize
?response_type=code+id_token
&response_mode=form_post
&redirect_uri=https%3A%2F%2Fp.rizon.top%3A443%2Fhttp%2Flocalhost%3A3000%2Fapi%2Fauth%2Fcallback%2Fazureb2c
&scope=offline_access%20openid
&state=45e76b516360e8aa4e79d44661344ba06f8ed0fc8a08beb3362bcbe7cde2fe90
&client_id=<client_id>

The Form Data response includes (along with the code & id_token):

state: 45e76b516360e8aa4e79d44661344ba06f8ed0fc8a08beb3362bcbe7cde2fe90

The next-auth error looks like:

[next-auth][error][callback_oauth_error] Error: Invalid state returned from oAuth provider
    at <irrelevant-project-path>/node_modules/next-auth/dist/server/lib/oauth/callback.js:46:27
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (<irrelevant-project-path>/node_modules/next-auth/dist/server/lib/oauth/callback.js:26:103)
    at _next (<irrelevant-project-path>/node_modules/next-auth/dist/server/lib/oauth/callback.js:28:194)
    at <irrelevant-project-path>/node_modules/next-auth/dist/server/lib/oauth/callback.js:28:364
    at new Promise (<anonymous>)
    at <irrelevant-project-path>/node_modules/next-auth/dist/server/lib/oauth/callback.js:28:97
    at <irrelevant-project-path>/node_modules/next-auth/dist/server/lib/oauth/callback.js:143:17
    at <irrelevant-project-path>/node_modules/next-auth/dist/server/routes/callback.js:58:31
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (<irrelevant-project-path>/node_modules/next-auth/dist/server/routes/callback.js:26:103)
    at _next (<irrelevant-project-path>/node_modules/next-auth/dist/server/routes/callback.js:28:194)
    at <irrelevant-project-path>/node_modules/next-auth/dist/server/routes/callback.js:28:364
    at new Promise (<anonymous>)
    at <irrelevant-project-path>/node_modules/next-auth/dist/server/routes/callback.js:28:97
    at <irrelevant-project-path>/node_modules/next-auth/dist/server/routes/callback.js:302:17
https://next-auth.js.org/errors#callback_oauth_error

Additional context
No additional context I can think of.

Documentation feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • Found the documentation helpful
  • Found documentation but was incomplete
  • Could not find relevant documentation
  • Found the example project helpful
  • Did not find the example project helpful

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions