From 1a16b60439331c8c9963900de6a3c0087492227c Mon Sep 17 00:00:00 2001 From: Sam Heslop Date: Wed, 15 Aug 2018 14:13:54 +0100 Subject: [PATCH] Return methodType and encryptAsObject (#12) * Return methodType='paymentMethodCipher' * Provide both encrypt and encryptToObject --- package.json | 3 ++- ravelin.js | 27 ++++++++++++++++++++------- test/test.js | 22 +++++++++++++++++++++- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index e8ac8ff..4e33604 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,11 @@ { "name": "ravelinjs", - "version": "0.0.8", + "version": "0.0.9", "description": "Ravelin Browser Card Encryption Library.", "main": "ravelin.js", "scripts": { "minify": "uglifyjs --support-ie8 -o ravelin.min.js ravelin.js", + "minify-latest": "uglifyjs --support-ie8 -o ravelin.min.js ravelin.js && cp ravelin.min.js ravelin-$(node -e 'console.log(require(\"./package.json\").version)').min.js", "test": "mocha -C test/test.js", "test-appium": "npm run minify && wdio ./test/appium.conf.js", "test-crossbrowser": "npm run minify && wdio ./test/crossbrowser.conf.js", diff --git a/ravelin.js b/ravelin.js index 078ca3c..464d99a 100644 --- a/ravelin.js +++ b/ravelin.js @@ -9,7 +9,7 @@ } }(typeof self !== 'undefined' ? self : this, function () { - var version = '0.0.8'; + var version = '0.0.9'; var RSAKey = (function(){ // prng4.js - uses Arcfour as a PRNG @@ -1033,14 +1033,14 @@ } /** - * encrypt takes the encrypted card details and prepares them to be sent - * to Ravelin. + * encryptAsObject takes the encrypted card details and prepares them to be sent + * to Ravelin, with the resulting payload returned as an object. * * @param {Object} details An object containing properties pan, month, year, * month, and nameOnCard (optional). - * @return {String} The encrypted paylaod to be sent to Ravelin. + * @return {Object} The encrypted payload to be sent to Ravelin. */ - RavelinJS.prototype.encrypt = function(details) { + RavelinJS.prototype.encryptAsObject = function(details) { if (!this.rsaKey) { throw new Error("RavelinJS Key has not been set"); } @@ -1091,14 +1091,27 @@ var aesResult = aesEncrypt(JSON.stringify(details)); var rsaResultB64 = rsaEncrypt(this.rsaKey, aesResult.aesKeyB64, aesResult.ivB64); - return JSON.stringify({ + return { + methodType: 'paymentMethodCipher', cardCiphertext: aesResult.ciphertextB64, aesKeyCiphertext: rsaResultB64, algorithm: "RSA_WITH_AES_256_GCM", ravelinjsVersion: version, - }); + }; }; + /** + * encrypt takes the encrypted card details and prepares them to be sent + * to Ravelin, with the resulting payload returned as a string. + * + * @param {Object} details An object containing properties pan, month, year, + * month, and nameOnCard (optional). + * @return {String} The encrypted payload to be sent to Ravelin. + */ + RavelinJS.prototype.encrypt = function(details) { + return JSON.stringify(this.encryptAsObject(details)); + } + if ((typeof window !== 'undefined' && window.addEventListener) || (typeof document !== 'undefined' && document.attachEvent)) { sjcl.random.startCollectors(); } diff --git a/test/test.js b/test/test.js index 3aae5c9..e5fb9cf 100644 --- a/test/test.js +++ b/test/test.js @@ -57,10 +57,11 @@ describe('ravelinjs', function() { function validCipher(c) { c = JSON.parse(c); return ( + c.methodType == 'paymentMethodCipher' && c.cardCiphertext != "" && c.cardCiphertext.length > 10 && c.aesKeyCiphertext != "" && c.aesKeyCiphertext.length > 10 && c.algorithm == "RSA_WITH_AES_256_GCM" && - c.ravelinjsVersion == "0.0.8" + c.ravelinjsVersion == "0.0.9" ); } @@ -82,6 +83,25 @@ describe('ravelinjs', function() { year: "20", })).to.satisfy(validCipher); }); + + it('can return payload as object', function() { + ravelin.setRSAKey(dummyRSAKey); + function validCipher(c) { + return ( + c.methodType == 'paymentMethodCipher' && + c.cardCiphertext != "" && c.cardCiphertext.length > 10 && + c.aesKeyCiphertext != "" && c.aesKeyCiphertext.length > 10 && + c.algorithm == "RSA_WITH_AES_256_GCM" && + c.ravelinjsVersion == "0.0.9" + ); + } + + expect(ravelin.encryptAsObject({ + pan: '4111 1111 1111 1111', + month: 10, + year: 2020, + })).to.satisfy(validCipher); + }); }); });