Async read and write keys

This commit is contained in:
Vladislav Hristov 2018-06-27 17:59:35 +03:00
parent 9cfed8ec81
commit 9660242234
5 changed files with 52 additions and 32 deletions

View File

@ -51,14 +51,15 @@ module.exports = class Client {
this.keyEncryptor = defaultKeyEncryptor({ passphrase }); this.keyEncryptor = defaultKeyEncryptor({ passphrase });
} }
send(order) { async send(order) {
const isInObject = ('operation' in order); const isInObject = ('operation' in order);
if (!isInObject) throw new Error('Operation for the order needed'); if (!isInObject) throw new Error('Operation for the order needed');
if (order.operation.toUpperCase() === constants.orderOperations.ini) return this.initialization(order); 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.'); const keys = await this.keys();
if (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.upload) return this.upload(order);
if (order.operation.toUpperCase() === constants.orderOperations.download) return this.download(order); if (order.operation.toUpperCase() === constants.orderOperations.download) return this.download(order);
@ -67,7 +68,8 @@ module.exports = class Client {
} }
async initialization(order) { async initialization(order) {
if (this.keys() === null) this._generateKeys(); const keys = await this.keys();
if (keys === null) this._generateKeys();
const res = await this.ebicsRequest(order); const res = await this.ebicsRequest(order);
const xml = res.orderData(); const xml = res.orderData();
@ -110,20 +112,22 @@ module.exports = class Client {
} }
ebicsRequest(order) { ebicsRequest(order) {
return new Promise((resolve, reject) => { return new Promise(async (resolve, reject) => {
const { version } = order; const { version } = order;
const keys = this.keys(); const keys = await this.keys();
$request.post({ $request.post({
url: this.url, url: this.url,
body: signer.version(version).sign(serializer.use(order, this).toXML(), keys.x()), body: signer.version(version).sign((await serializer.use(order, this)).toXML(), keys.x()),
headers: { 'content-type': 'text/xml;charset=UTF-8' }, headers: { 'content-type': 'text/xml;charset=UTF-8' },
}, (err, res, data) => (err ? reject(err) : resolve(response.version(version)(data, keys)))); }, (err, res, data) => (err ? reject(err) : resolve(response.version(version)(data, keys))));
}); });
} }
keys() { async keys() {
return this._readKeys(); const keysString = await this._readKeys();
return keysString ? new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))) : null;
} }
_generateKeys() { _generateKeys() {
@ -132,20 +136,28 @@ module.exports = class Client {
this._writeKeys(keysObject); this._writeKeys(keysObject);
} }
setBankKeys(bankKeys) { async setBankKeys(bankKeys) {
const keysObject = this.keys(); const keysObject = await this.keys();
keysObject.setBankKeys(bankKeys); keysObject.setBankKeys(bankKeys);
this._writeKeys(keysObject); await this._writeKeys(keysObject);
} }
_readKeys() { async _readKeys() {
const keysString = this.keyStorage.read(); try {
const keys = await this.keyStorage.read();
return keysString ? new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))) : null; return keys;
} catch (err) {
return null;
}
} }
_writeKeys(keysObject) { async _writeKeys(keysObject) {
this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys))); try {
await this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys)));
} catch (err) {
throw err;
}
} }
}; };

View File

@ -7,8 +7,8 @@ const Crypto = require('../../../crypto/Crypto');
const genericSerializer = require('./generic'); const genericSerializer = require('./generic');
module.exports = { module.exports = {
use(order, client) { async use(order, client) {
const keys = client.keys(); const keys = await client.keys();
const ebicsAccount = { const ebicsAccount = {
partnerId: client.partnerId, partnerId: client.partnerId,
userId: client.userId, userId: client.userId,

View File

@ -118,8 +118,8 @@ const process = {
}; };
module.exports = { module.exports = {
use(order, client) { async use(order, client) {
const keys = client.keys(); const keys = await client.keys();
const { orderDetails, transactionId } = order; const { orderDetails, transactionId } = order;
const { xmlOptions, xmlSchema, productString } = genericSerializer(client.host, transactionId); const { xmlOptions, xmlSchema, productString } = genericSerializer(client.host, transactionId);
const orderType = orderDetails.OrderType.toUpperCase(); const orderType = orderDetails.OrderType.toUpperCase();

View File

@ -47,19 +47,17 @@ const encryptedOrderData = (document, transactionKey) => {
}; };
module.exports = { module.exports = {
use(order, client) { async use(order, client) {
const keys = client.keys(); const keys = await client.keys();
const ebicsAccount = { const ebicsAccount = {
partnerId: client.partnerId, partnerId: client.partnerId,
userId: client.userId, userId: client.userId,
hostId: client.hostId, hostId: client.hostId,
}; };
const { const { transactionId, document } = order;
transactionId, document,
} = order;
const { const {
rootName, xmlOptions, xmlSchema, transfer, rootName, xmlOptions, xmlSchema, transfer,
} = downloadSerializer.use(order, client); } = await downloadSerializer.use(order, client);
this.rootName = rootName; this.rootName = rootName;
this.xmlOptions = xmlOptions; this.xmlOptions = xmlOptions;

View File

@ -6,14 +6,24 @@ module.exports = (pathToFile) => {
const path = pathToFile; const path = pathToFile;
return { return {
read() { write(data) {
if (!fs.existsSync(path)) return new Promise((resolve, reject) => {
return null; fs.writeFile(path, data, { encoding: 'utf8' }, (error) => {
return fs.readFileSync(path, { encoding: 'utf8' }); if (error) throw error;
return resolve();
});
});
}, },
write(data) { read() {
fs.writeFileSync(path, data, { encoding: 'utf8' }); return new Promise((resolve, reject) => {
fs.readFile(path, { encoding: 'utf8' }, (error, data) => {
if (error) reject(error);
return resolve(data);
});
});
}, },
}; };
}; };