説明
最上位のsecurityがない場合、API全体に適用する既定の認証要件は宣言されません。個別の操作で認証を指定することもできるため、全体の設定がないだけで認証の欠落とは判断できません。
想定される影響
共通の要件を記載し忘れると、それを継承すべき操作が認証不要として記載されるおそれがあります。クライアントとサーバーのポリシーが食い違う原因になります。
対処方法
共通のポリシーがある場合は、最上位のsecurityに指定してください。操作ごとに管理する場合は、認証が必要なすべての操作を確認してください。参照する方式をOpenAPI 3.0のcomponents.securitySchemesまたは2.0のsecurityDefinitionsに定義し、サーバー側でも適用してください。
例
次の例では、既定の認証要件と対応するpetstore_authの定義を追加します。例のOAuth2 URLは、実際のプロバイダーのURLに置き換えてください。
変更前
json
{
"openapi": "3.0.0",
"info": {
"title": "Simple API overview",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"responses": {
"200": {
"description": "ok"
}
}
}
}
}
}
変更後
json
{
"openapi": "3.0.0",
"info": {
"title": "Simple API overview",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"responses": {
"200": {
"description": "ok"
}
}
}
}
},
"security": [
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
],
"components": {
"securitySchemes": {
"petstore_auth": {
"type": "oauth2",
"flows": {
"authorizationCode": {
"authorizationUrl": "https://example.com/oauth/authorize",
"tokenUrl": "https://example.com/oauth/token",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
}
}
}
}
}