amazon-web-services - 具有 OpenAPI 规范中定义的 Cognito Authorizer 的 AWS APIGateway

标签 amazon-web-services aws-api-gateway amazon-cognito

工具

  • Terraform v0.11.14

  • 设置
  • 由 Terraform 管理的 API 网关,使用 OpenAPI 规范定义
  • Cognito 授权方

  • 我正在尝试为我的 API 中的方法指定授权方。我可以使用控制台来做到这一点(它有很好的记录):

    Manual Cognito Authorizer

    问题

    我希望能够使用 OpenAPI 规范以编程方式进行设置。 AWS 的相关文档是 here

    它说您可以通过指定在 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}'
    

    这按预期工作。

    然后一旦完成,您应该能够将 Authorizer 应用于资源方法,如下所示:
    post:
      summary: Create a new Item
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Item'
      security:
        - NameOfCognitoAuthorizer:
    

    但是,一旦我应用更改并检查 AWS 控制台中的 post 方法,我可以看到 Authorizer 尚未应用于 API 方法。谁能看到我做错了什么?

    为了完整起见,我的 API 是使用 terraform 创建的:

    resource "aws_api_gateway_rest_api" "this" {
      name        = "MyAPI"
      body        = "${file("./api-spec.yaml")}"
    
      endpoint_configuration {
        types = ["REGIONAL"]
      }
    }
    

    最佳答案

    我遇到过同样的问题。这是一个有效的 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}
    

    关键是端点上的安全引用(注意 test/read 这里是我在 Cognito 上定义的范围,但您可以使用空数组 [] ):
          security:
            - EndpointAuthorizer: ["test/read"]
    

    在上面的定义中,AWS 将导入在 EndpointAuthorizer 中定义的 Cognito 授权器(在我的例子中名为 components.securitySchemes)。 ,但如果您愿意,也可以使用 Terraform 创建它(只需确保将其从 OpenAPI 规范中删除)

    关于amazon-web-services - 具有 OpenAPI 规范中定义的 Cognito Authorizer 的 AWS APIGateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56337629/

    相关文章:

    amazon-web-services - Refresh token 可以刷新吗?

    javascript - 如何验证 getOpenIdTokenForDeveloperIdentity 认知 token

    amazon-web-services - CloudFormation 删除更新堆栈操作上的 AWS Cognito Lambda 触发器

    amazon-web-services - 如何永久更改我的 aws cloudwatch 仪表板的时间范围?

    php - 亚马逊 S3-x-amz-meta

    mysql - jenkins 在重新启动我的 sql "sudo: no tty present and no askpass program specified Sorry, try again."时失败

    amazon-web-services - Amazon HTTP API 网关无法通过 VPC 链接工作

    django - 当我检查 nginx access.log 时,定期出现未知的 HEAD 请求

    amazon-web-services - 只能从 API Gateway 访问的公共(public) ELB

    amazon-web-services - terraform aws apigateway path_part