Apigateway aws 服务的授权方请求问题

问题描述

我正在尝试使用 Lambda (node.js) 为 websocket 创建一个简单的请求授权器,但我不断收到错误 500。我使用的代码是 aws 文档中提供的演示。我调用的 url wss://*****/production?QueryString1=queryValue1

我已经在 apigateway 上创建了一个请求授权器并遵循了 aws apigateway 文档中的所有说明,但仍然得到未经授权的响应或 500?

exports.handler = function(event,context,callback) {        
console.log('Received event:',JSON.stringify(event,null,2));

// A simple request-based authorizer example to demonstrate how to use request 
// parameters to allow or deny a request. In this example,a request is  
// authorized if the client-supplied headerauth1 header,QueryString1
// query parameter,and stage variable of StageVar1 all match
// specified values of 'headerValue1','queryValue1',and 'stageValue1',// respectively.

// Retrieve request parameters from the Lambda function input:
var headers = event.headers;
var queryStringParameters = event.queryStringParameters;
var pathParameters = event.pathParameters;
var stageVariables = event.stageVariables;
    
// Parse the input for the parameter values
var tmp = event.methodArn.split(':');
var apiGatewayArnTmp = tmp[5].split('/');
var awsAccountId = tmp[4];
var region = tmp[3];
var restApiId = apiGatewayArnTmp[0];
var stage = apiGatewayArnTmp[1];
var method = apiGatewayArnTmp[2];
var resource = '/'; // root resource
if (apiGatewayArnTmp[3]) {
    resource += apiGatewayArnTmp[3];
}
    
// Perform authorization to return the Allow policy for correct parameters and 
// the 'Unauthorized' error,otherwise.
var authResponse = {};
var condition = {};
condition.IpAddress = {};
 
if (//headers.headerauth1 === "headerValue1"
    queryStringParameters.QueryString1 === "queryValue1"
    //&& stageVariables.StageVar1 === "stageValue1"
    ){
    callback(null,generateAllow('me',event.methodArn));
}  else {
    callback("Unauthorized");
}
}
 
// Help function to generate an IAM policy
var generatePolicy = function(principalId,effect,resource) {
// Required output:
var authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
    var policyDocument = {};
    policyDocument.Version = '2012-10-17'; // default version
    policyDocument.Statement = [];
    var statementOne = {};
    statementOne.Action = 'execute-api:Invoke'; // default action
    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;
}
 
var generateAllow = function(principalId,resource) {
return generatePolicy(principalId,'Allow',resource);
}
 
var generateDeny = function(principalId,'Deny',resource);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...