当我从邮递员调用 api 网关端点并在授权标头中使用值“allow”时,我收到“内部服务器错误”
但是对于“否认”我得到了正确的回应:
对于 lambda 授权者,我使用来自 https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html
的代码从模板中,这是 lambda 授权者部分:
AuthorizerForAPIGW:
Type: AWS::Serverless::Function
Properties:
FunctionName: "authorizer-for-api-gw"
Role: !GetAtt LambdaFuncRoleForAuthorizer.Arn
Handler: src/index.handler
Runtime: nodejs16.x
MemorySize: 320
Timeout: 20
InlineCode: |
exports.handler = function(event, context, callback) {
var token = event.authorizationToken;
switch (token) {
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"); // Return a 500 Invalid token response
}
};
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;
console.log("statement",statementOne);
console.log("policyDocument",policyDocument);
}
authResponse.context = {
"stringKey": "stringval",
"numberKey": 123,
"booleanKey": true
};
return authResponse;
}