【问题标题】:AWS authorizer returns 500, message: null, with AuthorizerConfigurationException error in responseAWS 授权方返回 500,消息:null,响应中有 AuthorizerConfigurationException 错误
【发布时间】:2022-05-06 01:48:57
【问题描述】:

我今天大部分时间都在尝试让授权者工作,我检查了多个示例,它们似乎都在做我的代码所做的同样的事情。

我使用无服务器框架,这是授权码:

exports.handler = function (event: APIGatewayTokenAuthorizerEvent): APIGatewayAuthorizerResult {
    const authorizer = new Authorizer();

    try {
        if (!event.authorizationToken) throw new Error("No token");

        const token = event.authorizationToken.split(" ")[1];
        const decodedData = authorizer.verifyToken(token) as unknown as User;
        const policy = generatePolicy(token, event.methodArn);

        return {
            ...policy,
            context: {
                user: JSON.stringify(decodedData),
            },
        };
    } catch (err) {
        console.log(err);
        throw "Unauthorized";
    }
};

const generatePolicy = (principalId: string, methodArn: string) => {
    return {
        principalId,
        policyDocument: {
            Version: "2012-10-17",
            Statement: [
                {
                    Action: "execute-api:Invoke",
                    Effect: "Allow",
                    Resource: methodArn,
                },
            ],
        },
    };
};

这是无服务器配置

const serverlessConfiguration: AWS = {
service: "user-crud",
frameworkVersion: "2",
custom: {
    webpack: {
        webpackConfig: "./webpack.config.js",
        includeModules: true,
    },
},
plugins: ["serverless-webpack"],
provider: {
    name: "aws",
    runtime: "nodejs14.x",
    region: "eu-west-1",
    apiGateway: {
        minimumCompressionSize: 1024,
        shouldStartNameWithService: true,
    },
    environment: {
        AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1",
    },
    lambdaHashingVersion: "20201221",
},

functions: {
    jwtAuthorizer: {
        handler: "src/api/authorizer.handler",
        name: "jwtAuthorizer",
    },
    get: {
        name: "get",
        handler: "src/api/get.handler",
        role: "arn:aws:iam::109394173706:role/dynamodb_cloudwatch_full",
        events: [
            {
                http: {
                    path: "get",
                    method: "get",
                    cors: true,
                    authorizer: "jwtAuthorizer",
                },
            },
        ],
    },

}...

当令牌正确并且我返回对象时,我总是得到 500 响应,所以我猜返回对象有问题?

如果令牌不正确并且我抛出“未授权”,那么我会得到正确的 401 响应。

【问题讨论】:

    标签: javascript node.js amazon-web-services lambda-authorizer aws-jwt-authorizer


    【解决方案1】:

    显然,处理程序需要是异步的,否则,它需要一个回调...时间花得值:|

    【讨论】:

      【解决方案2】:

      嗯,还有一些其他的原因,

      {
          message : null
      }
      

      来自 Api 网关的错误。我很难确定我的身份。

      1. API 网关可能缺乏调用授权方 lambda 的权限。确保为您的授权人添加Lambda Invoke Role
      2. 您不应该修改请求上下文 (不是在谈论响应上下文)。每当我尝试时,我都会在 Api 网关日志中得到 Execution failed due to configuration error: Invalid JSON in response: Unrecognized field "headers", not marked as ignorable。您必须通过添加 cors 并明确提及这些标头来解决它
      3. 退货政策应符合 Lambda 响应 1.0 版本。如果要使用基于boolean 的返回,则必须启用 lambda response 2.0
      4. 如果你更新了授权者lambda函数名什么的,最好deleteRestApiGateway重新创建。更改有时可能无法在 CloudFront 中正确更新。
      5. 您提供的响应上下文将只允许字符串、数字或布尔类型。例如,

          function allowPolicy(methodArn) {
              console.log("Allow Policy")
              return {
                  "principalId": "apigateway.amazonaws.com",
                  "policyDocument": {
                      "Version": "2012-10-17",
                      "Statement": [
                          {
                              "Action": "execute-api:Invoke",
                              "Effect": "Allow",
                              "Resource": methodArn
                          }
                      ]
                  },
                  "context": {
                      "stringValue": "blablabla",
                      "numberValue": 10,
                      "booleanValue": true,
                  }
              };
          }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-01
        • 2020-06-17
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多