如何通过 AWS API Gateway 为单个终端节点使用多个 Cognito 用户池?

Dan*_*iel 7 amazon-web-services amazon-cognito aws-api-gateway

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

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

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

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

谢谢

Abh*_*yak 7

控制台不允许创建多个认知池用户,但 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'
Run Code Online (Sandbox Code Playgroud)


Dan*_*iel 6

截至目前,解决此问题的唯一可行的解​​决方案似乎是使用 Lambda 函数来授权用户,在令牌信息中检索用户池 ID,然后将其与允许的用户池列表进行比较,以确定是否授予他们访问权限。


Jai*_*oba 5

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

  • 允许使用来自不同 aws 帐户的不同 Cognitos 提供的 JWT 对单个端点进行授权。使用 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
Run Code Online (Sandbox Code Playgroud)