Authentication
Swagger 2.0 lets you define the following authentication types for an API:
- Basic authentication
- API key (as a header or a query string parameter)
- OAuth 2 common flows (authorization code, implicit, resource owner password credentials, client credentials)
Follow the links above for examples specific to these authentication types, or continue reading to learn how to describe authentication in general.
Authentication is described by using the securityDefinitions
and security
keywords. You use securityDefinitions
to define all authentication types supported by the API, then use security
to apply specific authentication types to the whole API or individual operations.
The securityDefinitions
section is used to define all security schemes (authentication types) supported by the API. It is a name->definition map that maps arbitrary names to the security scheme definitions. Here, the API supports three security schemes named BasicAuth, ApiKeyAuth and OAuth2, and these names will be used to refer to these security schemes from elsewhere:
Each security scheme can be of type
:
basic
for Basic authenticationapiKey
for an API keyoauth2
for OAuth 2
Other required properties depend on the security type. For details, check the Swagger Specification or our examples for Basic auth and API keys.
After you have defined the security schemes in securityDefinitions
, you can apply them to the whole API or individual operations by adding the security
section on the root level or operation level, respectively. When used on the root level, security
applies the specified security schemes globally to all API operations, unless overridden on the operation level.
In the following example, the API calls can be authenticated using either an API key or OAuth 2. The ApiKeyAuth and OAuth2 names refer to the security schemes previously defined in securityDefinitions
.
Global security
can be overridden in individual operations to use a different authentication type, different OAuth 2 scopes, or no authentication at all:
Using Multiple Authentication Types
Some REST APIs support several authentication types. The security
section lets you combine the security requirements using logical OR and AND to achieve the desired result. security
uses the following logic:
That is, security
is an array of hashmaps, where each hashmap contains one or more named security schemes. Items in a hashmap are combined using logical AND, and array items are combined using logical OR. Security schemes combined via OR are alternatives – any one can be used in the given context. Security schemes combined via AND must be used simultaneously in the same request. Here, we can use either Basic authentication or an API key:
Here, the API requires a pair of API keys to be included in requests:
Here, we can use either OAuth 2 or a pair of API keys:
FAQ
What does [ ] mean in “securitySchemeName: [ ]”?
[]
is a YAML/JSON syntax for an empty array. The Swagger Specification requires that items in the security
array specify a list of required scopes, as in:
Scopes are only used with OAuth 2, so the Basic and API key security
items use an empty array instead.
Reference
Did not find what you were looking for? Ask the community
Found a mistake? Let us know