MinimalPermissionsGuidancePlugin

将发送到 API 的 JWT 令牌中使用的权限与记录的代理所记录的请求所需的最低范围进行比较,并显示差异。

显示开发代理检查记录的 API 请求是否使用最少的 API 权限的命令行的屏幕截图。

插件实例定义

{
  "name": "MinimalPermissionsGuidancePlugin",
  "enabled": true,
  "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
  "configSection": "minimalPermissionsGuidancePlugin"
}

配置示例

{
  "minimalPermissionsGuidancePlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.27.0/minimalpermissionsguidanceplugin.schema.json",
    "apiSpecsFolderPath": "./api-specs"
  }
}

配置属性

properties 说明 默认
apiSpecsFolderPath 具有 API 规范的文件夹的相对路径或绝对路径

命令行选项

注解

MinimalPermissionsGuidancePlugin 插件检查应用是否使用最少的权限来调用 API。 若要检查权限,插件使用有关位于指定本地文件夹中的 API 的信息。

定义 API 权限

MinimalPermissionsGuidancePlugin 插件支持检查使用 OAuth 保护的 API 的 OAuth 权限。 该插件使用提供的 API 规范中的信息计算调用应用中使用的 API 所需的最小权限。 然后,该插件将 JSON Web 令牌(JWT)令牌中使用的权限与开发代理记录的请求所需的最低范围进行比较。

若要定义 API 的权限,请在 API 的 OpenAPI 定义中包括这些权限。 以下示例演示如何在 OpenAPI 定义中定义 API 的权限:

{
  "openapi": "3.0.1",
  "info": {
    "title": "Northwind API",
    "description": "Northwind API",
    "version": "v1.0"
  },
  "servers": [
    {
      "url": "https://api.northwind.com"
    }
  ],
  "components": {
    "securitySchemes": {
      "OAuth2": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
            "tokenUrl": "https://login.microsoftonline.com/common/oauth2/token",
            "scopes": {
              "customer.read": "Grants access to ready customer info",
              "customer.readwrite": "Grants access to read and write customer info"
            }
          }
        }
      }
    },
    "schemas": {
      "Customer": {
        "type": "object",
        // [...] trimmed for brevity
      }
    }
  },
  "paths": {
    "/customers/{customers-id}": {
      "description": "Provides operations to manage a customer",
      "get": {
        "summary": "Get customer by ID",
        "operationId": "getCustomerById",
        "security": [
          {
            "OAuth2": [
              "customer.read"
            ]
          },
          {
            "OAuth2": [
              "customer.readwrite"
            ]
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json; charset=utf-8": {
                "schema": {
                  "$ref": "#/components/schemas/Customer"
                }
              }
            }
          }
        }
      },
      "patch": {
        "summary": "Update customer by ID",
        "operationId": "updateCustomerById",
        "security": [
          {
            "OAuth2": [
              "customer.readwrite"
            ]
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Customer"
              }
            }
          }
        },
        "responses": {
          "204": {
            "description": "No Content"
          }
        }
      },
      "parameters": [
        {
          "name": "customers-id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ]
    }
  },
  "x-ms-generated-by": {
    "toolName": "Dev Proxy",
    "toolVersion": "0.22.0"
  }
}

相关部分是 securitySchemes 该部分,可在其中定义 API 使用的 OAuth 范围。 然后,对于每个操作,你将在部分中包括所需的范围 security

替换 API 规范中的变量

某些 API 规范可能包含服务器 URL 中的变量。 使用变量是适应不同环境(例如开发、过渡、生产)、API 版本或租户的常见做法。 具有变量的 URL 如下所示:

openapi: 3.0.4
info:
  title: SharePoint REST API
  description: SharePoint REST API
  version: v1.0
servers:
  - url: https://{tenant}.sharepoint.com
    variables:
      tenant:
        default: contoso

插件 MinimalPermissionsGuidancePlugin 支持替换 API 规范内容中的变量。 若要替换变量,请使用选项启动开发代理 --env ,并指定变量名称和值。 例如,若要将 tenant 变量 contoso替换为,请使用以下命令:

devproxy --env tenant=northwind

此命令将 API 规范中的变量替换为 tenantnorthwind。 该插件使用替换的 URL 来检查应用是否使用最少的权限来调用 API。

详细信息