Skip to content

Commit

Permalink
feat: update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nanov committed Oct 11, 2023
1 parent 66bf57f commit a03ec22
Show file tree
Hide file tree
Showing 7 changed files with 2,039 additions and 6,934 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
node_js:
- "8"
- "9"
- "10"
- "11"
- "12"
- "16" # maintance lts
- "17"
- "18" # active lts
- "19"
- "20" # current
after_success: npm run coverage
8 changes: 4 additions & 4 deletions lib/middleware/signer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
"use strict";

const H004Signer = require('../orders/H004/signer');
const H004Signer = require("../orders/H004/signer");

module.exports = {
version(v) {
if (v.toUpperCase() === 'H004') return H004Signer;
if (v.toUpperCase() === "H004") return H004Signer;

throw Error('Error from middleware/signer.js: Invalid version number');
throw Error("Error from middleware/signer.js: Invalid version number");
},
};
119 changes: 79 additions & 40 deletions lib/orders/H004/response.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
"use strict";

const zlib = require('zlib');
const crypto = require('crypto');
const zlib = require("zlib");
const crypto = require("crypto");

const Crypto = require('../../crypto/Crypto');
const Crypto = require("../../crypto/Crypto");

const { DOMParser, XMLSerializer } = require('xmldom');
const xpath = require('xpath');
const errors = require('./errors');
const { DOMParser, XMLSerializer } = require("@xmldom/xmldom");
const xpath = require("xpath");
const errors = require("./errors");

const DEFAULT_IV = Buffer.from(Array(16).fill(0, 0, 15));

Expand All @@ -21,58 +21,85 @@ const lastChild = (node) => {

module.exports = (xml, keys) => ({
keys,
doc: new DOMParser().parseFromString(xml, 'text/xml'),
doc: new DOMParser().parseFromString(xml, "text/xml"),

isSegmented() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select('//xmlns:header/xmlns:mutable/xmlns:SegmentNumber', this.doc);
const select = xpath.useNamespaces({ xmlns: "urn:org:ebics:H004" });
const node = select(
"//xmlns:header/xmlns:mutable/xmlns:SegmentNumber",
this.doc
);

return !!node.length;
},

isLastSegment() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select("//xmlns:header/xmlns:mutable/*[@lastSegment='true']", this.doc);
const select = xpath.useNamespaces({ xmlns: "urn:org:ebics:H004" });
const node = select(
"//xmlns:header/xmlns:mutable/*[@lastSegment='true']",
this.doc
);

return !!node.length;
},

orderData() {
const orderDataNode = this.doc.getElementsByTagNameNS('urn:org:ebics:H004', 'OrderData');
const orderDataNode = this.doc.getElementsByTagNameNS(
"urn:org:ebics:H004",
"OrderData"
);

if (!orderDataNode.length) return {};

const orderData = orderDataNode[0].textContent;
const decipher = crypto.createDecipheriv('aes-128-cbc', this.transactionKey(), DEFAULT_IV).setAutoPadding(false);
const data = Buffer.from(decipher.update(orderData, 'base64', 'binary') + decipher.final('binary'), 'binary');
const decipher = crypto
.createDecipheriv("aes-128-cbc", this.transactionKey(), DEFAULT_IV)
.setAutoPadding(false);
const data = Buffer.from(
decipher.update(orderData, "base64", "binary") +
decipher.final("binary"),
"binary"
);

return zlib.inflateSync(data);
},

transactionKey() {
const keyNodeText = this.doc.getElementsByTagNameNS('urn:org:ebics:H004', 'TransactionKey')[0].textContent;
return Crypto.privateDecrypt(this.keys.e(), Buffer.from(keyNodeText, 'base64'));
const keyNodeText = this.doc.getElementsByTagNameNS(
"urn:org:ebics:H004",
"TransactionKey"
)[0].textContent;
return Crypto.privateDecrypt(
this.keys.e(),
Buffer.from(keyNodeText, "base64")
);
},

transactionId() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select('//xmlns:header/xmlns:static/xmlns:TransactionID', this.doc);
const select = xpath.useNamespaces({ xmlns: "urn:org:ebics:H004" });
const node = select(
"//xmlns:header/xmlns:static/xmlns:TransactionID",
this.doc
);

return node.length ? node[0].textContent : '';
return node.length ? node[0].textContent : "";
},

orderId() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select('.//xmlns:header/xmlns:mutable/xmlns:OrderID', this.doc);
const select = xpath.useNamespaces({ xmlns: "urn:org:ebics:H004" });
const node = select(
".//xmlns:header/xmlns:mutable/xmlns:OrderID",
this.doc
);

return node.length ? node[0].textContent : '';
return node.length ? node[0].textContent : "";
},

businessCode() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select('//xmlns:body/xmlns:ReturnCode', this.doc);
const select = xpath.useNamespaces({ xmlns: "urn:org:ebics:H004" });
const node = select("//xmlns:body/xmlns:ReturnCode", this.doc);

return node.length ? node[0].textContent : '';
return node.length ? node[0].textContent : "";
},

