Authentication schemes are missing from an OpenAPI 2.0 document

Define and apply the authentication schemes required by an API in securityDefinitions and security.

Description

In OpenAPI 2.0, securityDefinitions describes how clients authenticate. A public API that accepts anonymous requests can omit it.

Potential impact

Without a definition for a protected API, generated clients may omit credentials or send them to the wrong place.

Remediation

Define the server’s scheme in securityDefinitions. Reference it by name in global or operation-level security, then verify that the server rejects requests missing the required credentials.

Examples

The example requires ApiKeyAuth across the API. Clients send the key in the X-API-Key header.

Before

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    }
  },
  "securityDefinitions": {}
}

After

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    }
  },
  "securityDefinitions": {
    "ApiKeyAuth": {
      "type": "apiKey",
      "in": "header",
      "name": "X-API-Key"
    }
  },
  "security": [
    {
      "ApiKeyAuth": []
    }
  ]
}

References