我不会在开放 API 规范中发布任何可能导致安全问题的密钥和/或机密。此类信息应与 API 文档分开并通过安全线路进行交流。
我没有找到很多示例来说明至少如何启用 apiKey,因此这是定义 HMAC 安全性或 apiKey 的一种方法。 (使用开放 API 规范 3.0)
在 components 下的根级别,需要定义“securitySchemes”。 (这是让它工作所必需的。)
components:
securitySchemes:
api_key:
type: apiKey
name: gpa_key
in: header
description: HMAC code encoded with key
parameters:
CustomerIdPathParam:
name: customerId
in: query
description: Customer id
required: true
schema:
type: string
maxLength: 32
example: TEST_123012
schemas:
InventoryDetails:
type: objectBlockquote
securitySchemes 中的字段名称“api_key”必须与用于端点定义的安全性相匹配。
注意:“type”必须是以下类型之一:“apiKey”、“http”、“oauth2”、“OpenIdConnect”。在这种情况下,我选择了 apiKey 来表示 HMAC 安全性。
那么您有几种方法可以为您的 API 实施安全性。
- 所有已定义端点的安全性,安全性定义必须在根级别定义,使用在securitySchemes 中指定的名称。让阵列保持打开状态。
info:
servers:
- url: 'https://stage.com'
description: Stage Server
- url: 'https://prod.com'
description: Prod Server
security:
- api_key: [ ]
paths:
- 为每个请求定义安全性:
/coffee/hot/:
post:
summary: Coffee request order
description: Coffee a request order
security:
- api_key: [ ]
tags:
- Starbuckin
在此示例中,我们为此 POST 请求端点启用“api_key”安全类型。
- 同时使用。如果您想删除任何请求的安全性,请将安全性定义留空,如下所示:
/coffee/buildinfo:
get:
summary: Show the build information
description: Show the detail build information
tags:
- Starbuckin
security: [ ]
responses:
最后,结果应该在启用了安全性的请求上显示一个安全锁图标。