-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from alma/feature/ecom-1820-sfcc-add-hmac-ver…
…ification-on-ipn Add hmac verification on IPN
- Loading branch information
Showing
5 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
cartridges/int_alma/cartridge/scripts/helpers/almaSecurityHelper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
var Mac = require('dw/crypto/Mac'); | ||
var Encoding = require('dw/crypto/Encoding'); | ||
|
||
|
||
/** | ||
* Check is the IPN signature is valid | ||
* @param {string|null} almaSignature signature to check | ||
* @param {string} paymentId Paymewnt id | ||
* @param {string} key key to check the signature | ||
* @throws Error | ||
*/ | ||
function checkIpnSignature(almaSignature, paymentId, key) { | ||
if (!almaSignature) { | ||
throw new Error('There is no signature in header'); | ||
} | ||
|
||
var mac = new Mac(Mac.HMAC_SHA_256); | ||
var hmac = mac.digest(paymentId, key); | ||
var hmacHex = Encoding.toHex(hmac); | ||
|
||
if (hmacHex !== almaSignature) { | ||
throw new Error('Signature is not valid'); | ||
} | ||
} | ||
|
||
module.exports = { | ||
checkIpnSignature: checkIpnSignature | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
var proxyquire = require('proxyquire') | ||
.noCallThru() | ||
.noPreserveCache(); | ||
|
||
function mac() { | ||
return { | ||
digest: function () { | ||
return 'good_byte_signature'; | ||
} | ||
}; | ||
} | ||
|
||
var encoding = { | ||
toHex: function () { | ||
return '4545854d3b8704d4b21cf88bc8b5da5680c46b2ab9d45c8cffe6278d8a8b1860'; | ||
} | ||
}; | ||
|
||
function proxyModel() { | ||
return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaSecurityHelper', { | ||
'dw/crypto/Mac': mac, | ||
'dw/crypto/Encoding': encoding | ||
}); | ||
} | ||
|
||
module.exports = { | ||
almaSecurityHelper: proxyModel() | ||
}; |
30 changes: 30 additions & 0 deletions
30
test/unit/int_alma/scripts/helpers/almaSecurityHelperTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
// almaSecurityHelper.js unit tests | ||
|
||
var assert = require('chai').assert; | ||
var almaSecurityHelper = require('../../../../mocks/helpers/almaSecurityHelper').almaSecurityHelper; | ||
var PAYMENT_ID = 'payment_id_test'; | ||
var API_KEY = 'api_key_test'; | ||
var BAD_SIGNATURE = 'bad_signature'; | ||
var GOOD_SIGNATURE = '4545854d3b8704d4b21cf88bc8b5da5680c46b2ab9d45c8cffe6278d8a8b1860'; | ||
|
||
describe('Alma security helper', function () { | ||
it('checkIpnSignature throw error without signature in header', function () { | ||
assert.throw(function () { | ||
almaSecurityHelper.checkIpnSignature(null, PAYMENT_ID, API_KEY); | ||
}, 'There is no signature in header'); | ||
}); | ||
|
||
it('checkIpnSignature throw error with bad signature', function () { | ||
assert.throw(function () { | ||
almaSecurityHelper.checkIpnSignature(BAD_SIGNATURE, PAYMENT_ID, ''); | ||
}, 'Signature is not valid'); | ||
}); | ||
|
||
it('checkIpnSignature not throw error with good signature', function () { | ||
assert.doesNotThrow(function () { | ||
almaSecurityHelper.checkIpnSignature(GOOD_SIGNATURE, PAYMENT_ID, API_KEY); | ||
}); | ||
}); | ||
}); |