reorder file structure

This commit is contained in:
Vladislav Hristov 2018-06-11 15:25:07 +03:00
parent ff9a3a16b4
commit 10111878fa
11 changed files with 60 additions and 118 deletions

View File

@ -1,72 +1,17 @@
'use strict'; 'use strict';
// const fs = require('fs');
// const path = require('path');
const $request = require('request'); const $request = require('request');
const XMLSign = require('./middleware/XMLSign'); const XMLSign = require('./middleware/XMLSign');
const ParseResponse = require('./middleware/ParseResponse'); const ParseResponse = require('./middleware/ParseResponse');
// const BankLetter = require('./BankLetter');
const OrderSerializer = require('./orders/H004/OrderSerializer');
/* const defaultIniTemplatePath = path.join(__dirname, '../templates/ini.hbs');
const utils = {
mapObject: (o = {}, predicate = v => v) => Object.entries(o).reduce((r, [key, value]) => { r[key] = value; return r; }, o),
exponent: {
// str = 65537 => AQAB
toBase64(str) {
return new BN(str).toBuffer().toString('base64');
},
// str = AQAB => 65537
fromBase64(str) {
return new BN(Buffer.from(str, 'base64'), 2).toNumber();
},
},
}; */
module.exports = class Client { module.exports = class Client {
/* constructor({
url,
hostId,
userId,
partnerId,
keyManager = new FsKeyManager({ path: './keys.ebics', passphrase: 'node-ebics' }),
}) {
this.url = url;
this.hostId = hostId;
this.userId = userId;
this.partnerId = partnerId;
this.keyManager = keyManager;
} */
constructor({ url }) { constructor({ url }) {
this.url = url; this.url = url;
} }
/*
saveIniLetter(bankName, target, template) {
const letter = new BankLetter({
client: this,
bankName,
template: template || fs.readFileSync(defaultIniTemplatePath, { encoding: 'utf8' }),
});
try {
fs.writeFileSync(target, letter.generate());
} catch (error) {
throw error;
}
}
*/
async initialization(order) { async initialization(order) {
const res = await this.ebicsRequest(OrderSerializer.serialize(order)); const res = await this.ebicsRequest(order);
const xml = res.orderData(); const xml = res.orderData();
return { return {
@ -79,12 +24,12 @@ module.exports = class Client {
} }
async download(order) { async download(order) {
const res = await this.ebicsRequest(OrderSerializer.serialize(order)); const res = await this.ebicsRequest(order);
order.transactionId = res.transactionId(); order.transactionId = res.transactionId();
if (res.isSegmented() && res.isLastSegment()) if (res.isSegmented() && res.isLastSegment())
await this.ebicsRequest(OrderSerializer.serialize(order)); await this.ebicsRequest(order);
// return res.orderData(); // return res.orderData();
return { return {
@ -96,13 +41,13 @@ module.exports = class Client {
} }
async upload(order) { async upload(order) {
let res = await this.ebicsRequest(OrderSerializer.serialize(order)); let res = await this.ebicsRequest(order);
const transactionId = res.transactionId(); const transactionId = res.transactionId();
const orderId = res.orderId(); const orderId = res.orderId();
order.transactionId = transactionId; order.transactionId = transactionId;
res = await this.ebicsRequest(OrderSerializer.serialize(order)); res = await this.ebicsRequest(order);
return [transactionId, orderId]; return [transactionId, orderId];
} }
@ -111,16 +56,13 @@ module.exports = class Client {
} }
ebicsRequest(serializedOrder) { ebicsRequest(order) {
const { keys } = serializedOrder;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const s = XMLSign.go(keys, serializedOrder.toXML());
$request.post({ $request.post({
url: this.url, url: this.url,
body: s, // XMLSign.go(this, serializedOrder), body: XMLSign.sign(order),
headers: { 'content-type': 'text/xml;charset=UTF-8' }, headers: { 'content-type': 'text/xml;charset=UTF-8' },
}, (err, res, data) => (err ? reject(err) : resolve(ParseResponse.go(keys, data)))); }, (err, res, data) => (err ? reject(err) : resolve(ParseResponse.parse(data, order.keys, order.version))));
}); });
} }

View File

@ -7,15 +7,7 @@ const crypto = require('crypto');
module.exports = class OrderBuilder { module.exports = class OrderBuilder {
constructor() { constructor() {
this._transactionKey = crypto.randomBytes(16); this._transactionKey = crypto.randomBytes(16);
this._root = { this._root = {};
nodeName: 'ebicsRequest',
nodeAttributes: {
'xmlns:ds': 'http://www.w3.org/2000/09/xmldsig#',
xmlns: 'urn:org:ebics:H004',
Version: 'H004',
Revision: '1',
},
};
this._body = {}; this._body = {};
} }
@ -26,27 +18,36 @@ module.exports = class OrderBuilder {
return this; return this;
} }
static payment() { payment() {
const builder = new OrderBuilder(); this._type = 'payment';
builder._type = 'payment'; return this;
return builder;
} }
static status() { status() {
const builder = new OrderBuilder(); this._type = 'status';
builder._type = 'status'; return this;
return builder;
} }
static ini() { ini() {
this._type = 'ini';
this._root.nodeName = 'ebicsUnsecuredRequest';
return this;
}
static h004() {
const builder = new OrderBuilder(); const builder = new OrderBuilder();
builder._type = 'ini'; builder._version = 'H004';
builder._root.nodeName = 'ebicsUnsecuredRequest'; builder._root.nodeName = 'ebicsRequest';
builder._root.nodeAttributes = {
'xmlns:ds': 'http://www.w3.org/2000/09/xmldsig#',
xmlns: 'urn:org:ebics:H004',
Version: 'H004',
Revision: '1',
};
return builder; return builder;
} }
@ -67,6 +68,7 @@ module.exports = class OrderBuilder {
get partnerId() { return this._data.ebicsData.partnerId; } get partnerId() { return this._data.ebicsData.partnerId; }
get userId() { return this._data.ebicsData.userId; } get userId() { return this._data.ebicsData.userId; }
get keys() { return this._data.ebicsData.keysManager.keys(); } get keys() { return this._data.ebicsData.keysManager.keys(); }
get version() { return this._version; }
set transactionId(tid) { set transactionId(tid) {
this._data.transactionId = tid === '' ? null : tid; this._data.transactionId = tid === '' ? null : tid;

View File

@ -1,16 +1,11 @@
'use strict'; 'use strict';
const Response = require('../Response'); const H004Response = require('../versions/H004/Response');
module.exports = class ParseResponse { module.exports = class ParseResponse {
static go(keys, data) { static parse(data, keys, version) {
const response = new Response(keys, data); if (version.toUpperCase() === 'H004') return new H004Response(data, keys);
// TODO: throw Error('Unknow EBICS response version');
// raise error if any
this.data = response.doc;
return response;
} }
}; };

View File

@ -1,16 +1,15 @@
'use strict'; 'use strict';
const Signer = require('../Signer'); const H004Signer = require('../versions/H004/Signer');
const H004Serializer = require('../versions/H004/OrderSerializer');
module.exports = class XMLSign { module.exports = class XMLSign {
static go(keys, xml) { static sign(order) {
const signer = new Signer(keys, xml); const { keys } = order;
signer.digest(); if (order.version.toUpperCase() === 'H004') return new H004Signer(H004Serializer.serialize(order).toXML(), keys).digest().sign().toXML();
signer.sign();
this.data = signer.toXML(); throw Error('Error from XMLSign class: Invalid version number');
return this.data;
} }
}; };

View File

@ -1,8 +1,8 @@
'use strict'; 'use strict';
const InitializationSerializer = require('./InitializationSerializer'); const InitializationSerializer = require('./serializers/InitializationSerializer');
const StatusSerializer = require('./StatusSerializer'); const StatusSerializer = require('./serializers/StatusSerializer');
const PaymentSerializer = require('./PaymentSerializer'); const PaymentSerializer = require('./serializers/PaymentSerializer');
module.exports = class OrderSerializer { module.exports = class OrderSerializer {
static serialize(order) { static serialize(order) {

View File

@ -4,7 +4,7 @@ const zlib = require('zlib');
const crypto = require('crypto'); const crypto = require('crypto');
const BN = require('bn.js'); const BN = require('bn.js');
const Crypto = require('./crypto/Crypto'); const Crypto = require('../../crypto/Crypto');
const { DOMParser, XMLSerializer } = require('xmldom'); const { DOMParser, XMLSerializer } = require('xmldom');
const xpath = require('xpath'); const xpath = require('xpath');
@ -20,7 +20,7 @@ const lastChild = (node) => {
}; };
module.exports = class Response { module.exports = class Response {
constructor(keys, data) { constructor(data, keys) {
this.keys = keys; this.keys = keys;
this.doc = new DOMParser().parseFromString(data, 'text/xml'); this.doc = new DOMParser().parseFromString(data, 'text/xml');
} }

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// const crypto = require('crypto'); // const crypto = require('crypto');
const Crypto = require('./crypto/Crypto'); const Crypto = require('../../crypto/Crypto');
const { DOMParser, XMLSerializer } = require('xmldom'); const { DOMParser, XMLSerializer } = require('xmldom');
const xpath = require('xpath'); const xpath = require('xpath');
@ -15,7 +15,7 @@ module.exports = class Signer {
* @param {Keys} keys * @param {Keys} keys
* @param {String} data * @param {String} data
*/ */
constructor(keys, data) { constructor(data, keys) {
/** /**
* Keys to operate with * Keys to operate with
* *
@ -44,6 +44,8 @@ module.exports = class Signer {
if (nodeDigestValue) if (nodeDigestValue)
nodeDigestValue.textContent = Crypto.digestWithHash(fixedContent).toString('base64').trim(); nodeDigestValue.textContent = Crypto.digestWithHash(fixedContent).toString('base64').trim();
return this;
} }
sign() { sign() {
@ -55,6 +57,8 @@ module.exports = class Signer {
nodeSignatureValue.textContent = Crypto.privateSign(this.keys.x(), contentToSign); // this.keys.x().key.sign(contentToSign, 'base64'); nodeSignatureValue.textContent = Crypto.privateSign(this.keys.x(), contentToSign); // this.keys.x().key.sign(contentToSign, 'base64');
} }
return this;
} }
toXML() { toXML() {

View File

@ -3,8 +3,8 @@
const zlib = require('zlib'); const zlib = require('zlib');
const js2xmlparser = require('js2xmlparser'); const js2xmlparser = require('js2xmlparser');
const consts = require('../../consts'); const consts = require('../../../consts');
const Crypto = require('../../crypto/Crypto'); const Crypto = require('../../../crypto/Crypto');
const GenericSerializer = require('./GenericSerializer'); const GenericSerializer = require('./GenericSerializer');

View File

@ -5,8 +5,8 @@ const crypto = require('crypto');
const js2xmlparser = require('js2xmlparser'); const js2xmlparser = require('js2xmlparser');
const consts = require('../../consts'); const consts = require('../../../consts');
const Crypto = require('../../crypto/Crypto'); const Crypto = require('../../../crypto/Crypto');
const GenericSerializer = require('./GenericSerializer'); const GenericSerializer = require('./GenericSerializer');

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const consts = require('../../consts'); const consts = require('../../../consts');
const Crypto = require('../../crypto/Crypto'); const Crypto = require('../../../crypto/Crypto');
const GenericSerializer = require('./GenericSerializer'); const GenericSerializer = require('./GenericSerializer');