nextjs-auth0 icon indicating copy to clipboard operation
nextjs-auth0 copied to clipboard

Can not fetch user info with access_token

Open eminaydin opened this issue 5 years ago • 5 comments

I can fetch user info with idToken but not with access_token. Even though it works as expected just wanted to learn why would it be the case for me? If accessToken is used instead of idToken I get the following error:

error: "Forbidden"
errorCode: "insufficient_scope"
message: "Insufficient scope, expected any of: read:users,read:current_user,read:user_idp_tokens"
statusCode: 403

My code that throws error:

import auth0 from "../../src/utils/auth0";

export default async function me(req, res) {
  try {
    const {
      user: { sub },
      idToken,
      accessToken,
    } = await auth0.getSession(req);

    if (idToken) {
      const users = await fetch(process.env.AUDIENCE + "users/" + sub, {
        method: "GET",
        headers: {
          "Content-Type": "application/json",

// When accessToken is replaced with IdToken it works fine 

          Authorization: `Bearer ${accessToken}`,
        },
      })
        .then((a) => a.json())
        .then((json) => json);

      res.status(200).json(users);
    }
  } catch (error) {
    console.log(error);
    res.status(error.status || 500).end(error.message);
  }
}

Environment:


import { initAuth0 } from "@auth0/nextjs-auth0";

export default initAuth0({
  domain: process.env.DOMAIN,
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
  audience: process.env.AUDIENCE,
  scope: "openid profile read:users read:user_idp_tokens",
  redirectUri: "http://localhost:3000/api/callback",
  postLogoutRedirectUri: "http://localhost:3000/",
  session: {
    cookieSecret: process.env.COOKIE_SECRET,
    cookieLifetime: 86400 * 30,
    storeIdToken: true,
    storeAccessToken: true,
    storeRefreshToken: true,
  },
  oidcClient: {
    httpTimeout: 2500,
    clockTolerance: 10000,
  },
});

Dependencies:

 "dependencies": {
    "@auth0/nextjs-auth0": "^0.16.0",
}

eminaydin avatar Jan 25 '21 12:01 eminaydin

Hi @eminaydin - thanks for raising this

You're able to access that endpoint with an ID Token due to some old functionality of the Management API that we are deprecating.

Using ID tokens to call Management API endpoints is being deprecated. You must use access tokens. The grace period for this migration started on March 31, 2018. After you complete your migration to access tokens, turn off the Allow ID Tokens for Management API v2 Authentication toggle in the Dashboard. See https://auth0.com/docs/product-lifecycle/deprecations-and-migrations/migrate-to-calling-api-with-access-tokens

You should follow the advice in that article and turn off the "Allow ID Tokens for Management API v2 Authentication" toggle in the Dashboard.

To read "users/" + sub you should set the read:current_user scope (read:users and read:user_idp_tokens are for something else).

Could you change your code to use the Access Token and set your scope to read:current_user to check it works? (you may need to logout and login again)

adamjmcgrath avatar Jan 25 '21 12:01 adamjmcgrath

Hi @eminaydin - thanks for raising this

You're able to access that endpoint with an ID Token due to some old functionality of the Management API that we are deprecating.

Using ID tokens to call Management API endpoints is being deprecated. You must use access tokens. The grace period for this migration started on March 31, 2018. After you complete your migration to access tokens, turn off the Allow ID Tokens for Management API v2 Authentication toggle in the Dashboard. See https://auth0.com/docs/product-lifecycle/deprecations-and-migrations/migrate-to-calling-api-with-access-tokens

You should follow the advice in that article and turn off the "Allow ID Tokens for Management API v2 Authentication" toggle in the Dashboard.

To read "users/" + sub you should set the read:current_user scope (read:users and read:user_idp_tokens are for something else).

Could you change your code to use the Access Token and set your scope to read:current_user to check it works? (you may need to logout and login again)

Hey @adamjmcgrath, thanks a lot for the guidance. I have followed the steps and it is working properly right now. Just one quick reminder, I haven't set the toggle myself so I assume it was toggled by default. If this is something deprecated don't you think its worth to include it in the documentation to suggest new users disable the functionality?

eminaydin avatar Jan 25 '21 13:01 eminaydin

If this is something deprecated don't you think it's worth to include it in the documentation to suggest new users disable the functionality?

This should be disabled for new users by default and the toggle should not be accessible, the fact that you had it enabled suggests to me that you had an older tenant configuration - when did you create your tenant?

adamjmcgrath avatar Jan 25 '21 15:01 adamjmcgrath

If this is something deprecated don't you think it's worth to include it in the documentation to suggest new users disable the functionality?

This should be disabled for new users by default and the toggle should not be accessible, the fact that you had it enabled suggests to me that you had an older tenant configuration - when did you create your tenant?

Well, the tenant has been created at least a year ago and that might be the case as you mentioned.

eminaydin avatar Jan 25 '21 16:01 eminaydin

Cool - makes sense, thanks for confirming @eminaydin - closing

adamjmcgrath avatar Jan 25 '21 16:01 adamjmcgrath