2018-05-17 15:03:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const zlib = require('zlib');
|
2018-06-01 13:16:43 +00:00
|
|
|
const crypto = require('crypto');
|
2018-05-31 08:58:33 +00:00
|
|
|
|
2018-05-17 15:03:59 +00:00
|
|
|
const js2xmlparser = require('js2xmlparser');
|
|
|
|
|
|
|
|
const GenericOrder = require('./GenericOrder');
|
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const pad = (d) => {
|
|
|
|
const dLen = d.length;
|
|
|
|
const len = 16 * (Math.trunc(dLen / 16) + 1);
|
|
|
|
|
|
|
|
return Buffer.concat([d, Buffer.from(Buffer.from([0]).toString().repeat(len - dLen - 1)), Buffer.from([len - dLen])]);
|
|
|
|
};
|
|
|
|
|
2018-05-17 15:03:59 +00:00
|
|
|
module.exports = class GenericUploadOrder extends GenericOrder {
|
|
|
|
constructor(client, document) {
|
|
|
|
super(client);
|
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
this._document = document;
|
|
|
|
this._key = crypto.randomBytes(16);
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
this._schema.body = {
|
|
|
|
DataTransfer: {
|
|
|
|
DataEncryptionInfo: {
|
2018-06-01 13:16:43 +00:00
|
|
|
'@': { authenticate: true },
|
2018-05-17 15:03:59 +00:00
|
|
|
EncryptionPubKeyDigest: {
|
2018-06-01 13:16:43 +00:00
|
|
|
'@': { Version: 'E002', Algorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' },
|
|
|
|
'#': this.client.bankE().publicDigest(),
|
2018-05-17 15:03:59 +00:00
|
|
|
},
|
2018-05-31 08:58:33 +00:00
|
|
|
TransactionKey: this.client.bankE().publicEncrypt(this._key).toString('base64'),
|
2018-05-17 15:03:59 +00:00
|
|
|
},
|
|
|
|
SignatureData: {
|
2018-06-01 13:16:43 +00:00
|
|
|
'@': { authenticate: true },
|
|
|
|
'#': this.encryptedOrderSignature(),
|
|
|
|
},
|
|
|
|
},
|
2018-05-17 15:03:59 +00:00
|
|
|
};
|
2018-06-01 13:16:43 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
orderSignature() {
|
|
|
|
const xmlObj = {
|
2018-06-01 13:16:43 +00:00
|
|
|
'@': {
|
|
|
|
xmlns: 'http://www.ebics.org/S001',
|
|
|
|
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
|
|
|
|
'xsi:schemaLocation': 'http://www.ebics.org/S001 http://www.ebics.org/S001/ebics_signature.xsd',
|
2018-05-17 15:03:59 +00:00
|
|
|
},
|
|
|
|
OrderSignatureData: {
|
2018-06-01 13:16:43 +00:00
|
|
|
SignatureVersion: 'A006',
|
2018-05-17 15:03:59 +00:00
|
|
|
SignatureValue: this.signatureValue(),
|
|
|
|
PartnerID: this.partnerId,
|
2018-06-01 13:16:43 +00:00
|
|
|
UserID: this.userId,
|
|
|
|
},
|
2018-05-17 15:03:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return js2xmlparser.parse('UserSignatureData', xmlObj, this.xmlOptions);
|
2018-06-01 13:16:43 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
signatureValue() {
|
2018-06-01 13:16:43 +00:00
|
|
|
const digested = crypto.createHash('sha256').update(this._document.replace(/\n|\r/g, '')).digest();
|
2018-05-31 08:58:33 +00:00
|
|
|
|
|
|
|
return this.client.a().sign(digested);
|
2018-06-01 13:16:43 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-05-31 08:58:33 +00:00
|
|
|
encryptedOrderData() {
|
2018-06-01 13:16:43 +00:00
|
|
|
const dst = zlib.deflateSync(this._document.replace(/\r|\n/g, ''));
|
|
|
|
const cipher = crypto.createCipheriv('aes-128-cbc', this._key, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])).setAutoPadding(false);
|
2018-05-31 08:58:33 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
return Buffer.concat([cipher.update(pad(dst)), cipher.final()]).toString('base64');
|
2018-05-31 08:58:33 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
encryptedOrderSignature() {
|
|
|
|
const dst = zlib.deflateSync(this.orderSignature());
|
|
|
|
const cipher = crypto.createCipheriv('aes-128-cbc', this._key, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])).setAutoPadding(false);
|
2018-05-31 08:58:33 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
return Buffer.concat([cipher.update(pad(dst)), cipher.final()]).toString('base64');
|
2018-05-31 08:58:33 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
};
|