Skip to content

Commit

Permalink
Merge pull request #652 from warcode/feature/sharesecret
Browse files Browse the repository at this point in the history
Feature/sharesecret #268
  • Loading branch information
engelgabriel committed Sep 1, 2015
2 parents b7eed3d + 98748e9 commit e85e249
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/rocketchat-sharedsecret/package.js
Original file line number Diff line number Diff line change
@@ -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) {});
82 changes: 82 additions & 0 deletions packages/rocketchat-sharedsecret/sharedsecret.coffee
Original file line number Diff line number Diff line change
@@ -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 = "<img src=#{lockImage} style='width:8px;height:9px;'></img> " + decrypted
message.html = "<img src=#{lockImage} style='width:8px;height:9px;'></img> " + 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

0 comments on commit e85e249

Please sign in to comment.