How to obtain the Token endpoint from a NextAuth Provider configured with the wellKnown option?

Viewed 349

NextAuth v4 implemented the possibility of configuring providers using the OIDC Discovery feature, that is, by defining just a wellKnown option (here) pointing to this discovery endpoint provided by the Authorization Service, e.g.:

export const oauthCustomProviderConfig = {
  id: 'myprovider',
  name: 'MyProvider',
  type: 'oauth',
  version: '2.0',
  wellKnown:
    process.env.A6_APP_OAUTH_PROVIDER_DISCOVERY_ENDPOINT ||
    'http://localhost:9081/auth/realms/myrealm/.well-known/openid-configuration',
  idToken: true,
  issuer: process.env.A6_APP_OAUTH_PROVIDER_ISSUER || 'realms/myrealm/',
  checks: ['pkce', 'state'],
  async profile(profile, tokens) {
    return {
      id: profile.sub,
      name: profile.name,
      email: profile.email,
      image: profile.picture,
    };
  },
  clientId: process.env.A6_APP_OAUTH_CLIENT_ID || 'clientId',
  clientSecret: process.env.A6_APP_OAUTH_CLIENT_SECRET || 'clientSecret',
};

This works fine, but then in order to develop a "Refresh Token Rotation" solution, we have to manually call the Token endpoint of our provider. As per the linked documentation:

async function refreshAccessToken(token) {
  try {
    const url =
      "https://oauth2.googleapis.com/token?" +
      new URLSearchParams({
        client_id: process.env.GOOGLE_CLIENT_ID,
        client_secret: process.env.GOOGLE_CLIENT_SECRET,
        grant_type: "refresh_token",
        refresh_token: token.refreshToken,
      })
...

I would expect to be able to access the configured Token endpoint using the getProviders() feature (here), however, this doesn't provide the configured endpoints:

{
  myprovider: {
    id: 'myprovider',
    name: 'MyProvider',
    type: 'oauth',
    signinUrl: 'http://localhost:90/api/auth/signin/myprovider',
    callbackUrl: 'http://localhost:90/api/auth/callback/myprovider'
  }
}

I think the only solution at this point would be to manually provide the Token endpoint as a configuration in my application, or to explore the discovery endpoint manually (adding a request each time we refresh the token), which makes the use of the wellKnown feature not optimal.

Is there a way to obtain the configured Token endpoint from the configured provider?

0 Answers
Related