使用Serverless Framework的Cognito用户池授权程序

27

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

无服务器框架是否支持aws cognito?

如果支持,我们如何在无服务器环境下设置aws-userpool?

3个回答

42

是的。Serverless (v1.5) 支持 Cognito 用户池授权。

如果您使用以前版本的 Serverless,则必须更新到 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

更多细节请阅读文章。


感谢您的建议。但是,无服务器在v1.5中引入了这个功能。 - Niroshan Ranapathi
1
是的,我只是建议以防有人使用旧版本的Serverless(v1.5之前)。 - dege
2
@NiroshanRanapathi 你如何将授权程序设置为在serverless.yml资源部分中声明的Cognito用户池? - Resist Design
@NiroshanRanapathi 我想无服务器架构可能能够从 ARN 编号中检测出类型,但我不清楚“源”是如何确定的。在 API Gateway 控制台中,该字段是必需的。 - Omnibyte
这是如何在Lambda函数内获取JWT的授权声明的方法 https://vadymhimself.medium.com/setting-up-a-cognito-user-pool-authorizer-for-node-js-lambda-in-serverless-framework-ebf3ba6e7be5 - Bolein95
显示剩余6条评论

31

如果您想将授权者设置为在资源中声明的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

1
非常感谢!serverless.yml 的文档写得太差了。 - Bolein95
1
顺便说一下,如果有人想知道如何访问函数代码中的声明,请查看以下链接:https://vadymhimself.medium.com/setting-up-a-cognito-user-pool-authorizer-for-node-js-lambda-in-serverless-framework-ebf3ba6e7be5 - Bolein95

7

无服务器计算 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接