Skip to content

Commit

Permalink
Issue eventuate-clients#49: reworked encrption conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
dartvandru committed May 1, 2018
1 parent e3dd0c0 commit a6047a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/modules/Encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ export default class Encryption {
findKey(id) {
return this.encryptionKeyStore[id];
}

isEncrypted(eventDataStr) {
return eventDataStr.includes(this.prefix);
}
}
49 changes: 27 additions & 22 deletions src/modules/EventuateClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/Encryption-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit a6047a9

Please sign in to comment.