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

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

説明

OpenAPI 3.0 の security では、apiKey と http スキームの値を空の配列 [] にします。必要なスコープは oauth2 または openIdConnect スキームに指定します。

想定される影響

対応していないスキームにスコープを指定すると、検証に失敗したり、クライアントが存在しない権限を要求したりする場合があります。

対処方法

各選択肢を、API が受け付ける認証情報と権限に照らして確認します。OAuth2 の認可コードフローを使うクライアントでは PKCE を使ってください。

例

例の文書では api_key と petstore_auth を認証の選択肢として宣言しています。API キーにスコープはなく、OAuth2 を選ぶ場合は write:pets と read:pets の両方が必要です。

変更前

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"
            }
          }
        }
      }
    }
  }
}

参考資料