Gre*_*uff 5 amazon-web-services amazon-cognito aws-api-gateway
工具
设置
我正在尝试为我的 API 中的方法指定授权者。我可以使用控制台来做到这一点(它有很好的文档记录):
问题
我希望能够使用 OpenAPI 规范以编程方式进行设置。AWS 的相关文档在这里
它表示您可以通过指定以下内容在 OpenAPI 规范中创建 Authorizer 对象:
securitySchemes:
NameOfCognitoAuthorizer:
type: apiKey
name: Authorization
in: header
x-amazon-apigateway-authtype: cognito_user_pools
x-amazon-apigateway-authorizer:
type: cognito_user_pools
providerARNs:
- 'arn:aws:cognito-idp:{region}:{account_id}:userpool/{user_pool_id}'
Run Code Online (Sandbox Code Playgroud)
这按预期工作。
完成后,您应该能够将 Authorizer 应用到资源方法,如下所示:
post:
summary: Create a new Item
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/Item'
security:
- NameOfCognitoAuthorizer:
Run Code Online (Sandbox Code Playgroud)
但是,当我应用更改并在 AWS 控制台中检查 post 方法时,我可以看到 Authorizer 尚未应用于 API 方法。谁能看到我做错了什么吗?
为了完整起见,我的 API 是使用 terraform 创建的:
securitySchemes:
NameOfCognitoAuthorizer:
type: apiKey
name: Authorization
in: header
x-amazon-apigateway-authtype: cognito_user_pools
x-amazon-apigateway-authorizer:
type: cognito_user_pools
providerARNs:
- 'arn:aws:cognito-idp:{region}:{account_id}:userpool/{user_pool_id}'
Run Code Online (Sandbox Code Playgroud)
小智 9
我遇到过同样的问题。下面是一个有效的 OpenAPI 规范示例:
openapi: 3.0.0
info:
title: Sample API
description: api description here
version: '0.1'
paths:
/:
get:
summary: Describe the endpoint
responses:
'200':
description: "All good"
security:
- EndpointAuthorizer: ["test/read"]
x-amazon-apigateway-integration:
uri: arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/arn:aws:lambda:{region}:{account_id}:function:{function_name}/invocations
passthroughBehavior: "when_no_match"
httpMethod: "POST"
type: "aws_proxy"
components:
securitySchemes:
EndpointAuthorizer:
type: apiKey
name: Authorization
in: header
x-amazon-apigateway-authtype: cognito_user_pools
x-amazon-apigateway-authorizer:
type: cognito_user_pools
providerARNs:
- arn:aws:cognito-idp:{region}:{account_id}:userpool/{user_pool_id}
Run Code Online (Sandbox Code Playgroud)
关键是端点上的安全引用(注意test/read这里是我在 Cognito 上定义的范围,但您可以使用空数组[]):
security:
- EndpointAuthorizer: ["test/read"]
Run Code Online (Sandbox Code Playgroud)
在上面的定义中,AWS 将导入EndpointAuthorizer中定义的 Cognito Authorizer(在我的例子中命名)components.securitySchemes,但如果您愿意,您可以使用 Terraform 创建它(只需确保将其从 OpenAPI 规范中删除)
| 归档时间: |
|
| 查看次数: |
4493 次 |
| 最近记录: |