simple-oauth2 icon indicating copy to clipboard operation
simple-oauth2 copied to clipboard

Access Token function getToken() give error url.URL not a contructor

Open Ugesh2204 opened this issue 2 years ago • 2 comments

Context

  • node version: v18.13.0
  • module version: 5.0.0
  • environment (e.g. node, browser, native): React
  • any other relevant information:

How can we help?

Need some with this module get token function giving error like url.Url is not a contructor. My credentials are working fine with PostMan. Need some help to get the accessToken. Please find my code below. Don't know why am getting this error ?

const getToken = async () => {
  if (spAccessToken && !spAccessToken.expired(300)) {
    return spAccessToken;
  }
  const config = {
    client: {
      id: process.env.NEXT_PUBLIC_CLIENT_ID,
      secret: process.env.NEXT_PUBLIC_CLIENT_SECRET,
    },
    auth: {
      tokenHost: process.env.NEXT_PUBLIC_ACCESS_TOKEN_URL,
      tokenPath: process.env.NEXT_PUBLIC_TENANT_ID + "/oauth2/token",
    },
    options: {
      bodyFormat: "form",
      authorizationMethod: "body",
    },
  };
  const tokenConfig = {
    scope: "",
    resource: process.env.NEXT_PUBLIC_CLIENT_ID,
  };
  const { ClientCredentials } = require("simple-oauth2");
  const client = new ClientCredentials(config);

  try {
    console.log(config);
    const accessToken = await client.getToken(tokenConfig);
    console.log("get token", accessToken);
    // spAccessToken = accessToken;
    return accessToken;
  } catch (error) {
    // console.log(error.data);
    console.log(error.message);
    return;
  }
};

Ugesh2204 avatar Feb 03 '23 07:02 Ugesh2204

If you log the config.auth.tokenHost + config.auth.tokenPath does it have the right number of slashes in the right places for a URL?

Also, if you combine the strings outside the config object does that help? Here's my implementation:

const tokenURL = "https://login.microsoftonline.com/" + AppTenant + "/";
app.context.oauthConfig = {
    client: {
        id: AppID,
        secret: AppSecret
    },
    auth: {
        tokenHost: tokenURL,
        tokenPath: 'oauth2/token'
    },
    options: {
        bodyFormat: 'form'
    }
};

async function OAuthSetup()
{
    const clientCredFlow = new ClientCredentials(app.context.oauthConfig);
    app.context.OAUTH = clientCredFlow;
}

async function RenewToken()
{
    const tokenConfig = {
        scope: "",
        resource: "https://management.azure.com/"
    };

    try {
        app.context.TOKEN = await app.context.OAUTH.getToken(tokenConfig);
        app.context.RFCTOKEN = app.context.OAUTH.createToken(app.context.TOKEN);
    } catch (error) {
        LOG.LogError("Access Token Error: " + error.message);
    }
}

rallegretti avatar Feb 09 '23 17:02 rallegretti

Hey @Ugesh2204. When you say your environment is "react". What do you mean exactly? Is your code bundled and shipped to browsers?

Keep this in mind: this library is designed for server side usage, not because we don't want to support client applications, but rather because client applications don't have the means to safely store your secrets. So, unless you really know what you are doing, I would advice against using this library in a typical client application.

jonathansamines avatar Mar 22 '23 03:03 jonathansamines