Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Hristov authored and Vladislav Hristov committed Jun 15, 2018
1 parent d5d80ee commit 1876360
Show file tree
Hide file tree
Showing 26 changed files with 789 additions and 682 deletions.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
'use strict';

const Client = require('./lib/Client');
const OrderBuilder = require('./lib/OrderBuilder');
const ISO20022Builder = require('./lib/ISO20022OrderBuilder');
const keysManager = require('./lib/keymanagers/keysManager');
const fsKeysStorage = require('./lib/keymanagers/fsKeysStorage');

module.exports = Client;
module.exports = {
Client,
OrderBuilder,
ISO20022Builder,
keysManager,
fsKeysStorage,
};
35 changes: 21 additions & 14 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

const $request = require('request');

const XMLSign = require('./middleware/XMLSign');
const ParseResponse = require('./middleware/ParseResponse');
const signer = require('./middleware/signer');
const serializer = require('./middleware/serializer');
const response = require('./middleware/response');

module.exports = class Client {
constructor({ url }) {
constructor(url) {
this.url = url;
}

Expand Down Expand Up @@ -52,25 +53,31 @@ module.exports = class Client {
return [transactionId, orderId];
}

async downloadAndUnzip(order) { // eslint-disable-line

}

ebicsRequest(order) {
return new Promise((resolve, reject) => {
const { version, keys } = order;
// const s = signer.version(version).use(serializer.use(order).toXML(), keys).digest().sign().toXML(); // new (signer.version(version))(serializer.use(order).toXML(), keys).digest().sign().toXML();
$request.post({
url: this.url,
body: XMLSign.sign(order),
body: signer.version(version).sign(serializer.use(order).toXML(), keys), // s, // new (signer.version(version))(serializer.use(order).toXML(), keys).digest().sign().toXML(),
headers: { 'content-type': 'text/xml;charset=UTF-8' },
}, (err, res, data) => (err ? reject(err) : resolve(ParseResponse.parse(data, order.keys, order.version))));
}, (err, res, data) => (err ? reject(err) : resolve(response.version(version)(data, keys))));
});
}

request(order) {
if (order.type.toLowerCase() === 'ini') return this.initialization(order);
if (order.type.toLowerCase() === 'payment') return this.upload(order);
if (order.type.toLowerCase() === 'status') return this.download(order);
ini(order) {
return this.initialization(order);
}

payment(order) {
return this.upload(order);
}

statement(order) {
return this.download(order);
}

throw Error('Invalid order type');
status(order) {
return this.download(order);
}
};
101 changes: 101 additions & 0 deletions lib/ISO20022OrderBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'use strict';

const OrderBuilder = require('./OrderBuilder');

module.exports = class ISO20022OrderBuilder extends OrderBuilder {
get document() { return this._document; }
get ebicsData() { return this._ebicsData; }

use(dataToUse) {
if (Object.prototype.hasOwnProperty.call(dataToUse, 'ebicsData')) this._ebicsData = dataToUse.ebicsData;
if (Object.prototype.hasOwnProperty.call(dataToUse, 'document')) this._document = dataToUse.document;

return this;
}

static h004() {
const builder = new ISO20022OrderBuilder();

builder._version = 'H004';

return builder;
}

INI() {
return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'INI', OrderAttribute: 'DZNNN' },
});
}

HIA() {
return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'HIA', OrderAttribute: 'DZNNN' },
});
}

HPB() {
return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'HPB', OrderAttribute: 'DZHNN' },
});
}

HKD() {
return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'HKD', OrderAttribute: 'DZHNN', StandardOrderParams: {} },
});
}

HPD() {
return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'HPD', OrderAttribute: 'DZHNN', StandardOrderParams: {} },
});
}

HTD() {
return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'HTD', OrderAttribute: 'DZHNN', StandardOrderParams: {} },
});
}

HAA() {
return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'HAA', OrderAttribute: 'DZHNN', StandardOrderParams: {} },
});
}

HAC(start = null, end = null) {
const params = start && end
? { DateRange: { Start: start, End: end } }
: {};

return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'HAC', OrderAttribute: 'DZHNN', StandardOrderParams: params },
});
}

PTK(start = null, end = null) {
const params = start && end
? { DateRange: { Start: start, End: end } }
: {};

return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'PTK', OrderAttribute: 'DZHNN', StandardOrderParams: params },
});
}