businessSymbol(code) {
Expand All @@ -88,17 +115,23 @@ module.exports = (xml, keys) => ({
},

technicalCode() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select('//xmlns:header/xmlns:mutable/xmlns:ReturnCode', this.doc);
const select = xpath.useNamespaces({ xmlns: "urn:org:ebics:H004" });
const node = select(
"//xmlns:header/xmlns:mutable/xmlns:ReturnCode",
this.doc
);

return node.length ? node[0].textContent : '';
return node.length ? node[0].textContent : "";
},

technicalSymbol() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select('//xmlns:header/xmlns:mutable/xmlns:ReportText', this.doc);
const select = xpath.useNamespaces({ xmlns: "urn:org:ebics:H004" });
const node = select(
"//xmlns:header/xmlns:mutable/xmlns:ReportText",
this.doc
);

return node.length ? node[0].textContent : '';
return node.length ? node[0].textContent : "";
},

technicalShortText(code) {
Expand All @@ -113,20 +146,26 @@ module.exports = (xml, keys) => ({
const orderData = this.orderData().toString();
if (!Object.keys(orderData).length) return {};

const doc = new DOMParser().parseFromString(orderData, 'text/xml');
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const keyNodes = select('//xmlns:PubKeyValue', doc);
const doc = new DOMParser().parseFromString(orderData, "text/xml");
const select = xpath.useNamespaces({ xmlns: "urn:org:ebics:H004" });
const keyNodes = select("//xmlns:PubKeyValue", doc);
const bankKeys = {};

if (!keyNodes.length) return {};

for (let i = 0; i < keyNodes.length; i++) {
const type = lastChild(keyNodes[i].parentNode).textContent;
const modulus = xpath.select(".//*[local-name(.)='Modulus']", keyNodes[i])[0].textContent;
const exponent = xpath.select(".//*[local-name(.)='Exponent']", keyNodes[i])[0].textContent;

const mod = Buffer.from(modulus, 'base64');
const exp = Buffer.from(exponent, 'base64');
const modulus = xpath.select(
".//*[local-name(.)='Modulus']",
keyNodes[i]
)[0].textContent;
const exponent = xpath.select(
".//*[local-name(.)='Exponent']",
keyNodes[i]
)[0].textContent;

const mod = Buffer.from(modulus, "base64");
const exp = Buffer.from(exponent, "base64");
bankKeys[`bank${type}`] = {
mod,
exp,
Expand Down
46 changes: 30 additions & 16 deletions lib/orders/H004/signer.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@

'use strict';
"use strict";

// const crypto = require('crypto');
const Crypto = require('../../crypto/Crypto');
const Crypto = require("../../crypto/Crypto");

const { DOMParser, XMLSerializer } = require('xmldom');
const xpath = require('xpath');
const C14n = require('xml-crypto/lib/c14n-canonicalization').C14nCanonicalization;
const { DOMParser, XMLSerializer } = require("@xmldom/xmldom");
const xpath = require("xpath");
const C14n =
require("xml-crypto/lib/c14n-canonicalization").C14nCanonicalization;

const digest = (doc) => {
// get the xml node, where the digested value is supposed to be
const nodeDigestValue = doc.getElementsByTagName('ds:DigestValue')[0];
const nodeDigestValue = doc.getElementsByTagName("ds:DigestValue")[0];

// canonicalize the node that has authenticate='true' attribute
const contentToDigest = xpath.select("//*[@authenticate='true']", doc)
.map(x => new C14n().process(x)).join('');
const contentToDigest = xpath
.select("//*[@authenticate='true']", doc)
.map((x) => new C14n().process(x))
.join("");

// fix the canonicalization
const fixedContent = contentToDigest.replace(/xmlns="urn:org:ebics:H004"/g, 'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"');
const fixedContent = contentToDigest.replace(
/xmlns="urn:org:ebics:H004"/g,
'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"'
);

if (nodeDigestValue)
nodeDigestValue.textContent = Crypto.digestWithHash(fixedContent).toString('base64').trim();
nodeDigestValue.textContent = Crypto.digestWithHash(fixedContent)
.toString("base64")
.trim();

return doc;
};

const sign = (doc, key) => {
const nodeSignatureValue = doc.getElementsByTagName('ds:SignatureValue')[0];
const nodeSignatureValue = doc.getElementsByTagName("ds:SignatureValue")[0];

if (nodeSignatureValue) {
const select = xpath.useNamespaces({ ds: 'http://www.w3.org/2000/09/xmldsig#' });
const contentToSign = (new C14n().process(select('//ds:SignedInfo', doc)[0])).replace('xmlns:ds="http://www.w3.org/2000/09/xmldsig#"', 'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"');
const select = xpath.useNamespaces({
ds: "http://www.w3.org/2000/09/xmldsig#",
});
const contentToSign = new C14n()
.process(select("//ds:SignedInfo", doc)[0])
.replace(
'xmlns:ds="http://www.w3.org/2000/09/xmldsig#"',
'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"'
);

nodeSignatureValue.textContent = Crypto.privateSign(key, contentToSign); // this.keys.x().key.sign(contentToSign, 'base64');
}

return doc;
};

const toXML = doc => new XMLSerializer().serializeToString(doc);
const toXML = (doc) => new XMLSerializer().serializeToString(doc);

module.exports = {
sign(data, keyX) {
const doc = new DOMParser().parseFromString(data, 'text/xml');
const doc = new DOMParser().parseFromString(data, "text/xml");

return toXML(sign(digest(doc), keyX));
},
Expand Down

0 comments on commit a03ec22

Please sign in to comment.