目前,我设法将其解码base64
并重新编码为hex
,这不是最干净的方法,但在提供额外功能之前,这可能会帮助遇到类似问题的人。可以调整该AuthenticationHeaderDynamicValue
功能以执行您需要执行的任何操作。
(function() {
var request,
url,
md5 = function (input) {
return DynamicValue('com.luckymarmot.HashDynamicValue', {
hashType: 2,
input: input
}).getEvaluatedString();
},
hmac = function (key, input) {
return DynamicValue('com.luckymarmot.HMACDynamicValue', {
algorithm: 3,
input: input,
key: key
}).getEvaluatedString();
},
base64Decode = function (input) {
var a,
b = 0,
c,
e = {},
loop = 0,
result = '';
for (var i = 0; i < 64; i++) {
e['ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.charAt(i)] = i;
}
for (var x = 0; x < input.length; x++) {
c = e[input.charAt(x)];
b = (b << 6) + c;
loop += 6;
while (loop >= 8) {
(( a = (b >>> (loop -= 8)) &0xff ) || ( x < (input.length - 2))) && ( result += String.fromCharCode(a));
}
}
return result;
},
recode = function (input) {
var decodedString = base64Decode(input),
recodedString = '';
// Encode binary to hex
for (var i = 0; i < decodedString.length; i++) {
var hex = decodedString.charCodeAt(i).toString(16);
recodedString += ('000' + hex).slice(-2);
}
return recodedString;
},
AuthenticationHeaderDynamicValue = function () {
this.computeString = function (context) {
request = context.getCurrentRequest();
url = request.getUrl(true).components[1];
var time = Math.round(+new Date() / 1000),
md5Checksum = md5(request.jsonBody ? JSON.stringify(request.jsonBody) : url),
signature = hmac(this.secretKey, md5Checksum);
return recode(signature);
};
this.evaluate = function(context) {
if (this.secretKey) {
return this.computeString(context);
}
};
this.title = function() {
return "Authentication header";
};
this.text = function() {
return;
};
};
AuthenticationHeaderDynamicValue.identifier = "com.test.PawExtensions.AuthenticationHeaderDynamicValue";
AuthenticationHeaderDynamicValue.title = "AuthenticationHeader";
AuthenticationHeaderDynamicValue.inputs = [
DynamicValueInput("publicKey", "Public key", "String"),
DynamicValueInput("secretKey", "Secret key", "String")
];
registerDynamicValueClass(AuthenticationHeaderDynamicValue);
}).call(this);