代码之家  ›  专栏  ›  技术社区  ›  cchapman Tim Wan

SAM Local似乎没有运行授权程序功能

  •  0
  • cchapman Tim Wan  · 技术社区  · 5 年前

    我刚开始用 SAM Local ,但在尝试为我的终结点配置授权程序函数时,我又遇到了一个问题。

    我一直在看 main SAM documentation 了解如何设置Auth函数,但是每当我尝试在本地运行API时 sam local start-api ,它运行得很好,但好像它甚至没有尝试运行auth函数。

    我尝试在Global.API中定义Auth,并在SAM的resource s部分中定义API资源 模板.yaml

    # template.yaml
    Globals:
      Function:
        Timeout: 3
        CodeUri: src/
        Runtime: nodejs8.10
      Api:
        Auth:                        # Option #1: Defining it globally
          DefaultAuthorizer: CustomJWTAuthorizer
          Authorizers:
            CustomJWTAuthorizer:
              FunctionArn: !GetAtt AuthFunction.Arn    
    Resources:
      UserApi:
        Auth:                        # Option #2: Defining it as an API resource
          Authorizers:
            MyLambdaTokenAuth:
              FunctionPayloadType: TOKEN
              FunctionArn: !GetAtt AuthFunction.Arn
          DefaultAuthorizer: MyLambdaTokenAuth
      GetUserFunction:
        Type: AWS::Serverless::Function
        Properties:
          Handler: handler.getUser
          Events:
            GetUser:
              Type: Api
              Properties:
                Path: /users/{userId}
                Method: get
                Auth:                    # Option #3: Define it on the function level
                  Authorizer: AuthFunction
                RestApiId:
                    Ref: UserApi
      AuthFunction:
        Type: AWS::Serverless::Function
        Properties:
          Handler: handler.authorize
    

    我试过将事件打印到控制台,可以看到 event.requestContext 只是用虚拟数据填充,而不是在实时推送时传递:

      // console.log(event)
    
      ...  
      resource: '/users/{userId}', 
      requestContext:     { resourceId: '123456',
         apiId: '1234567890',
         resourcePath: '/users/{userId}',
         httpMethod: 'GET',
         requestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
         accountId: '123456789012',
         stage: null,
         identity: 
          { apiKey: null,
            userArn: null,
            cognitoAuthenticationType: null,
            caller: null,
            userAgent: 'Custom User Agent String',
            user: null,
            cognitoIdentityPoolId: null,
            cognitoAuthenticationProvider: null,
            sourceIp: '127.0.0.1',
            accountId: null },
       extendedRequestId: null,
       path: '/users/{userId}' },
       ...
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   Dunedan    5 年前

    不幸的是,当在本地运行代码时,AWS SAM CLI还不支持授权程序。但是,有一个打开的功能请求添加对它的支持: https://github.com/awslabs/aws-sam-cli/issues/137 .

    推荐文章