我在AWS API Gateway上创建了Lambda Authorizer,它调用Lambda函数。以下是用Node.js 8.0代码编写的Lambda函数中的代码。
exports.handler = function(event, context, callback) {
var token = event.authorizationToken;
switch (token.toLowerCase()) {
case 'allow':
callback(null, generatePolicy('user', 'Allow', event.methodArn));
break;
case 'deny':
callback(null, generatePolicy('user', 'Deny', event.methodArn));
break;
case 'unauthorized':
callback("Unauthorized"); // Return a 401 Unauthorized response
break;
default:
callback("Error: Invalid token");
}
};
// Help function to generate an IAM policy
var generatePolicy = function(principalId, effect, resource) {
var authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
var policyDocument = {};
policyDocument.Version = '2012-10-17';
policyDocument.Statement = [];
var statementOne = {};
statementOne.Action = 'execute-api:Invoke';
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
// Optional output with custom properties of the String, Number or Boolean type.
authResponse.context = {
"stringKey": "stringval",
"numberKey": 123,
"booleanKey": true
};
return authResponse;
}(以上示例代码来自网站https://markpollmann.com/lambda-authorizer/)
如果我通过为authorizationToken传递一个无效值来保存和测试这个函数,我会得到如下所示的预期结果。
Response:
{
"errorMessage": "Error: Invalid token"
}
Request ID:
"e93567c0-fcbb-4cb1-b0b3-28e9c1b30162"但是,如果我从Postman调用此API,通过在头中传递该值,我会得到以下响应。对于头部中的任何值,例如,拒绝、允许、未授权、错误等,我都会收到这个错误。
{
"message": null
}postman中的状态消息显示"500内部服务器错误“。以下是postman中标题部分的详细信息。
content-length →16
content-type →application/json
date →Fri, 08 Mar 2019 14:07:57 GMT
status →500
x-amz-apigw-id →W89kFDRDoEFxYg=
x-amzn-errortype →AuthorizerConfigurationException
x-amzn-requestid →92f31d11-41ab-11e9-9c36-97d38d96f31b我不明白为什么API返回上面的响应和错误消息,而Lambda测试工作正常。
我已经在SO中通过了以下两个线程,但答案/评论对我的情况没有帮助。
AWS API Gateway with custom authorizer returns AuthorizerConfigurationException AWS API Gateway Custom Authorizer AuthorizerConfigurationException
发布于 2019-03-10 00:27:05
我已经理解了为什么无效输入会得到message = null的原因。切换用例中的缺省块是在callback()方法中使用参数"Error: Invalid token“。API网关仅将Allow、Deny和Unauthorized标识为有效值。这些值也区分大小写。如果将这些值以外的任何字符串值传递给callback()方法,则API Gateway将向客户端返回message=null。
https://stackoverflow.com/questions/55064760
复制相似问题