Add keys generation and code optimization

This commit is contained in:
Vladislav Hristov 2018-06-27 14:21:49 +03:00
parent c0ec4b8fb7
commit 9cfed8ec81
4 changed files with 37 additions and 21 deletions

View File

@ -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)));
}
};

View File

@ -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() {

View File

@ -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') {

View File

@ -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;
},
};
};