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 });
}
send(order) {
async send(order) {
const isInObject = ('operation' in order);
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.');
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.download) return this.download(order);
@ -67,7 +68,8 @@ module.exports = class Client {
}
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 xml = res.orderData();
@ -110,20 +112,22 @@ module.exports = class Client {
}
ebicsRequest(order) {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
const { version } = order;
const keys = this.keys();
const keys = await this.keys();
$request.post({
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' },
}, (err, res, data) => (err ? reject(err) : resolve(response.version(version)(data, keys))));
});
}
keys() {
return this._readKeys();
async keys() {
const keysString = await this._readKeys();
return keysString ? new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))) : null;
}
_generateKeys() {
@ -132,20 +136,28 @@ module.exports = class Client {
this._writeKeys(keysObject);
}
setBankKeys(bankKeys) {
const keysObject = this.keys();
async setBankKeys(bankKeys) {
const keysObject = await this.keys();
keysObject.setBankKeys(bankKeys);
this._writeKeys(keysObject);
await this._writeKeys(keysObject);
}
_readKeys() {
const keysString = this.keyStorage.read();
async _readKeys() {
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) {
this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys)));
async _writeKeys(keysObject) {
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');
module.exports = {
use(order, client) {
const keys = client.keys();
async use(order, client) {
const keys = await client.keys();
const ebicsAccount = {
partnerId: client.partnerId,
userId: client.userId,

View File

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

View File

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

View File

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