node-ebics-client/lib/Response.js

58 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2018-05-17 15:03:59 +00:00
'use strict';
2018-06-01 13:16:43 +00:00
const zlib = require('zlib');
const crypto = require('crypto');
2018-05-17 15:03:59 +00:00
2018-06-01 13:16:43 +00:00
const { DOMParser, XMLSerializer } = require('xmldom');
const xpath = require('xpath');
2018-05-17 15:03:59 +00:00
2018-06-01 13:16:43 +00:00
const DEFAULT_IV = Buffer.from(Array(16).fill(0, 0, 15));
2018-05-17 15:03:59 +00:00
module.exports = class Response {
constructor(client, data) {
this.client = client;
2018-06-01 13:16:43 +00:00
this.doc = new DOMParser().parseFromString(data, 'text/xml');
}
2018-05-17 15:03:59 +00:00
isSegmented() {
2018-06-01 13:16:43 +00:00
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select('//xmlns:header/xmlns:mutable/xmlns:SegmentNumber', this.doc);
2018-05-17 15:03:59 +00:00
2018-06-01 13:16:43 +00:00
return !!node.length;
2018-05-17 15:03:59 +00:00
}
isLastSegment() {
2018-06-01 13:16:43 +00:00
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
2018-05-17 15:03:59 +00:00
const node = select("//xmlns:header/xmlns:mutable/*[@lastSegment='true']", this.doc);
2018-06-01 13:16:43 +00:00
return !!node.length;
2018-05-17 15:03:59 +00:00
}
orderData() {
2018-06-01 13:16:43 +00:00
const orderData = this.doc.getElementsByTagNameNS('urn:org:ebics:H004', 'OrderData')[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');
2018-05-17 15:03:59 +00:00
return zlib.inflateSync(data).toString();
}
transactionKey() {
2018-06-01 13:16:43 +00:00
const keyNodeText = this.doc.getElementsByTagNameNS('urn:org:ebics:H004', 'TransactionKey')[0].textContent;
2018-05-17 15:03:59 +00:00
const tkEncrypted = Buffer.from(keyNodeText, 'base64');
2018-06-01 13:16:43 +00:00
this.client.e().key.setOptions({ encryptionScheme: 'pkcs1' });
2018-05-17 15:03:59 +00:00
return this.client.e().key.decrypt(tkEncrypted);
}
transactionId() {
2018-06-01 13:16:43 +00:00
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select('//xmlns:header/xmlns:static/xmlns:TransactionID', this.doc);
2018-05-17 15:03:59 +00:00
return node.length ? node[0].textContent : '';
}
toXML() {
return new XMLSerializer().serializeToString(this.doc);
}
};