DynamoDB在HTTP.post

时间:2018-12-12 00:09:09

标签: javascript amazon-dynamodb

不知道代码在做什么,但是我正在使用Playfab Cloud Scripts,它本质上只是Javascript,您无法在外部库中引用它。我已经包含了此https://gist.github.com/ryanjduffy/4686906 < / p>

能够进行加密,但是签名有问题。我已确保多次使用更正后的AccessKey和SecretKey。我唯一的想法是我在某处错误地生成了签名。

function getSignatureKey(key, dateStamp, regionName, serviceName) {
var kDate = HMAC_SHA256_MAC(dateStamp, "AWS4" + key);
var kRegion = HMAC_SHA256_MAC(regionName, kDate);
var kService = HMAC_SHA256_MAC(serviceName, kRegion);
var kSigning = HMAC_SHA256_MAC("aws4_request", kService);
return kSigning;
}

handlers.DynamoRequest = function (args, context) {
   var method = "POST";
   var service = "dynamodb";
   var host = "dynamodb.us-east-2.amazonaws.com";
var region = "us-east-2";
var target = "DynamoDB_20120810.PutItem";
var contentType = "application/json";
var signedHeaders = "content-type;host;x-amz-date;x-amz-target";
var algorithm = "AWS4-HMAC-SHA256";
var accessKey = "keyInsertedHere";
var secretKey = "keyInsertedHere";

var date = new Date().toISOString();
date = date.split("-").join("");
date = date.split(":").join("");
date = date.substring(0, 15);
date = date + "Z";

var body = {
"TableName": "PlayerItems",
"Key": {
"PlayerID": {"S": "id1"}
}
};

body = JSON.stringify(body);

var canonicalHeaders = 'content-type:' + contentType + '\n' + 'host:' + host + '\n' + 'x-amz-date:' + date + '\n' + 'x-amz-target:' + target + '\n';

var payloadHash = SHA256_hash(body);
var canonicalRequest = method + "\n" + "/" + "\n" + "" + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + payloadHash;

var credentialScope = date.substring(0, 8) + "/" + region + "/" + service + "/" + "aws4_request";
var stringToSign = algorithm + "\n" +  date + "\n" +  credentialScope + "\n" +  SHA256_hash(canonicalRequest);
var signingKey = getSignatureKey(secretKey, date.substring(0, 8), region, service); 
var signature = HMAC_SHA256_MAC(signingKey, stringToSign);
var authHeader = algorithm + " " + "Credential=" + accessKey + "/" + credentialScope + ", " +  "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;

var headers = {
"Content-Type": contentType,
"Host": host,
"X-Amz-Target": target,
"X-Amz-Date": date,
"Authorization": authHeader
};

try{
var response = http.request("https://" + host, method, body, contentType, headers, true);   
return { responseContent: response };
}
catch(e){
return {error: e};
}
};

0 个答案:

没有答案