Z52() {
return this.details({
ebicsData: this.ebicsData,
orderDetails: { OrderType: 'Z52', OrderAttribute: 'DZHNN', StandardOrderParams: {} },
});
}
};
24 changes: 4 additions & 20 deletions lib/OrderBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

const crypto = require('crypto');

// const orderTypes = ['ini', 'download', 'upload', 'zip'];
const constants = require('./consts');

module.exports = class OrderBuilder {
constructor() {
this._productString = constants.productString;
this._transactionKey = crypto.randomBytes(16);
}

Expand All @@ -16,24 +17,6 @@ module.exports = class OrderBuilder {
return this;
}

payment() {
this._type = 'payment';

return this;
}

status() {
this._type = 'status';

return this;
}

ini() {
this._type = 'ini';

return this;
}

static h004() {
const builder = new OrderBuilder();

Expand All @@ -45,7 +28,6 @@ module.exports = class OrderBuilder {
/**
* Getters
*/
get type() { return this._type; }
get data() { return this._data; }
get orderDetails() { return this._data.orderDetails; }
get transactionId() { return this._data.transactionId; }
Expand All @@ -57,6 +39,8 @@ module.exports = class OrderBuilder {
get userId() { return this._data.ebicsData.userId; }
get keys() { return this._data.ebicsData.keysManager.keys(); }
get version() { return this._version; }
get productString() { return this._productString; }
get orderType() { return this.orderDetails.OrderType; }

set transactionId(tid) {
this._data.transactionId = tid === '' ? null : tid;
Expand Down
40 changes: 0 additions & 40 deletions lib/keymanagers/FsKeyStorage.js

This file was deleted.

118 changes: 46 additions & 72 deletions lib/keymanagers/KeysManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,76 +19,50 @@ const decrypt = (data, algorithm, passphrase) => {
return decrypted;
};

module.exports = class KeysManager {
constructor(keysStorage, passphrase, algorithm = 'aes-256-cbc', createIfNone = true) {
this._storage = keysStorage;
this._passphrase = passphrase;
this._algorithm = algorithm;

if (createIfNone && !this._storage.hasData())
this.generate();
}

/**
* Generates the keys to work with. Then either
* saves them to the storage or returnes the keys generated
*
* @param {Boolean} save
* @default true
*
* @returns void | Keys object
*/
generate(save = true) {
const keys = Keys.generate();

if (save) this.write(keys);

return keys;
}

/**
* Writes the keys to the storage
*
* @param {Keys} keysObject
*
* @returns void
*/
write(keysObject) {
keysObject = keysObject.keys;

Object.keys(keysObject).map((key) => {
keysObject[key] = keysObject[key] === null ? null : keysObject[key].toPem();

return key;
});

this._storage.save(encrypt(JSON.stringify(keysObject), this._algorithm, this._passphrase));
}

setBankKeys(bankKeys) {
const keys = this.keys();

keys.setBankKeys(bankKeys);
this.write(keys);
}

/**
* Gets the keys
*
* @returns Keys object
*/
keys() {
return this._read();
}

/**
* Reads the keys from the storage
*
* @returns Keys object
*/
_read() {
const keysString = this._storage.read();

return new Keys(JSON.parse(decrypt(keysString, this._algorithm, this._passphrase)));
}
module.exports = (keysStorage, passphrase, algorithm = 'aes-256-cbc') => {
const storage = keysStorage;
const pass = passphrase;
const algo = algorithm;
// const createIfNone = createIfNone;

return {
generate(save = true) {
const keys = Keys.generate();

if (save) {
this.write(keys);

return this;
}

return keys;
},

write(keysObject) {
keysObject = keysObject.keys;

Object.keys(keysObject).map((key) => {
keysObject[key] = keysObject[key] === null ? null : keysObject[key].toPem();

return key;
});

storage.save(encrypt(JSON.stringify(keysObject), algo, pass));

return this;
},

setBankKeys(bankKeys) {
const keys = this.keys();

keys.setBankKeys(bankKeys);
this.write(keys);
},

keys() {
const keysString = storage.read();

return new Keys(JSON.parse(decrypt(keysString, algo, pass)));
},
};
};

0 comments on commit 1876360

Please sign in to comment.