Security Scheme Objects in OpenAPI
Security scheme objects are defined in the Components Object under the securitySchemes
field. Each security scheme object has a unique key. Security Requirement Objects elsewhere in the document reference security scheme objects by their keys.
The following example requires a basic authentication scheme to access the /drinks
endpoint:
paths:/drinks:get:security:- MyScheme17: []components:securitySchemes:MyScheme17:type: httpscheme: basic
The type
field is the overall category of authentication. The value of type
determines the other fields the security object needs.
To decide which authentication type to choose, see our article OpenAPI Tips - How to Handle Auth (opens in a new tab).
OpenAPI-Supported Authentication Types
The following authentication types are supported in the OpenAPI Specification:
- API Key
- Basic HTTP
- Bearer Token
- OAuth 2.0
- OpenID Connect
- Digest
- Mutual TLS
OpenAPI Example Security Scheme Schema
Below is an example security schemes object with every possible field besides extensions.
components:securitySchemes:# apiKey ------------auth1:description: Recommended authenticatortype: apiKeyin: queryname: keyauth2:type: apiKeyin: headername: X-API-Keyauth3:type: apiKeyin: cookiename: key# http ------------auth4:type: httpscheme: basicauth5:type: httpscheme: bearerbearerFormat: JWTauth6:type: httpscheme: digest # not supported by Speakeasy# mutualTLS ------------auth7:type: mutualTLS # not supported by Speakeasy# openIdConnect ------------auth8:type: openIdConnectopenIdConnectUrl: https://example.com/openidconfig.json# oauth2 ------------auth9:type: oauth2flows:authorizationCode:scopes:read: Grants read accesswrite: Grants write accessauthorizationUrl: https://test.com/oauth/authorizetokenUrl: https://test.com/oauth/tokenrefreshUrl: https://test.com/oauth/refreshclientCredentials:scopes:read: Grants read accesswrite: Grants write accesstokenUrl: https://test.com/oauth/tokenrefreshUrl: https://test.com/oauth/refreshimplicit:scopes:read: Grants read accesswrite: Grants write accessauthorizationUrl: https://test.com/oauth/authorizerefreshUrl: https://test.com/oauth/refreshpassword:scopes:read: Grants read accesswrite: Grants write accesstokenUrl: https://test.com/oauth/tokenrefreshUrl: https://test.com/oauth/refresh