node-ebics-client/packages/ebics-client/lib/orders/H004/serializers/ini.js

149 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-06-15 06:33:41 +00:00
'use strict';
const zlib = require('zlib');
const js2xmlparser = require('js2xmlparser');
const Crypto = require('../../../crypto/Crypto');
const genericSerializer = require('./generic');
2018-06-20 09:20:03 +00:00
const keySignature = (ebicsAccount, key, xmlOptions) => {
2018-06-15 06:33:41 +00:00
const xmlOrderData = {
'@': {
'xmlns:ds': 'http://www.w3.org/2000/09/xmldsig#',
xmlns: 'http://www.ebics.org/S001',
},
SignaturePubKeyInfo: {
PubKeyValue: {
'ds:RSAKeyValue': {
'ds:Modulus': key.n().toString('base64'),
'ds:Exponent': key.e().toString('base64'),
},
TimeStamp: Crypto.timestamp(),
},
SignatureVersion: 'A006',
},
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('SignaturePubKeyOrderData', xmlOrderData, xmlOptions);
};
2018-06-20 09:20:03 +00:00
const orderData = (ebicsAccount, keys, xmlOptions) => {
2018-06-15 06:33:41 +00:00
const xmlOrderData = {
'@': {
'xmlns:ds': 'http://www.w3.org/2000/09/xmldsig#',
xmlns: 'urn:org:ebics:H004',
},
AuthenticationPubKeyInfo: {
PubKeyValue: {
'ds:RSAKeyValue': {
'ds:Modulus': keys.x().n().toString('base64'),
'ds:Exponent': keys.x().e().toString('base64'),
},
},
AuthenticationVersion: 'X002',
},
EncryptionPubKeyInfo: {
PubKeyValue: {
'ds:RSAKeyValue': {
'ds:Modulus': keys.e().n().toString('base64'),
'ds:Exponent': keys.e().e().toString('base64'),
},
},
EncryptionVersion: 'E002',
},
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('HIARequestOrderData', xmlOrderData, xmlOptions);
};
2018-06-20 09:20:03 +00:00
const commonHeader = (ebicsAccount, orderDetails, productString) => ({
2018-06-15 06:33:41 +00:00
'@': { authenticate: true },
static: {
2018-06-20 09:20:03 +00:00
HostID: ebicsAccount.hostId,
2018-06-15 06:33:41 +00:00
Nonce: Crypto.nonce(),
Timestamp: Crypto.timestamp(),
2018-06-20 09:20:03 +00:00
PartnerID: ebicsAccount.partnerId,
UserID: ebicsAccount.userId,
2018-06-15 06:33:41 +00:00
Product: {
'@': { Language: 'en' },
'#': productString,
},
OrderDetails: orderDetails,
SecurityMedium: '0000',
},
mutable: {},
});
const process = {
INI: {
rootName: 'ebicsUnsecuredRequest',
2018-06-20 09:20:03 +00:00
header: (ebicsAccount, orderDetails, productString) => {
const ch = commonHeader(ebicsAccount, orderDetails, productString);
2018-06-15 06:33:41 +00:00
delete ch.static.Nonce;
delete ch.static.Timestamp;
return ch;
},
2018-06-20 09:20:03 +00:00
body: (ebicsAccount, keys, xmlOptions) => ({
2018-06-15 06:33:41 +00:00
DataTransfer: {
2018-06-20 09:20:03 +00:00
OrderData: Buffer.from(zlib.deflateSync(keySignature(ebicsAccount, keys.a(), xmlOptions))).toString('base64'),
2018-06-15 06:33:41 +00:00
},
}),
},
HIA: {
rootName: 'ebicsUnsecuredRequest',
2018-06-20 09:20:03 +00:00
header: (ebicsAccount, orderDetails, productString) => {
const ch = commonHeader(ebicsAccount, orderDetails, productString);
2018-06-15 06:33:41 +00:00
delete ch.static.Nonce;
delete ch.static.Timestamp;
return ch;
},
2018-06-20 09:20:03 +00:00
body: (ebicsAccount, keys, xmlOptions) => ({
2018-06-15 06:33:41 +00:00
DataTransfer: {
2018-06-20 09:20:03 +00:00
OrderData: Buffer.from(zlib.deflateSync(orderData(ebicsAccount, keys, xmlOptions))).toString('base64'),
2018-06-15 06:33:41 +00:00
},
}),
},
HPB: {
rootName: 'ebicsNoPubKeyDigestsRequest',
2018-06-20 09:20:03 +00:00
header: (ebicsAccount, orderDetails, productString) => commonHeader(ebicsAccount, orderDetails, productString),
2018-06-15 06:33:41 +00:00
body: () => ({}),
},
};
module.exports = {
2018-06-27 14:59:35 +00:00
async use(order, client) {
const keys = await client.keys();
2018-06-20 09:20:03 +00:00
const { orderDetails, transactionId } = order;
const { xmlOptions, xmlSchema, productString } = genericSerializer(client.host, transactionId);
2018-06-15 06:33:41 +00:00
const orderType = orderDetails.OrderType.toUpperCase();
2018-06-20 09:20:03 +00:00
const ebicsAccount = {
partnerId: client.partnerId,
userId: client.userId,
hostId: client.hostId,
};
2018-06-15 06:33:41 +00:00
this.rootName = process[orderType].rootName;
this.xmlOptions = xmlOptions;
this.xmlSchema = xmlSchema;
2018-06-20 09:20:03 +00:00
this.xmlSchema.header = process[orderType].header(ebicsAccount, orderDetails, productString);
this.xmlSchema.body = process[orderType].body(ebicsAccount, keys, this.xmlOptions);
2018-06-15 06:33:41 +00:00
if (orderType !== 'HPB' && Object.prototype.hasOwnProperty.call(this.xmlSchema, 'AuthSignature'))
delete this.xmlSchema.AuthSignature;
return this;
},
toXML() {
return js2xmlparser.parse(this.rootName, this.xmlSchema, this.xmlOptions);
},
};