From a6047a953bedc68a2452ea4b63c3d2b948c25018 Mon Sep 17 00:00:00 2001 From: Valentin Andruschenko Date: Tue, 1 May 2018 23:22:28 +0300 Subject: [PATCH] Issue #49: reworked encrption conditions --- src/modules/Encryption.js | 4 +++ src/modules/EventuateClient.js | 49 +++++++++++++++++++--------------- test/Encryption-spec.js | 11 ++++++++ 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/modules/Encryption.js b/src/modules/Encryption.js index 95352c7..3f59fa5 100644 --- a/src/modules/Encryption.js +++ b/src/modules/Encryption.js @@ -31,4 +31,8 @@ export default class Encryption { findKey(id) { return this.encryptionKeyStore[id]; } + + isEncrypted(eventDataStr) { + return eventDataStr.includes(this.prefix); + } } \ No newline at end of file diff --git a/src/modules/EventuateClient.js b/src/modules/EventuateClient.js index b93aed4..e2f14e8 100644 --- a/src/modules/EventuateClient.js +++ b/src/modules/EventuateClient.js @@ -103,11 +103,8 @@ export default class EventuateClient { options = rest; let events = this.prepareEvents(_events); - - if (encryptionKeyId && this.encryption) { - // Encrypt event data; - events = this.encryptEvens(encryptionKeyId, events); - } + // Encrypt event data if needed + events = this.encryptEvents(encryptionKeyId, events); const jsonData = { entityTypeName, events }; this.addBodyOptions(jsonData, options); @@ -198,10 +195,8 @@ export default class EventuateClient { options = rest; let events = this.prepareEvents(_events); - if (encryptionKeyId && this.encryption) { - // Encrypt event data; - events = this.encryptEvens(encryptionKeyId, events); - } + // Encrypt event data if needed + events = this.encryptEvents(encryptionKeyId, events); const jsonData = { entityId, entityVersion, @@ -597,9 +592,7 @@ export default class EventuateClient { const { id: eventId, eventType, entityId, entityType, swimlane, eventToken } = parsedEvent; let { eventData: eventDataStr } = parsedEvent; - if (this.encryption && eventDataStr.includes(this.encryption.prefix)) { - eventDataStr = this.encryption.decrypt(eventDataStr); - } + eventDataStr = this.decrypt(eventDataStr); const eventData = JSON.parse(eventDataStr); const event = { @@ -725,21 +718,33 @@ export default class EventuateClient { }); } - encryptEvens(encryptionKeyId, events) { - return events.map(({ eventData, ...rest }) => { - eventData = this.encryption.encrypt(encryptionKeyId, eventData); - return { eventData, ...rest }; + encryptEvents(encryptionKeyId, events) { + return events.map(({ eventData, ...rest }) => { + eventData = this.encrypt(encryptionKeyId, eventData); + return { eventData, ...rest }; }); } decryptEvens(events) { - return events.map(({ eventData, ...rest }) => { - if (eventData.includes(this.encryption.prefix)) { - eventData = this.encryption.decrypt(eventData); - } + return events.map(({ eventData, ...rest }) => { + eventData = this.decrypt(eventData); + return { eventData, ...rest }; + }); + } - return { eventData, ...rest }; - }); + encrypt(encryptionKeyId, eventData) { + if (encryptionKeyId && this.encryption) { + eventData = this.encryption.encrypt(encryptionKeyId, eventData); + } + return eventData; + } + + decrypt(eventDataStr) { + if (this.encryption && this.encryption.isEncrypted(eventDataStr)) { + return this.encryption.decrypt(eventDataStr); + } + + return eventDataStr; } } diff --git a/test/Encryption-spec.js b/test/Encryption-spec.js index 70187ad..3d70f3f 100644 --- a/test/Encryption-spec.js +++ b/test/Encryption-spec.js @@ -20,6 +20,17 @@ describe('Encryption', () => { expect(encryption.decrypt).to.be.a('function'); expect(encryption.cipher).to.be.a('function'); expect(encryption.decipher).to.be.a('function'); + expect(encryption.isEncrypted).to.be.a('function'); + }); + + it('isEncrypted() should return true', () => { + const str = encryptedPrefix + 'abcde'; + expect(encryption.isEncrypted(str)).to.be.true; + }); + + it('isEncrypted() should return true', () => { + const str = 'abcde'; + expect(encryption.isEncrypted(str)).to.be.false; }); it('should cipher and decipher', () => {