【问题标题】:How to use multiple Cognito user pools for a single endpoint with AWS API Gateway?如何通过 AWS API Gateway 将多个 Cognito 用户池用于单个终端节点?
【发布时间】:2020-02-15 15:08:23
【问题描述】:

我最近将 API 网关实现为具有单个代理端点的代理。

我使用 Cognito 作为授权机制,只要我只有一个用户池,一切都很好。

我想要实现的是能够允许来自不同用户池的用户,但在 AWS 控制台中,我似乎只能选择一种 Cognito 机制,它只是一个用户池。

有没有办法通过另一种方式允许多个用户池?这种情况是否有替代的最佳实践?我真的需要用户位于不同的用户池中,这样他们的身份验证属性就不会共享,他们的帐户也不会相互化。

谢谢

【问题讨论】:

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


    【解决方案1】:

    我发现 Abhay Nayak 的回答很有用,它帮助我实现了我的场景:

    • 允许使用不同 Cognitos 提供的 JWT 从不同的 aws 帐户对单个端点进行授权。使用 cognito 用户池授权器,而不是自定义 lambda 授权器。

    这是我的无服务器 .yml 模板中的授权方和端点:

    functions:
      service:
        handler: service.service
        events:
          - http:
              path: service
              method: get
              authorizer:
                type: COGNITO_USER_POOLS
                authorizerId:
                  Ref: ApiGatewayAuthorizer
    
    
    resources:
      Resources:
        ApiGatewayAuthorizer:
          Type: AWS::ApiGateway::Authorizer
          Properties:
            AuthorizerResultTtlInSeconds: 300
            Name: API_AUTH_cognito_authorizer
            IdentitySource: method.request.header.Authorization
            RestApiId:
              Ref: ApiGatewayRestApi
            Type: COGNITO_USER_POOLS
            ProviderARNs:
              - arn:aws:cognito-idp:us-east-1:account1:userpool/userpool1
              - arn:aws:cognito-idp:us-east-1:account1:userpool/userpool2
              - arn:aws:cognito-idp:us-east-1:account2:userpool/userpool3
              - arn:aws:cognito-idp:us-east-1:account2:userpool/userpool4
    

    【讨论】:

      【解决方案2】:

      控制台不允许创建多个认知池用户,但 CLI 可以,我不确定是否所有程序更新(如 terraform 或 cloudformation)都可以做到,但 CLI 对我有用。试试这个:https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-authorizer.html

      您的 CLI 命令可能类似于以下内容:

          aws apigateway create-authorizer 
          --rest-api-id xxxxxxx 
          --name 'cognito-auth-name' 
          --type COGNITO_USER_POOLS 
          --provider-arns arn:aws:cognito-idp:arn-of-userpool arn:aws:cognito-idp:arn-of-userpool arn:aws:cognito-idp:arn-of-userpool
          --identity-source 'method.request.header.Authorization'
      

      【讨论】:

        【解决方案3】:

        截至今天,该问题的唯一可行解决方案似乎是使用 Lambda 函数来授权用户,在令牌信息中检索他们的用户池 ID,然后将其与允许的用户池列表进行比较,以便为他们提供访问与否。

        【讨论】:

        • 如果可以分享 Lambda 代码示例,那将是非常有帮助的,比如响应格式是什么等等。谢谢您的回复,对您有很大帮助。
        • 嘿@Nazeel,您找到解决问题的方法了吗?
        • 在 aws lambda 授权程序蓝图中,有一些模板类型的 python 代码可用,我使用它并根据我的需要进行了修改。
        【解决方案4】:

        Controlling and Managing Access to a REST API in API Gateway”有多种方式,User Pool as Authorizer 就是其中之一。在你的情况下,我会选择 IAM Permissionsattaching a policy to an IAM user representing the API caller, to an IAM group containing the user, or to an IAM role assumed by the user

        作为参考,我建议使用 IAM Permissions 而不是 Lambda authorizers,因为我认为不需要“使用不记名令牌身份验证以及标题、路径、查询描述的信息来控制对 REST API 方法的访问字符串、阶段变量或上下文变量请求参数”。

        【讨论】:

        • 感谢您的回答。但是,我需要一个用户池,因为我必须检索有关每个用户的信息(直接从令牌中获取这些信息)。我不希望每个最终用户都有一个 IAM 用户,因为它不是用来存储 OAuth 属性的。此外,根据您的建议,我也无法通过使用 OAuth 联合身份来注册用户,所以这不是一个解决方案。
        猜你喜欢
        • 2021-07-25
        • 2022-11-15
        • 1970-01-01
        • 2017-05-16
        • 2019-04-01
        • 2018-03-02
        • 2022-01-11
        • 2016-08-24
        • 2020-12-22
        相关资源
        最近更新 更多