Skip to content

Commit e85e249

Browse files
committed
Merge pull request #652 from warcode/feature/sharesecret
Feature/sharesecret #268
2 parents b7eed3d + 98748e9 commit e85e249

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Package.describe({
2+
name: 'rocketchat:sharedsecret',
3+
version: '0.0.1',
4+
summary: 'RocketChat libraries',
5+
git: ''
6+
});
7+
8+
Package.onUse(function(api) {
9+
api.versionsFrom('1.0');
10+
11+
api.use([
12+
'coffeescript',
13+
'rocketchat:[email protected]'
14+
]);
15+
16+
api.use(['jparker:crypto-aes'], ['server','client']);
17+
18+
api.addFiles('sharedsecret.coffee', ['server','client']);
19+
});
20+
21+
Package.onTest(function(api) {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
SharedSecret = []
2+
3+
if (Meteor.isServer)
4+
class EncryptMessage
5+
constructor: (message) ->
6+
currentUser = Meteor.user()._id
7+
currentRoomId = message.rid
8+
9+
if(SharedSecret? && SharedSecret[currentUser]? && SharedSecret[currentUser][currentRoomId]?)
10+
currentSecret = SharedSecret[currentUser][currentRoomId]
11+
encrypted = CryptoJS.AES.encrypt(message.msg, currentSecret)
12+
message.msg = encrypted.toString()
13+
14+
#urls
15+
if(message.urls)
16+
for urls in message.urls
17+
urls.url = CryptoJS.AES.encrypt(urls.url, currentSecret).toString()
18+
19+
message.encrypted = true
20+
21+
return message
22+
23+
class HandleSlashCommand
24+
constructor: (command, params, item) ->
25+
if(command == "setsecretkey")
26+
currentUser = Meteor.user()._id
27+
currentRoomId = item.rid
28+
secret = params
29+
30+
if(secret == "off")
31+
secret = null
32+
33+
if(SharedSecret[currentUser]?)
34+
SharedSecret[currentUser][currentRoomId] = secret
35+
else
36+
SharedSecret[currentUser] = []
37+
SharedSecret[currentUser][currentRoomId] = secret
38+
39+
RocketChat.callbacks.add 'beforeSaveMessage', EncryptMessage, 9999 #LAST
40+
RocketChat.slashCommands.add 'setsecretkey', HandleSlashCommand
41+
42+
if (Meteor.isClient)
43+
class DecryptMessage
44+
constructor: (message) ->
45+
if(message.encrypted)
46+
currentRoomId = message.rid
47+
currentSecret = localStorage.getItem("rocket.chat.sharedSecretKey.#{currentRoomId}")
48+
49+
if(currentSecret?)
50+
decrypted = CryptoJS.AES.decrypt(message.msg, currentSecret).toString(CryptoJS.enc.Utf8)
51+
52+
if(decrypted == "")
53+
message.msg = "~ encrypted message ~"
54+
message.html = "~ encrypted message ~"
55+
else
56+
lockImage = "/images/lock8.png"
57+
message.msg = "<img src=#{lockImage} style='width:8px;height:9px;'></img> " + decrypted
58+
message.html = "<img src=#{lockImage} style='width:8px;height:9px;'></img> " + decrypted
59+
60+
#urls
61+
if(message.urls)
62+
for urls in message.urls
63+
urls.url = CryptoJS.AES.decrypt(urls.url, currentSecret).toString(CryptoJS.enc.Utf8)
64+
else
65+
message.msg = "~ encrypted message ~"
66+
message.html = "~ encrypted message ~"
67+
68+
return message
69+
70+
class HandleSlashCommand
71+
constructor: (command, params, item) ->
72+
if(command == "setsecretkey")
73+
secret = params
74+
if(secret == "off")
75+
secret = null
76+
currentRoomId = item.rid
77+
localStorage.setItem("rocket.chat.sharedSecretKey.#{currentRoomId}", secret)
78+
79+
80+
81+
RocketChat.callbacks.add 'renderMessage', DecryptMessage, -9999 #FIRST
82+
RocketChat.slashCommands.add 'setsecretkey', HandleSlashCommand

0 commit comments

Comments
 (0)