スコープに対応していない認証方式へのスコープの指定

OpenAPI 3.0でスコープを指定できるのは、OAuth2とOpenID Connectのセキュリティ要件です。

説明

OpenAPI 3.0では、スコープを使用できる認証方式はoauth2とopenIdConnectです。apiKeyやhttpにスコープを指定すると、文書の認証要件がその方式と一致しなくなります。

想定される影響

検証ツールが文書をエラーとして扱ったり、APIの利用者が対応していないスコープを要求したりする可能性があります。

対処方法

apiKeyとhttpのセキュリティ要件には、空の配列[]を指定します。oauth2またはopenIdConnectでは、実際に必要なスコープを指定し、認証プロバイダーの設定と一致させてください。

例

この例では、api_keyのスコープを削除し、OAuth2のスコープを維持しています。別々の配列要素は選択肢を表すため、両方の認証方式を要求するものではありません。OAuth2の認可コードフローはPKCEと併用してください。

変更前

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "security": [
    {
      "api_key": [
        "write:api",
        "read:api"
      ]
    },
    {
      "petstore_auth": [
        "write:pets",
        "read:pets"
      ]
    }
  ],
  "paths": {},
  "components": {
    "securitySchemes": {
      "api_key": {
        "type": "apiKey",
        "name": "api_key",
        "in": "header"
      },
      "petstore_auth": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://example.org/api/oauth/dialog",
            "tokenUrl": "https://example.org/api/oauth/token",
            "scopes": {
              "write:pets": "modify pets in your account",
              "read:pets": "read your pets"
            }
          }
        }
      }
    }
  }
}

変更後

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "security": [
    {
      "api_key": []
    },
    {
      "petstore_auth": [
        "write:pets",
        "read:pets"
      ]
    }
  ],
  "paths": {},
  "components": {
    "securitySchemes": {
      "api_key": {
        "type": "apiKey",
        "name": "api_key",
        "in": "header"
      },
      "petstore_auth": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://example.org/api/oauth/dialog",
            "tokenUrl": "https://example.org/api/oauth/token",
            "scopes": {
              "write:pets": "modify pets in your account",
              "read:pets": "read your pets"
            }
          }
        }
      }
    }
  }
}

参考資料