Kur*_*oZ7 6 node.js aws-lambda aws-api-gateway
我在 AWS API Gateway 上创建了一个 Lambda 授权方,它调用 Lambda 函数。以下是用 Node.js 8.0 代码编写的 Lambda 函数中的代码。
\n\nexports.handler = function(event, context, callback) {\n var token = event.authorizationToken;\n switch (token.toLowerCase()) {\n case \'allow\':\n callback(null, generatePolicy(\'user\', \'Allow\', event.methodArn));\n break;\n case \'deny\':\n callback(null, generatePolicy(\'user\', \'Deny\', event.methodArn));\n break;\n case \'unauthorized\':\n callback("Unauthorized"); // Return a 401 Unauthorized response\n break;\n default:\n callback("Error: Invalid token"); \n }\n};\n\n// Help function to generate an IAM policy\nvar generatePolicy = function(principalId, effect, resource) {\n var authResponse = {};\n\n authResponse.principalId = principalId;\n if (effect && resource) {\n var policyDocument = {};\n policyDocument.Version = \'2012-10-17\'; \n policyDocument.Statement = [];\n var statementOne = {};\n statementOne.Action = \'execute-api:Invoke\'; \n statementOne.Effect = effect;\n statementOne.Resource = resource;\n policyDocument.Statement[0] = statementOne;\n authResponse.policyDocument = policyDocument;\n }\n\n // Optional output with custom properties of the String, Number or Boolean type.\n authResponse.context = {\n "stringKey": "stringval",\n "numberKey": 123,\n "booleanKey": true\n };\n return authResponse;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n(以上示例代码来自网站https://markpollmann.com/lambda-authorizer/)
\n\n如果我通过传递一个无效的authorizationToken值来保存并测试这个函数,我会得到下面的预期结果。
\n\nResponse:\n{\n "errorMessage": "Error: Invalid token"\n}\n\nRequest ID:\n"e93567c0-fcbb-4cb1-b0b3-28e9c1b30162"\nRun Code Online (Sandbox Code Playgroud)\n\n但是,如果我从 Postman 调用此 API,通过传递标头中的值,我会得到以下响应。对于标头中的任何值,我都会收到此错误,即拒绝、允许、未经授权、错误等。
\n\n{\n "message": null\n}\nRun Code Online (Sandbox Code Playgroud)\n\n邮递员中的状态消息显示“500 内部服务器错误”。以下是邮递员中标题部分的详细信息。
\n\ncontent-length \xe2\x86\x9216\ncontent-type \xe2\x86\x92application/json\ndate \xe2\x86\x92Fri, 08 Mar 2019 14:07:57 GMT\nstatus \xe2\x86\x92500\nx-amz-apigw-id \xe2\x86\x92W89kFDRDoEFxYg=\nx-amzn-errortype \xe2\x86\x92AuthorizerConfigurationException\nx-amzn-requestid \xe2\x86\x9292f31d11-41ab-11e9-9c36-97d38d96f31b\nRun Code Online (Sandbox Code Playgroud)\n\n我不明白为什么 API 会返回上述响应和错误消息,而 Lambda 测试却工作正常。
\n\n我已经在 SO 中完成了以下两个线程,但答案/评论对我的情况没有帮助。
\n\n具有自定义授权者的 AWS API Gateway 返回 AuthorizerConfigurationException \n AWS API Gateway 自定义授权者 AuthorizerConfigurationException
\n我已经理解为什么我收到无效输入的 message = null 的原因。switch case 中的默认块在callback() 方法中使用参数“Error: Invalid token”。API Gateway 仅将“允许”、“拒绝”和“未经授权”识别为有效值。这些值也区分大小写。如果将除这些值之外的任何字符串值传递给callback()方法,则API网关将向客户端返回message=null。
| 归档时间: |
|
| 查看次数: |
7295 次 |
| 最近记录: |