/
encrypt-posts.js
220 lines (199 loc) · 7.37 KB
/
encrypt-posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { ajax } from "discourse/lib/ajax";
import { withPluginApi } from "discourse/lib/plugin-api";
import {
ENCRYPT_ACTIVE,
getEncryptionStatus,
getIdentity,
getTopicKey,
getUserIdentities,
hasTopicKey,
putTopicKey,
putTopicTitle,
} from "discourse/plugins/discourse-encrypt/lib/discourse";
import {
encrypt,
exportKey,
generateKey,
} from "discourse/plugins/discourse-encrypt/lib/protocol";
import I18n from "I18n";
import { Promise } from "rsvp";
/**
* Adds metadata extracted from the composer.
*
* @param {Object} metadata
*
* @return {Object}
*/
function addMetadata(metadata) {
const controller = Discourse.__container__.lookup("controller:composer");
const model = controller.model;
const currentUser = controller.currentUser;
const now = new Date().toISOString();
metadata.signed_by_id = currentUser.id;
metadata.signed_by_name = currentUser.username;
metadata.user_id = model.post ? model.post.user_id : model.user.id;
metadata.user_name = model.post ? model.post.username : model.user.username;
metadata.created_at = model.post ? model.post.created_at : now;
metadata.updated_at = now;
if (model.topic) {
metadata.topic_id = model.topic.id;
}
if (model.post) {
metadata.post_id = model.post.id;
metadata.post_number = model.post.post_number;
}
return metadata;
}
export default {
name: "encrypt-posts",
initialize(container) {
const currentUser = container.lookup("current-user:main");
if (getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
return;
}
withPluginApi("0.11.3", (api) => {
api.modifyClassStatic("model:topic", {
pluginId: "encrypt-posts",
update(topic, props) {
// TODO: https://github.com/emberjs/ember.js/issues/15291
const { _super } = this;
if (!hasTopicKey(topic.id)) {
return _super.call(this, ...arguments);
}
return getTopicKey(topic.id)
.then((key) => encrypt(key, { raw: props.title }))
.then((encryptedTitle) => {
props.title = I18n.t("encrypt.encrypted_title");
props.encrypted_title = encryptedTitle;
})
.then(() => _super.call(this, ...arguments));
},
});
api.modifyClass("adapter:post", {
pluginId: "encrypt-posts",
createRecord(store, type, args) {
// TODO: https://github.com/emberjs/ember.js/issues/15291
const { _super } = this;
if (!args.is_encrypted) {
return _super.call(this, ...arguments);
}
const identityPromise = getIdentity();
const topicKeyPromise = args.topic_id
? getTopicKey(args.topic_id)
: generateKey();
const { title, raw } = args;
const titlePromise = title
? topicKeyPromise
.then((key) => encrypt(key, { raw: title }))
.then(
(encryptedTitle) => (args.encrypted_title = encryptedTitle)
)
.finally(() => (args.title = I18n.t("encrypt.encrypted_title")))
: Promise.resolve();
const replyPromise = raw
? topicKeyPromise
.then((key) => encrypt(key, { raw }, { includeUploads: true }))
.then((encryptedRaw) => (args.encrypted_raw = encryptedRaw))
.finally(() => (args.raw = I18n.t("encrypt.encrypted_post")))
: Promise.resolve();
let encryptedKeysPromise = Promise.resolve();
let usernames = [];
if (args.target_recipients) {
usernames = args.target_recipients.split(",");
}
if (usernames.length > 0) {
usernames.push(currentUser.username);
encryptedKeysPromise = Promise.all([
topicKeyPromise,
getUserIdentities(usernames),
])
.then(([key, identities]) => {
const promises = [];
for (let i = 0; i < usernames.length; ++i) {
promises.push(
exportKey(key, identities[usernames[i]].encryptPublic)
);
}
return Promise.all(promises);
})
.then((userKeys) => {
args.encrypted_keys = {};
for (let i = 0; i < userKeys.length; ++i) {
args.encrypted_keys[usernames[i]] = userKeys[i];
}
args.encrypted_keys = JSON.stringify(args.encrypted_keys);
})
.catch((username) => {
bootbox.alert(
I18n.t("encrypt.composer.user_has_no_key", { username })
);
return Promise.reject(username);
});
}
// Hide any information that might give a hint of what this message
// may contain.
args.composer_open_duration_msecs = 10000;
args.typing_duration_msecs = 10000;
return Promise.all([titlePromise, replyPromise, encryptedKeysPromise])
.then(() => _super.call(this, ...arguments))
.then((result) =>
Promise.all([topicKeyPromise, titlePromise, identityPromise])
.then(([key, encTitle, identity]) => {
putTopicKey(result.payload.topic_id, key);
putTopicTitle(result.payload.topic_id, encTitle);
return encrypt(
key,
{
raw,
signed_by_id: currentUser.id,
signed_by_name: currentUser.username,
user_id: result.payload.user_id,
user_name: result.payload.username,
topic_id: result.payload.topic_id,
post_id: result.payload.id,
post_number: result.payload.post_number,
created_at: result.payload.created_at,
updated_at: result.payload.updated_at,
},
{
signKey: identity.signPrivate,
includeUploads: true,
}
).then((encryptedRaw) => {
result.payload.encrypted_raw = encryptedRaw;
return ajax("/encrypt/post", {
type: "PUT",
data: {
post_id: result.payload.id,
encrypted_raw: encryptedRaw,
},
});
});
})
.then(() => result)
);
},
update(store, type, id, attrs) {
// TODO: https://github.com/emberjs/ember.js/issues/15291
const { _super } = this;
if (!hasTopicKey(attrs.topic_id)) {
return _super.call(this, ...arguments);
}
return Promise.all([getTopicKey(attrs.topic_id), getIdentity()])
.then(([key, identity]) =>
encrypt(key, addMetadata({ raw: attrs.raw }), {
signKey: identity.signPrivate,
includeUploads: true,
})
)
.then((encryptedRaw) => {
delete attrs.cooked;
delete attrs.raw_old;
attrs.raw = encryptedRaw;
})
.then(() => _super.call(this, ...arguments));
},
});
});
},
};