|
|
@@ -1,11 +1,24 @@ |
|
|
(function(){
|
|
|
+
|
|
|
+ var crypto = require('crypto');
|
|
|
+ var url = require('url');
|
|
|
+
|
|
|
var SIGNATURE_KEYNAME = "signature";
|
|
|
var SIGNATURE_METHOD_KEYNAME = "signatureMethod";
|
|
|
var SIGNATURE_VERSION_KEYNAME = "signatureVersion";
|
|
|
var HMAC_SHA1_ALGORITHM = "HmacSHA1";
|
|
|
var HMAC_SHA256_ALGORITHM = "HmacSHA256";
|
|
|
var HTTP_GET_METHOD = "GET";
|
|
|
|
|
|
+ var MANDATORY_PARAMS = {
|
|
|
+ all: ["pipelineName","version","returnURL","callerReference"],
|
|
|
+ SingleUse: ["transactionAmount"],
|
|
|
+ Recurring: ["transactionAmount","recurringPeriod"],
|
|
|
+ Recipient: ["maxFixedFee","maxVariableFee","recipientPaysFee"],
|
|
|
+ MultiUse: ["globalAmountLimit"], //TODO there are some other requirements here that should be delt with (usagelimit types, etc)
|
|
|
+ EditToken: ["tokenId"]
|
|
|
+ };
|
|
|
+
|
|
|
module.exports = function(pipelineName, awsAccessKey, awsSecretKey){
|
|
|
/**
|
|
|
* Version parameter for consistent signature for incoming and outgoing requests
|
|
@@ -123,25 +136,103 @@ |
|
|
/**
|
|
|
* Computes RFC 2104-compliant HMAC signature.
|
|
|
*/
|
|
|
- //TODO: FINSIH THIS
|
|
|
function sign(data, key, algorithm) {
|
|
|
- if (algorithm === 'HmacSHA1') {
|
|
|
- $hash = 'sha1';
|
|
|
- } else if ($algorithm === 'HmacSHA256') {
|
|
|
- $hash = 'sha256';
|
|
|
+ var hmac;
|
|
|
+ if (algorithm === HMAC_SHA1_ALGORITHM) {
|
|
|
+ hmac = crypto.createHmac('sha1', key);
|
|
|
+ } else if (algorithm === HMAC_SHA256_ALGORITHM) {
|
|
|
+ hmac = crypto.createHmac('sha256', key);
|
|
|
} else {
|
|
|
- throw new Exception ("Non-supported signing method specified");
|
|
|
+ throw "Non-supported signing method specified";
|
|
|
}
|
|
|
- return base64_encode(
|
|
|
- hash_hmac($hash, $data, $key, true)
|
|
|
- );
|
|
|
+ hmac.update(data);
|
|
|
+ return hmac.digest('base64');
|
|
|
+ //php: l1aQhiVCfR2L0Q9t1Nt1HRa7tF0= <== Yes this sign is identical to the php version
|
|
|
+ //node:l1aQhiVCfR2L0Q9t1Nt1HRa7tF0=
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Construct the pipeline request url using given parameters.
|
|
|
+ * Computes signature and adds it as additional parameter.
|
|
|
+ * @param parameters - Map of pipeline request parameters.
|
|
|
+ * @return Returns the pipeline request url.
|
|
|
+ * @throws MalformedURLException
|
|
|
+ * @throws SignatureException
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
+ */
|
|
|
+ function constructUrl($parameters) {
|
|
|
+ if(!parameters){
|
|
|
+ throw "Parameters can not be empty.";
|
|
|
+ }
|
|
|
+ var hostHeader = getHostHeader(CBUI_URL);
|
|
|
+ var requestURI = getRequestURI(CBUI_URL);
|
|
|
+ var signature = this.signParameters(parameters, HTTP_GET_METHOD, hostHeader, requestURI);
|
|
|
+ parameters["signature"] = signature;
|
|
|
+ return CBUI_URL + "?" + buildQueryString(parameters);
|
|
|
}
|
|
|
+
|
|
|
+ function getHostHeader(endPoint) {
|
|
|
+ var theurl = url.parse(endPoint);
|
|
|
+ var host = theurl.host.toLowerCase();
|
|
|
+ var protocol = theurl.protocol.toUpperCase();
|
|
|
+ if(theurl.hasOwnProperty('port')) {
|
|
|
+ if (("HTTPS" == protocol && theurl.port != 443) || ("HTTP" == protocol && theurl.port != 80)) {
|
|
|
+ return host + ":" + theurl.port;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return host;
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
+ function getRequestURI(endPoint) {
|
|
|
+ var theurl = url.parse(endPoint);
|
|
|
+ var requestURI = '/';
|
|
|
+ if(theurl.hasOwnProperty('pathname')){
|
|
|
+ requestURI = theurl.pathname;
|
|
|
+ }
|
|
|
+ return requestURI;
|
|
|
+ }
|
|
|
+
|
|
|
+ function validateCommonMandatoryParameters(parameters) {
|
|
|
+ if (!parameters.hasOwnProperty("pipelineName")){
|
|
|
+ throw "pipelineName is missing in parameters.";
|
|
|
+ }
|
|
|
+ if (!parameters.hasOwnProperty("version")){
|
|
|
+ throw "version is missing in parameters.";
|
|
|
+ }
|
|
|
+ if (!parameters.hasOwnProperty("returnURL")){
|
|
|
+ throw "returnURL is missing in parameters.";
|
|
|
+ }
|
|
|
+ if (!parameters.hasOwnProperty("callerReference")){
|
|
|
+ throw "callerReference is missing in parameters.";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ abstract protected function validateParameters($parameters);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Constructs the query string for the parameters added to this class
|
|
|
+ *
|
|
|
+ * This function also calculates the signature of the all the name value pairs
|
|
|
+ * added to the class.
|
|
|
+ *
|
|
|
+ * @return string URL
|
|
|
+ */
|
|
|
+ public function getURL() {
|
|
|
+ $this->validateCommonMandatoryParameters($this->parameters);
|
|
|
+ $this->validateParameters($this->parameters);
|
|
|
+ return $this->constructUrl($this->parameters);
|
|
|
+ }
|
|
|
|
|
|
//////////////////////////////////////////////
|
|
|
// UTILS //
|
|
|
//////////////////////////////////////////////
|
|
|
+
|
|
|
+ function buildQueryString(params){
|
|
|
+ Object.keys(params).map(function(p){
|
|
|
+ return escape(p) + "=" + escape(params[p]) + '&';
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Convert paremeters to Url encoded query string
|
|
|
*/
|
|
@@ -158,10 +249,8 @@ |
|
|
return (casesensitive? a.key : a.key.toLowerCase()) > (casesensitive? b.key : b.key.toLowerCase());
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
|
|
|
+ }//end of the class
|
|
|
|
|
|
+
|
|
|
})();
|
0 comments on commit
f08e0f5