node-ebics-client/lib/orders/H004/serializers/upload.js

96 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-06-15 06:33:41 +00:00
'use strict';
const zlib = require('zlib');
const crypto = require('crypto');
const js2xmlparser = require('js2xmlparser');
const Crypto = require('../../../crypto/Crypto');
const downloadSerializer = require('./download');
2018-06-20 09:20:03 +00:00
const transKey = crypto.randomBytes(16);
2018-06-15 06:33:41 +00:00
const signatureValue = (document, key) => {
const digested = Crypto.digestWithHash(document.replace(/\n|\r/g, ''));
return Crypto.sign(key, digested);
};
2018-06-20 09:20:03 +00:00
const orderSignature = (ebicsAccount, document, key, xmlOptions) => {
2018-06-15 06:33:41 +00:00
const xmlObj = {
'@': {
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',
},
OrderSignatureData: {
SignatureVersion: 'A006',
SignatureValue: signatureValue(document, key),
2018-06-20 09:20:03 +00:00
PartnerID: ebicsAccount.partnerId,
UserID: ebicsAccount.userId,
2018-06-15 06:33:41 +00:00
},
};
return js2xmlparser.parse('UserSignatureData', xmlObj, xmlOptions);
};
2018-06-20 09:20:03 +00:00
const encryptedOrderSignature = (ebicsAccount, document, transactionKey, key, xmlOptions) => {
const dst = zlib.deflateSync(orderSignature(ebicsAccount, document, key, xmlOptions));
2018-06-15 06:33:41 +00:00
const cipher = crypto.createCipheriv('aes-128-cbc', transactionKey, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])).setAutoPadding(false);
return Buffer.concat([cipher.update(Crypto.pad(dst)), cipher.final()]).toString('base64');
};
const encryptedOrderData = (document, transactionKey) => {
const dst = zlib.deflateSync(document.replace(/\n|\r/g, ''));
const cipher = crypto.createCipheriv('aes-128-cbc', transactionKey, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])).setAutoPadding(false);
return Buffer.concat([cipher.update(Crypto.pad(dst)), cipher.final()]).toString('base64');
};
module.exports = {
2018-06-20 09:20:03 +00:00
use(order, client) {
const keys = client.keys();
const ebicsAccount = {
partnerId: client.partnerId,
userId: client.userId,
hostId: client.hostId,
};
2018-06-15 06:33:41 +00:00
const {
2018-06-20 09:20:03 +00:00
transactionId, document,
} = order;
2018-06-15 06:33:41 +00:00
const {
rootName, xmlOptions, xmlSchema, transfer,
2018-06-20 09:20:03 +00:00
} = downloadSerializer.use(order, client);
2018-06-15 06:33:41 +00:00
this.rootName = rootName;
this.xmlOptions = xmlOptions;
this.xmlSchema = xmlSchema;
this.transfer = transfer;
2018-06-20 09:20:03 +00:00
if (transactionId) return this.transfer(encryptedOrderData(document, transKey));
2018-06-15 06:33:41 +00:00
this.xmlSchema.header.static.NumSegments = 1;
this.xmlSchema.body = {
DataTransfer: {
DataEncryptionInfo: {
'@': { authenticate: true },
EncryptionPubKeyDigest: {
'@': { Version: 'E002', Algorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' },
'#': Crypto.digestPublicKey(keys.bankE()),
},
2018-06-20 09:20:03 +00:00
TransactionKey: Crypto.publicEncrypt(keys.bankE(), transKey).toString('base64'),
2018-06-15 06:33:41 +00:00
},
SignatureData: {
'@': { authenticate: true },
2018-06-20 09:20:03 +00:00
'#': encryptedOrderSignature(ebicsAccount, document, transKey, keys.a(), this.xmlOptions),
2018-06-15 06:33:41 +00:00
},
},
};
return this;
},
toXML() {
return js2xmlparser.parse(this.rootName, this.xmlSchema, this.xmlOptions);
},
};