Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,43 @@ module.exports = class Client {
async download(order) {
if (this.tracesStorage)
this.tracesStorage.new().ofType('ORDER.DOWNLOAD');
const res = await this.ebicsRequest(order);

const orderData = [];

let res = await this.ebicsRequest(order);
let transactionKey = res.transactionKey();

order.transactionId = res.transactionId();
order.segmentNumber = res.segmentNumber();

if (res.orderData()) orderData.push(res.orderData());

// In case of multi-segment download transaction is
// usually supplied during INITIALISATION phase,
// whereas the actual data is delivered in following segments
while (res.isSegmented() && !res.isLastSegment()) {
order.segmentNumber = res.segmentNumber() + 1;

res = await this.ebicsRequest(order);
transactionKey = transactionKey || res.transactionKey();
res.obtainedTransactionKey = transactionKey;

if (res.orderData()) orderData.push(res.orderData());
}

if (res.isSegmented() && res.isLastSegment()) {
if (this.tracesStorage)
this.tracesStorage.connect().ofType('RECEIPT.ORDER.DOWNLOAD');

order.segmentNumber = null; // Clear segment number for receipt request
await this.ebicsRequest(order);
}

const returnedTechnicalCode = res.technicalCode();
const returnedBusinessCode = res.businessCode();

return {
orderData: res.orderData(),
orderData: orderData.length === 1 ? orderData[0] : orderData, // backward compatibility
orderId: res.orderId(),

technicalCode: returnedTechnicalCode,
Expand Down
11 changes: 10 additions & 1 deletion lib/orders/H004/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const lastChild = (node) => {
module.exports = (xml, keys) => ({
keys,
doc: new DOMParser().parseFromString(xml, 'text/xml'),
obtainedTransactionKey: null,

isSegmented() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
Expand All @@ -30,6 +31,13 @@ module.exports = (xml, keys) => ({
return !!node.length;
},

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

return node.length ? Number.parseInt(node[0].textContent, 10) : null;
},

isLastSegment() {
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
const node = select("//xmlns:header/xmlns:mutable/*[@lastSegment='true']", this.doc);
Expand All @@ -40,7 +48,7 @@ module.exports = (xml, keys) => ({
orderData() {
const orderDataNode = this.doc.getElementsByTagNameNS('urn:org:ebics:H004', 'OrderData');

if (!orderDataNode.length) return {};
if (!orderDataNode.length) return null;

const orderData = orderDataNode[0].textContent;
const decipher = crypto.createDecipheriv('aes-128-cbc', this.transactionKey(), DEFAULT_IV).setAutoPadding(false);
Expand All @@ -50,6 +58,7 @@ module.exports = (xml, keys) => ({
},

transactionKey() {
if (this.obtainedTransactionKey) return this.obtainedTransactionKey;
const keyNodeText = this.doc.getElementsByTagNameNS('urn:org:ebics:H004', 'TransactionKey')[0].textContent;
return Crypto.privateDecrypt(this.keys.e(), Buffer.from(keyNodeText, 'base64'));
},
Expand Down
51 changes: 31 additions & 20 deletions lib/orders/H004/serializers/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
userId: client.userId,
hostId: client.hostId,
};
const { orderDetails, transactionId } = order;
const { orderDetails, transactionId, segmentNumber } = order;
const {
rootName, xmlOptions, xmlSchema, receipt, transfer, productString,
} = genericSerializer(client.hostId, transactionId);
Expand All @@ -25,35 +25,46 @@ module.exports = {
this.receipt = receipt;
this.transfer = transfer;

if (transactionId) return this.receipt();
if (!segmentNumber && transactionId) return this.receipt();

this.xmlSchema.header = {
'@': { authenticate: true },
static: {
HostID: ebicsAccount.hostId,
Nonce: Crypto.nonce(),
Timestamp: Crypto.timestamp(),
PartnerID: ebicsAccount.partnerId,
UserID: ebicsAccount.userId,
Product: {
'@': { Language: 'en' },
'#': productString,
},
OrderDetails: orderDetails,
BankPubKeyDigests: {
Authentication: {
'@': { Version: 'X002', Algorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' },
'#': Crypto.digestPublicKey(keys.bankX()),
...!transactionId && {
Nonce: Crypto.nonce(),
Timestamp: Crypto.timestamp(),
PartnerID: ebicsAccount.partnerId,
UserID: ebicsAccount.userId,
Product: {
'@': { Language: 'en' },
'#': productString,
},
Encryption: {
'@': { Version: 'E002', Algorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' },
'#': Crypto.digestPublicKey(keys.bankE()),
OrderDetails: orderDetails,
BankPubKeyDigests: {
Authentication: {
'@': { Version: 'X002', Algorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' },
'#': Crypto.digestPublicKey(keys.bankX()),
},
Encryption: {
'@': { Version: 'E002', Algorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' },
'#': Crypto.digestPublicKey(keys.bankE()),
},
},
SecurityMedium: '0000',
},
...transactionId && {
TransactionID: transactionId,
},
SecurityMedium: '0000',
},
mutable: {
TransactionPhase: 'Initialisation',
TransactionPhase: segmentNumber ? 'Transfer' : 'Initialisation',
...segmentNumber && {
SegmentNumber: {
'@': { lastSegment: false },
'#': segmentNumber,
},
},
},
};

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.