...clude
Script include -
var refinitivWCUtils = Class.create();
refinitivWCUtils.prototype = {
initialize: function() {},
getGroupsAuthHeader: function() {
// Retrieve environment properties
var environment = {
"gateway-url": gs.getProperty('x_cbre2_generic_ap.refinitiv.gateway_url'),
"gateway-host": gs.getProperty('x_cbre2_generic_ap.refinitiv.gateway_host'),
"content": "application/json",
"api-key": gs.getProperty('x_cbre2_generic_ap.refinitiv.api_key')
};
// Manually set the date, this can be dynamically generated as well
var dateSent = new Date().toUTCString(); // Generates the current UTC date string
// Construct the dataToHash string
var dataToHash = "(request-target): get " + environment["gateway-url"] + "groups\n" +
"host: " + environment["gateway-host"] + "\n" +
"date: " + dateSent + "\n" +
"content-type: " + environment["content"];
// Generate HMAC
var hmac = this._generateHash(dataToHash);
// Create the Authorization header
var authHeader = "Signature keyId=\"" + environment["api-key"] + "\",algorithm=\"hmac-sha256\",headers=\"(request-target) host date content-type\",signature=\"" + hmac + "\"";
// Log debugging information
var debug = {
"environment": environment,
"dataToHash": dataToHash,
"hmac": hmac
};
gs.info('Generated Groups Auth Header Debug: ' + JSON.stringify(debug));
return authHeader;
},
getSimpleScreeningRequestAuthHeader: function(dateSent, payload) {
// Calculate content length from payload
var contentLength = unescape(encodeURIComponent(payload)).length;
// Retrieve environment properties
var environment = {
"gateway-url": gs.getProperty('x_cbre2_generic_ap.refinitiv.gateway_url'),
"gateway-host": gs.getProperty('x_cbre2_generic_ap.refinitiv.gateway_host'),
"content": "application/json",
"api-key": gs.getProperty('x_cbre2_generic_ap.refinitiv.api_key')
};
// Construct the dataToHash string
var dataToHash = "(request-target): post " + environment["gateway-url"] + "cases/screeningRequest\n" +
"host: " + environment["gateway-host"] + "\n" +
"date: " + dateSent + "\n" +
"content-type: " + environment["content"] + "\n" +
"content-length: " + contentLength + "\n" +
payload;
// Generate HMAC
var hmac = this._generateHash(dataToHash);
// Create the Authorization header
var authHeader = "Signature keyId=\"" + environment["api-key"] + "\",algorithm=\"hmac-sha256\",headers=\"(request-target) host date content-type content-length\",signature=\"" + hmac + "\"";
// Log debugging information
var debug = {
"environment": environment,
"dataToHash": dataToHash,
"hmac": hmac
};
gs.info('Generated Simple Screening Request Auth Header Debug: ' + JSON.stringify(debug));
return authHeader;
},
// Method to generate HMAC-SHA256 hash and convert it to Base64
_generateHash: function(dataToHash) {
var hmacUtil = new x_cbre2_generic_ap.CryptoJS_Wrapper();
var secret = gs.getProperty('x_cbre2_generic_ap.refinitiv.api_secret');
// Generate HMAC-SHA256 hash
var hash = hmacUtil.HmacSHA256(dataToHash, secret);
// Convert hash to Base64
return hmacUtil.convertToBase64(hash);
},
type: 'refinitivWCUtils'
};
Need CryptoJS_Wrapper script include to convert the hmac signature and generate the token and return it.
Code to call:
var dateToday = new Date().toGMTString();
var authHeaderString = new x_cbre2_generic_ap.refinitivWCUtils().getGroupsAuthHeader();
try {
var endPoint = "https://api-worldcheck.refinitiv.com/v2/groups";
r = new sn_ws.RESTMessageV2('World check one', 'Default GET');
r.setStringParameterNoEscape('authorizarionString', authHeaderString);
r.setStringParameterNoEscape('currentDate', dateToday);
r.setStringParameterNoEscape('endPoint', endPoint);
// r.setStringParameterNoEscape('contentLength', contentLength);
//r.setStringParameter('payload', restBody);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var debug = {
"date": dateToday,
"endPoint": endPoint,
"httpStatus": httpStatus
}
gs.print(JSON.stringify(debug));
gs.print('authHeaderString is: ' + authHeaderString);
}
catch(ex) {
var message = ex.message;
gs.print('error caught, message: ' + message);
}