Undefined OAuth2 scope in global OpenAPI 3 security requirements

Global security requirements in OpenAPI 3.0 do not match the OAuth2 scope definitions.

Description

Match OAuth2 scopes in global OpenAPI 3.0 security to the selected flow’s scopes under components.securitySchemes. Operations without their own requirements inherit these defaults.

Potential impact

Clients that request a permission the provider cannot issue may fail to obtain a suitable token.

Remediation

Correct typos and define required permissions to match the authorization server. For OpenID Connect, check the provider’s scopes. Verify access on an operation that inherits the global setting.

Examples

The authorizationCode flow declares only read:api. The second excerpt removes error:api from the global requirement.

Before

json
{
  "openapi": "3.0.0",
  "security": [
    {
      "oAuth2AuthCode": [
        "read:api",
        "error:api"
      ]
    }
  ],
  "components": {
    "securitySchemes": {
      "oAuth2AuthCode": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://api.example.com/oauth/authorize",
            "tokenUrl": "https://api.example.com/oauth/token",
            "scopes": {
              "read:api": "read your apis"
            }
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "security": [
    {
      "oAuth2AuthCode": [
        "read:api"
      ]
    }
  ],
  "components": {
    "securitySchemes": {
      "oAuth2AuthCode": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://api.example.com/oauth/authorize",
            "tokenUrl": "https://api.example.com/oauth/token",
            "scopes": {
              "read:api": "read your apis"
            }
          }
        }
      }
    }
  }
}

References