如何将 AWS SAM CLI Local HttpAPI 与 JWT Bearer 令牌 Auth 离线使用?

问题描述

我想离线使用 AWS SAM JWT HttpApi Auth

基于 this AWS example,我决定创建以下 YAML 文件

AWstemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs10.x
      Events:
        ExplicitApi: # warning: creates a public endpoint
          Type: HttpApi
          Properties:
            ApiId: !Ref HttpApi
            Method: GET
            Path: /path
            TimeoutInMillis: 15000
            PayloadFormatVersion: "2.0"
            RouteSettings:
              ThrottlingBurstLimit: 600
  HttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      FailOnWarnings: True
      Auth:
        Authorizers:
          MyOauthAuthorizer:
            IdentitySource: $request.header.Authorization
            JwtConfiguration:
              audience:
                - audience
              issuer: issuer-url
        DefaultAuthorizer: MyOauthAuthorizer

使用基于文档的 AWS::Serverless:HttpApi 创建一个支持基于 JWT 的身份验证的 Amazon API Gateway HTTP API。

我以

开头

sam local start-api

但是,当我使用 Postman 查询时,无论是否使用 JWT Bearer 令牌,请求都会成功。
而且 AWS 查询不包含单个经过身份验证的用户对象。

在调试模式下运行它也不会提供任何有用的附加信息。

let response;

exports.lambdaHandler = async (event,context) => {
  try {
    // const ret = await axios(url);
    response = {
      statusCode: 200,body: JSON.stringify({
        message: "hello world",event,context,// location: ret.data.trim()
      }),};
  } catch (err) {
    console.log(err);
    return err;
  }

  return response;
};

我的期望是 AWS SAM CLI 会根据正确提供的颁发者 URL 将不记名令牌转换为我可以在以后的操作中使用的身份值。

AWS SAM Local 在本地运行时是否不支持功能

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)