首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在node.js中,Lambda授权程序未使用回调()返回正确的错误消息

在node.js中,Lambda授权程序未使用回调()返回正确的错误消息
EN

Stack Overflow用户
提问于 2019-03-08 21:59:16
回答 1查看 4K关注 0票数 3

我在AWS API Gateway上创建了Lambda Authorizer,它调用Lambda函数。以下是用Node.js 8.0代码编写的Lambda函数中的代码。

代码语言:javascript
复制
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传递一个无效值来保存和测试这个函数,我会得到如下所示的预期结果。

代码语言:javascript
复制
Response:
{
  "errorMessage": "Error: Invalid token"
}

Request ID:
"e93567c0-fcbb-4cb1-b0b3-28e9c1b30162"

但是,如果我从Postman调用此API,通过在头中传递该值,我会得到以下响应。对于头部中的任何值,例如,拒绝、允许、未授权、错误等,我都会收到这个错误。

代码语言:javascript
复制
{
    "message": null
}

postman中的状态消息显示"500内部服务器错误“。以下是postman中标题部分的详细信息。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-10 00:27:05

我已经理解了为什么无效输入会得到message = null的原因。切换用例中的缺省块是在callback()方法中使用参数"Error: Invalid token“。API网关仅将Allow、Deny和Unauthorized标识为有效值。这些值也区分大小写。如果将这些值以外的任何字符串值传递给callback()方法,则API Gateway将向客户端返回message=null。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55064760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档