From 9cfed8ec8161f331ee78ee7e87963534b223ea9f Mon Sep 17 00:00:00 2001 From: Vladislav Hristov Date: Wed, 27 Jun 2018 14:21:49 +0300 Subject: [PATCH] Add keys generation and code optimization --- lib/Client.js | 39 ++++++++++++++++++++------- lib/keymanagers/Keys.js | 8 +++--- lib/keymanagers/{Key.js => keyRSA.js} | 3 +++ lib/storages/fsKeysStorage.js | 8 ------ 4 files changed, 37 insertions(+), 21 deletions(-) rename lib/keymanagers/{Key.js => keyRSA.js} (95%) diff --git a/lib/Client.js b/lib/Client.js index b0fa455..1ed7c92 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -10,6 +10,16 @@ const signer = require('./middleware/signer'); const serializer = require('./middleware/serializer'); const response = require('./middleware/response'); +const stringifyKeys = (keys) => { + Object.keys(keys).map((key) => { + keys[key] = keys[key] === null ? null : keys[key].toPem(); + + return key; + }); + + return JSON.stringify(keys); +}; + module.exports = class Client { constructor({ url, @@ -47,6 +57,9 @@ module.exports = class Client { if (!isInObject) throw new Error('Operation for the order needed'); if (order.operation.toUpperCase() === constants.orderOperations.ini) return this.initialization(order); + + if (this.keys() === null) throw new Error('No keys provided. Can not send the order or any other order for that matter.'); + if (order.operation.toUpperCase() === constants.orderOperations.upload) return this.upload(order); if (order.operation.toUpperCase() === constants.orderOperations.download) return this.download(order); @@ -54,6 +67,8 @@ module.exports = class Client { } async initialization(order) { + if (this.keys() === null) this._generateKeys(); + const res = await this.ebicsRequest(order); const xml = res.orderData(); @@ -101,30 +116,36 @@ module.exports = class Client { $request.post({ url: this.url, - body: signer.version(version).sign(serializer.use(order, this).toXML(), keys.x()), // s, // new (signer.version(version))(serializer.use(order).toXML(), keys).digest().sign().toXML(), + body: signer.version(version).sign(serializer.use(order, this).toXML(), keys.x()), headers: { 'content-type': 'text/xml;charset=UTF-8' }, }, (err, res, data) => (err ? reject(err) : resolve(response.version(version)(data, keys)))); }); } keys() { - const keysString = this.keyStorage.read(); + return this._readKeys(); + } - return new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))); + _generateKeys() { + const keysObject = Keys.generate(); + + this._writeKeys(keysObject); } setBankKeys(bankKeys) { const keysObject = this.keys(); keysObject.setBankKeys(bankKeys); - const { keys } = keysObject; + this._writeKeys(keysObject); + } - Object.keys(keys).map((key) => { - keys[key] = keys[key] === null ? null : keys[key].toPem(); + _readKeys() { + const keysString = this.keyStorage.read(); - return key; - }); + return keysString ? new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))) : null; + } - this.keyStorage.write(this.keyEncryptor.encrypt(JSON.stringify(keys))); + _writeKeys(keysObject) { + this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys))); } }; diff --git a/lib/keymanagers/Keys.js b/lib/keymanagers/Keys.js index a7ad85d..fc110c7 100644 --- a/lib/keymanagers/Keys.js +++ b/lib/keymanagers/Keys.js @@ -1,6 +1,6 @@ 'use strict'; -const Key = require('./key'); +const Key = require('./keyRSA'); const keyOrNull = key => (key ? Key(key) : null); @@ -25,15 +25,15 @@ module.exports = class Keys { const keys = {}; Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => { - keys[key] = Key.generate(); + keys[key] = Key().generate(); }); return new Keys(keys); } setBankKeys(bankKeys) { - this.keys.bankX002.importKey(bankKeys.bankX002); - this.keys.bankE002.importKey(bankKeys.bankE002); + this.keys.bankX002 = Key().importKey(bankKeys.bankX002); + this.keys.bankE002 = Key().importKey(bankKeys.bankE002); } a() { diff --git a/lib/keymanagers/Key.js b/lib/keymanagers/keyRSA.js similarity index 95% rename from lib/keymanagers/Key.js rename to lib/keymanagers/keyRSA.js index b955cee..a3c59a8 100644 --- a/lib/keymanagers/Key.js +++ b/lib/keymanagers/keyRSA.js @@ -17,7 +17,10 @@ module.exports = encodedKey => ({ }, importKey({ mod, exp }) { + this.key = new NodeRSA(); this.key.importKey({ n: mod, e: exp }, 'components-public'); + + return this; }, n(to = 'buff') { diff --git a/lib/storages/fsKeysStorage.js b/lib/storages/fsKeysStorage.js index 2333da1..297f8e6 100644 --- a/lib/storages/fsKeysStorage.js +++ b/lib/storages/fsKeysStorage.js @@ -14,14 +14,6 @@ module.exports = (pathToFile) => { write(data) { fs.writeFileSync(path, data, { encoding: 'utf8' }); - return this; - }, - - hasData() { - if (fs.existsSync(path)) - return this.read() !== ''; - - return false; }, }; };