I am trying to call a Rest API (Logic Monitor API), that requires Token Authentication from the client side. But the authentication requires a signature, that is made up of Access ID, a base64 encoded HMAC signature based on the API Token access key and a Timestamp.
Ref: https://www.logicmonitor.com/support/rest-api-developers-guide/overview/using-logicmonitors-rest-api
The only example I found was in Node JS which imports modules like Crypto and request. Since I am trying to do it from the client-end, I will not be able to use Node JS. I tried using javaScript libraries like Crypto-JS
CDN: https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js", but it didn't work.
Below is my Javascript code:
var accessId = '12312312312';
var accessKey = '12324566767';
var company = 'api'
// Request Details
var httpVerb = "GET";
var epoch = (new Date).getTime();
var resourcePath = "/device/groups";
// Construct signature
var requestVars = httpVerb + epoch + resourcePath;
var hash = CryptoJS.HmacSHA256(requestVars, accessKey);
var signature = btoa(hash);
var auth = "LMv1 " + accessId + ":" + signature + ":" + epoch;
console.log(auth);
$.ajax({
url: 'https://' + company + '.logicmonitor.com/santaba/rest' + resourcePath,
headers: {
'Content-Type': 'application/json',
'Authorization': auth
},
type: "GET",
dataType: "json",
data: {},
success: function(result) {
console.log(result);
},
error: function(error) {
console.log(error);
}
});
Update 1: The Node JS example has the header set as below:
var options =
{
"method" : httpVerb,
"uri" : "https://" + company + ".logicmonitor.com/santaba/rest" + resourcePath,
"headers": {
'ContentType' : 'application/json',
'Authorization': auth //formed by the crypto authentication
}
};
// Make request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Print out the response body
console.log(body)
}
});