From 0d6a24cf2908603254228fb0c099e07cc8ef4b82 Mon Sep 17 00:00:00 2001 From: janfabian Date: Thu, 23 Apr 2020 15:37:12 +0200 Subject: [PATCH] verification errors --- package-lock.json | 2 +- package.json | 2 +- payment/config/v1.8.js | 2 +- payment/crypto.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index b61e325..5592e0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@topmonks/csob-paymentgate", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a2687f6..36024e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@topmonks/csob-paymentgate", - "version": "0.2.0", + "version": "0.2.1", "description": "", "main": "index.js", "scripts": { diff --git a/payment/config/v1.8.js b/payment/config/v1.8.js index 3b1cc90..f9eb620 100644 --- a/payment/config/v1.8.js +++ b/payment/config/v1.8.js @@ -32,7 +32,7 @@ const testConfig = { csobPublicKey: fs.readFileSync( path.resolve( __dirname, - "../../paymentgateway/keys/mips_platebnibrana.csob.cz.pub" + "../../paymentgateway/keys/mips_iplatebnibrana.csob.cz.pub" ), { encoding: "utf8" } ), diff --git a/payment/crypto.js b/payment/crypto.js index 49cf49e..9c2a56c 100644 --- a/payment/crypto.js +++ b/payment/crypto.js @@ -9,6 +9,19 @@ const sign = (text, privateKey, hashFn, encoding) => { return signer.sign(privateKey, encoding); }; +class VERIFICATION_ERROR extends Error { + constructor(message) { + super(message); + + this.name = this.constructor.name; + this.status = 400; + } + + statusCode() { + return this.status; + } +} + const verify = (text, signature, publicKey, hashFn, encoding) => { const verifier = crypto.createVerify(hashFn); verifier.update(text); @@ -29,7 +42,21 @@ const verifyResponse = ( optional: [...optional, noPayId ? "payId" : undefined], }); - return verify(text, response.signature, csobPublicKey, hashFn, encoding); + const verification = verify( + text, + response.signature, + csobPublicKey, + hashFn, + encoding + ); + + if (!verification) { + throw new VERIFICATION_ERROR( + "CSOB response signature verification failed." + ); + } + + return verification; }; const dttm = () => moment().format("YYYYMMDDHHmmss");