Change OAuth2 token URL

Use a custom decorator to change the OAuth credentials flow token URL.

Estimated time: 20 minutes

Step-by-step instructions

  1. Add environment variables to the API version's settings. environment variables
  2. Add this code to your repo with the API (the Redocly configuration file is an example).
    redocly.yamlacme-plugin.jsdecorators/change-token-url.js
    Copy
    Copied
    plugins:
      lint:
    extends:
      - recommended
    plugins:
      - './plugins/acme-plugin.js'
    decorators:
      acme/change-token-urls: error
    Copy
    Copied
    const ChangeTokenUrl = require('./decorators/change-token-url');
    const id = 'acme';
    
    /** @type {import('@redocly/cli').CustomRulesConfig} */
    const decorators = {
      oas3: {
        'change-token-url': ChangeTokenUrl,
      },
    };
    
    module.exports = {
      id,
      decorators,
    };
    Copy
    Copied
    module.exports = ChangeTokenUrl;
    
    /** @type {import('@redocly/cli').OasDecorator} */
    function ChangeTokenUrl() {
      return {
        SecuritySchemeFlows: {
          leave(flows, ctx) {
            if ('TOKEN_URL' in process.env && flows?.clientCredentials) {
              flows.clientCredentials.tokenUrl = process.env.TOKEN_URL;
            }
          }
        }
      }
    };