mirror of
https://github.com/node-ebics/node-ebics-client.git
synced 2024-12-23 12:51:48 +00:00
Async read and write keys
This commit is contained in:
parent
9cfed8ec81
commit
9660242234
@ -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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -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,
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user