写:宠物 and 阅读:宠物 are Oauth2 scopes
并且与 OpenAPI (fka.Swagger) 无关operations categorization
.
Oauth2 范围
当 API 通过 Oauth 进行保护时,范围用于向 API 使用者授予不同的权利/特权。范围由名称定义(您可以使用任何您想要的名称)。
Oauth scopes authorization in SwaggerUI http://petstore.swagger.io/ which can act as an API consumer:
在这种情况下,这个 oauth2 安全 API 提出了 2 个范围:
- write:pets:修改您帐户中的宠物
- 阅读:宠物:阅读你的宠物
当使用 OpenAPI (fka.Swagger) 规范描述 API 时,您可以定义这些范围,如问题中所示。
但是,如果您不声明这些范围涵盖哪些操作,则仅定义这些范围是没有用的。
这是通过将其添加到操作中来完成的:
security:
- petstore_auth:
- read:pets
在此示例中,只有当 API 使用者被允许使用read:pets
scope.
请注意,单个操作可以属于多个 oauth2 范围以及多个安全定义。
您可以在此处阅读有关 OpenAPI (fka.Swagger) 安全性的更多信息
- 安全方案对象 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#security-scheme-object
- 安全需求对象对象定义 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#security-requirement-object
- 我的《编写 OpenAPI (Swagger) 规范教程》中有关安全性的第 6 部分 http://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-6-defining-security/
OpenAPI(fka.Swagger)操作分类
无论 OAuth2 范围如何,如果您需要对 API 的操作进行分类,您可以使用tags
:
tags:
- pets
通过将其添加到操作中,它将被放入类别中pets
。
单个操作可以属于多个类别。
Theses categories are used by SwaggerUI http://petstore.swagger.io/ to regroup operations. In the screen capture below, we can see 3 categories (pet, store and user):
You can read more about categories here:
- 标签对象 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#tagObject
- 操作对象 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operation-object
- 我的《编写 OpenAPI (Swagger) 规范教程》中有关文档的第 7 部分 http://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-7-documentation/
这是使用 Oauth2 范围和类别的完整示例
swagger: "2.0"
info:
version: "1.0.0"
title: Swagger Petstore
securityDefinitions:
petstore_auth:
type: oauth2
authorizationUrl: http://petstore.swagger.io/api/oauth/dialog
flow: implicit
scopes:
write:pets: modify pets in your account
read:pets: read your pets
paths:
/pets/{petId}:
parameters:
- in: path
name: petId
description: ID of pet that needs to be fetched
required: true
type: integer
format: int64
get:
tags:
- pets
summary: Find pet by ID
responses:
"404":
description: Pet not found
"200":
description: A pet
schema:
$ref: "#/definitions/Pet"
security:
- petstore_auth:
- read:pets
delete:
tags:
- pets
summary: Deletes a pet
responses:
"404":
description: Pet not found
"204":
description: Pet deleted
security:
- petstore_auth:
- write:pets
definitions:
Pet:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
example: doggie