From dbcadbfe08e7dbdb5380352797803fecd5ef044e Mon Sep 17 00:00:00 2001 From: warcode Date: Tue, 1 Sep 2015 09:40:23 +0200 Subject: [PATCH 1/2] Create sharedsecret.coffee --- .../sharedsecret.coffee | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 packages/rocketchat-sharedsecret/sharedsecret.coffee diff --git a/packages/rocketchat-sharedsecret/sharedsecret.coffee b/packages/rocketchat-sharedsecret/sharedsecret.coffee new file mode 100644 index 000000000000..19b72c746ad3 --- /dev/null +++ b/packages/rocketchat-sharedsecret/sharedsecret.coffee @@ -0,0 +1,82 @@ +SharedSecret = [] + +if (Meteor.isServer) + class EncryptMessage + constructor: (message) -> + currentUser = Meteor.user()._id + currentRoomId = message.rid + + if(SharedSecret? && SharedSecret[currentUser]? && SharedSecret[currentUser][currentRoomId]?) + currentSecret = SharedSecret[currentUser][currentRoomId] + encrypted = CryptoJS.AES.encrypt(message.msg, currentSecret) + message.msg = encrypted.toString() + + #urls + if(message.urls) + for urls in message.urls + urls.url = CryptoJS.AES.encrypt(urls.url, currentSecret).toString() + + message.encrypted = true + + return message + + class HandleSlashCommand + constructor: (command, params, item) -> + if(command == "setsecretkey") + currentUser = Meteor.user()._id + currentRoomId = item.rid + secret = params + + if(secret == "off") + secret = null + + if(SharedSecret[currentUser]?) + SharedSecret[currentUser][currentRoomId] = secret + else + SharedSecret[currentUser] = [] + SharedSecret[currentUser][currentRoomId] = secret + + RocketChat.callbacks.add 'beforeSaveMessage', EncryptMessage, 9999 #LAST + RocketChat.slashCommands.add 'setsecretkey', HandleSlashCommand + +if (Meteor.isClient) + class DecryptMessage + constructor: (message) -> + if(message.encrypted) + currentRoomId = message.rid + currentSecret = localStorage.getItem("rocket.chat.sharedSecretKey.#{currentRoomId}") + + if(currentSecret?) + decrypted = CryptoJS.AES.decrypt(message.msg, currentSecret).toString(CryptoJS.enc.Utf8) + + if(decrypted == "") + message.msg = "~ encrypted message ~" + message.html = "~ encrypted message ~" + else + lockImage = "/images/lock8.png" + message.msg = " " + decrypted + message.html = " " + decrypted + + #urls + if(message.urls) + for urls in message.urls + urls.url = CryptoJS.AES.decrypt(urls.url, currentSecret).toString(CryptoJS.enc.Utf8) + else + message.msg = "~ encrypted message ~" + message.html = "~ encrypted message ~" + + return message + + class HandleSlashCommand + constructor: (command, params, item) -> + if(command == "setsecretkey") + secret = params + if(secret == "off") + secret = null + currentRoomId = item.rid + localStorage.setItem("rocket.chat.sharedSecretKey.#{currentRoomId}", secret) + + + + RocketChat.callbacks.add 'renderMessage', DecryptMessage, -9999 #FIRST + RocketChat.slashCommands.add 'setsecretkey', HandleSlashCommand From 98748e9d67a0068e9084de86f6f666520853fd3e Mon Sep 17 00:00:00 2001 From: warcode Date: Tue, 1 Sep 2015 09:41:14 +0200 Subject: [PATCH 2/2] Create package.js --- packages/rocketchat-sharedsecret/package.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 packages/rocketchat-sharedsecret/package.js diff --git a/packages/rocketchat-sharedsecret/package.js b/packages/rocketchat-sharedsecret/package.js new file mode 100644 index 000000000000..4371ff0846a0 --- /dev/null +++ b/packages/rocketchat-sharedsecret/package.js @@ -0,0 +1,21 @@ +Package.describe({ + name: 'rocketchat:sharedsecret', + version: '0.0.1', + summary: 'RocketChat libraries', + git: '' +}); + +Package.onUse(function(api) { + api.versionsFrom('1.0'); + + api.use([ + 'coffeescript', + 'rocketchat:lib@0.0.1' + ]); + + api.use(['jparker:crypto-aes'], ['server','client']); + + api.addFiles('sharedsecret.coffee', ['server','client']); +}); + +Package.onTest(function(api) {});