【问题标题】:Cognito user pool authorizer With Serverless Framework使用无服务器框架的 Cognito 用户池授权者
【发布时间】:2017-05-30 14:26:59
【问题描述】:

我需要使用 aws cognito 用户池授权我的 API 端点。我可以手动完成,但我需要使用无服务器框架自动化授权部分。

Serverless 框架是否支持 aws cognito?

如果是这样,我们如何使用无服务器设置 aws-userpool?

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway amazon-cognito serverless-framework


    【解决方案1】:

    是的。对 Cognito 用户池授权方的无服务器 (v1.5) 支持。

    如果您使用以前版本的无服务器,则必须更新 v1.5 或更高版本。

    对于 api 端点的用户池授权,您必须指定池 arn。

    functions:
      hello:
        handler: handler.hello
        events:
          - http:
              path: hello
              method: get
              integration: lambda
              authorizer:
                name: authorizer
                arn: arn:aws:cognito-idp:us-east-1:123456789:userpool/us-east-1_XXXXXX
    

    更多详情请阅读this 文章。

    【讨论】:

    • 只是一个说明,您不需要更新到 1.5 版,您可以使用自定义授权方 serverless.com/framework/docs/providers/aws/events/… 与此您必须构建将使用用户池的授权方这也可能有帮助aws.amazon.com/pt/blogs/compute/…
    • 感谢您的建议。但是 serverless 在 v1.5 中引入了这个功能
    • 是的,我知道 :) 我建议以防万一有人使用旧版本的无服务器 pre v1.5
    • 我应该改变这个“如果你使用以前版本的无服务器,你必须更新 v1.5 或更高版本”
    • @NiroshanRanapathi 如何将授权方设置为您在 serverless.yml 的资源部分中声明的 Cognito 用户池?
    【解决方案2】:

    如果您想将授权方设置为您在资源中声明的 Cognito 用户池,您还必须使用 CloudFormation 来创建授权方。

    functions:
      functionName:
        # ...
        events:
          - http:
              # ...
              authorizer: 
                 type: COGNITO_USER_POOLS
                 authorizerId: 
                   Ref: ApiGatewayAuthorizer
    
    resources:
      Resources:
        ApiGatewayAuthorizer: 
          Type: AWS::ApiGateway::Authorizer
          Properties: 
            Name: CognitoUserPool
            Type: COGNITO_USER_POOLS
            IdentitySource: method.request.header.Authorization
            RestApiId: 
              Ref: ApiGatewayRestApi
            ProviderARNs: 
              - Fn::GetAtt:
                  - UserPool
                  - Arn
    
        UserPool:
          Type: AWS::Cognito::UserPool
    

    【讨论】:

    • 非常感谢! serverless.yml 的文档记录很差。
    • 顺便说一句,如果有人想知道如何访问函数代码中的声明:vadymhimself.medium.com/…
    【解决方案3】:

    无服务器 1.35.1

    以防有人偶然发现我的做法。这是我的工作解决方案。

    无论您在何处创建用户池,都可以继续添加ApiGatewayAuthorizer

    # create a user pool as normal
    CognitoUserPoolClient:
      Type: AWS::Cognito::UserPoolClient
      Properties:
        # Generate an app client name based on the stage
        ClientName: ${self:custom.stage}-user-pool-client
        UserPoolId:
          Ref: CognitoUserPool
       ExplicitAuthFlows:
       - ADMIN_NO_SRP_AUTH
       GenerateSecret: true
    
    # then add an authorizer you can reference later
    ApiGatewayAuthorizer:
      DependsOn:
      # this is pre-defined by serverless
      - ApiGatewayRestApi
      Type: AWS::ApiGateway::Authorizer
      Properties:
        Name: cognito_auth
        # apparently ApiGatewayRestApi is a global string
        RestApiId: { "Ref" : "ApiGatewayRestApi" }
        IdentitySource: method.request.header.Authorization
        Type: COGNITO_USER_POOLS
        ProviderARNs:
        - Fn::GetAtt: [CognitoUserPool, Arn]
    

    然后当你定义你的函数时

    graphql:
      handler: src/app.graphqlHandler
      events:
      - http:
        path: /
        method: post
        cors: true
        integration: lambda
        # add this and just reference the authorizer
        authorizer:
          type: COGNITO_USER_POOLS
          authorizerId:
            Ref: ApiGatewayAuthorizer
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-19
      • 2020-03-31
      • 2020-05-18
      • 2020-04-01
      • 2022-11-10
      • 2017-10-25
      • 1970-01-01
      • 2021-10-08
      相关资源
      最近更新 更多