Cognito user pool authorizer With Serverless Framework

后端 未结 3 1168
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 00:42

I need to authorize my API end point using aws cognito userpool. I can do it manually, but I need to automate the authorization part with the serverless framework.

相关标签:
3条回答
  • 2020-12-31 01:27

    Yes . Serverless (v1.5) support to Cognito user pool authorizer.

    If you use previous version of serverless you have to update v1.5 or later.

    For the user-pool authorization of api end point you have to specify pool 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
    

    More details read this article.

    0 讨论(0)
  • 2020-12-31 01:28

    If you want to set the authorizer to a Cognito User Pool you have declared in your resources you must use CloudFormation to create the authorizer as well.

    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
    
    0 讨论(0)
  • 2020-12-31 01:36

    Serverless 1.35.1

    In case someone stumbles across this how I did. Here is my working solution.

    Wherever you create the user pool, you can go ahead and add 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]
    

    Then when you define your functions

    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
    
    0 讨论(0)
提交回复
热议问题