mirror of
https://github.com/node-ebics/node-ebics-client.git
synced 2024-11-21 21:52:07 +00:00
Add keys generation and code optimization
This commit is contained in:
parent
c0ec4b8fb7
commit
9cfed8ec81
@ -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)));
|
||||
}
|
||||
};
|
||||
|
@ -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() {
|
||||
|
@ -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') {
|
@ -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;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user