omemo.js 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. (function (root, factory) {
  2. define(["jasmine", "mock", "test-utils"], factory);
  3. } (this, function (jasmine, mock, test_utils) {
  4. const { $iq, $pres, $msg, _, Strophe } = converse.env;
  5. const u = converse.env.utils;
  6. async function deviceListFetched (_converse, jid) {
  7. const stanza = await test_utils.waitUntil(() => _.filter(
  8. _converse.connection.IQ_stanzas,
  9. iq => iq.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.devicelist"]`)
  10. ).pop());
  11. await test_utils.waitUntil(() => _converse.devicelists.get(jid));
  12. return stanza;
  13. }
  14. function ownDeviceHasBeenPublished (_converse) {
  15. return _.filter(
  16. _converse.connection.IQ_stanzas,
  17. iq => iq.querySelector('iq[from="'+_converse.bare_jid+'"] publish[node="eu.siacs.conversations.axolotl.devicelist"]')
  18. ).pop();
  19. }
  20. function bundleHasBeenPublished (_converse) {
  21. return _.filter(
  22. _converse.connection.IQ_stanzas,
  23. iq => iq.querySelector('publish[node="eu.siacs.conversations.axolotl.bundles:123456789"]')
  24. ).pop();
  25. }
  26. function bundleFetched (_converse, jid, device_id) {
  27. return _.filter(
  28. _converse.connection.IQ_stanzas,
  29. iq => iq.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.bundles:${device_id}"]`)
  30. ).pop();
  31. }
  32. async function initializedOMEMO (_converse) {
  33. await test_utils.waitUntilDiscoConfirmed(
  34. _converse, _converse.bare_jid,
  35. [{'category': 'pubsub', 'type': 'pep'}],
  36. ['http://jabber.org/protocol/pubsub#publish-options']
  37. );
  38. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  39. let stanza = $iq({
  40. 'from': _converse.bare_jid,
  41. 'id': iq_stanza.getAttribute('id'),
  42. 'to': _converse.bare_jid,
  43. 'type': 'result',
  44. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  45. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  46. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  47. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  48. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  49. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  50. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse))
  51. stanza = $iq({
  52. 'from': _converse.bare_jid,
  53. 'id': iq_stanza.getAttribute('id'),
  54. 'to': _converse.bare_jid,
  55. 'type': 'result'});
  56. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  57. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse))
  58. stanza = $iq({
  59. 'from': _converse.bare_jid,
  60. 'id': iq_stanza.getAttribute('id'),
  61. 'to': _converse.bare_jid,
  62. 'type': 'result'});
  63. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  64. await _converse.api.waitUntil('OMEMOInitialized');
  65. }
  66. describe("The OMEMO module", function() {
  67. it("adds methods for encrypting and decrypting messages via AES GCM",
  68. mock.initConverse(
  69. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  70. async function (done, _converse) {
  71. const message = 'This message will be encrypted'
  72. test_utils.createContacts(_converse, 'current', 1);
  73. _converse.api.trigger('rosterContactsFetched');
  74. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  75. const view = await test_utils.openChatBoxFor(_converse, contact_jid);
  76. const payload = await view.model.encryptMessage(message);
  77. const result = await view.model.decryptMessage(payload);
  78. expect(result).toBe(message);
  79. done();
  80. }));
  81. it("enables encrypted messages to be sent and received",
  82. mock.initConverse(
  83. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  84. async function (done, _converse) {
  85. let sent_stanza;
  86. test_utils.createContacts(_converse, 'current', 1);
  87. _converse.api.trigger('rosterContactsFetched');
  88. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  89. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  90. await test_utils.openChatBoxFor(_converse, contact_jid);
  91. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  92. let stanza = $iq({
  93. 'from': contact_jid,
  94. 'id': iq_stanza.getAttribute('id'),
  95. 'to': _converse.connection.jid,
  96. 'type': 'result',
  97. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  98. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  99. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  100. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  101. .c('device', {'id': '555'});
  102. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  103. await test_utils.waitUntil(() => _converse.omemo_store);
  104. const devicelist = _converse.devicelists.get({'jid': contact_jid});
  105. await test_utils.waitUntil(() => devicelist.devices.length === 1);
  106. const view = _converse.chatboxviews.get(contact_jid);
  107. view.model.set('omemo_active', true);
  108. const textarea = view.el.querySelector('.chat-textarea');
  109. textarea.value = 'This message will be encrypted';
  110. view.onKeyDown({
  111. target: textarea,
  112. preventDefault: _.noop,
  113. keyCode: 13 // Enter
  114. });
  115. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
  116. stanza = $iq({
  117. 'from': contact_jid,
  118. 'id': iq_stanza.getAttribute('id'),
  119. 'to': _converse.bare_jid,
  120. 'type': 'result',
  121. }).c('pubsub', {
  122. 'xmlns': 'http://jabber.org/protocol/pubsub'
  123. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  124. .c('item')
  125. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  126. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  127. .c('signedPreKeySignature').t(btoa('2222')).up()
  128. .c('identityKey').t(btoa('3333')).up()
  129. .c('prekeys')
  130. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  131. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  132. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  133. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  134. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  135. stanza = $iq({
  136. 'from': _converse.bare_jid,
  137. 'id': iq_stanza.getAttribute('id'),
  138. 'to': _converse.bare_jid,
  139. 'type': 'result',
  140. }).c('pubsub', {
  141. 'xmlns': 'http://jabber.org/protocol/pubsub'
  142. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  143. .c('item')
  144. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  145. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  146. .c('signedPreKeySignature').t(btoa('200000')).up()
  147. .c('identityKey').t(btoa('300000')).up()
  148. .c('prekeys')
  149. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  150. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  151. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  152. spyOn(_converse.connection, 'send').and.callFake(stanza => { sent_stanza = stanza });
  153. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  154. await test_utils.waitUntil(() => sent_stanza);
  155. expect(sent_stanza.toLocaleString()).toBe(
  156. `<message from="romeo@montague.lit/orchard" id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  157. `to="mercutio@montague.lit" `+
  158. `type="chat" xmlns="jabber:client">`+
  159. `<body>This is an OMEMO encrypted message which your client doesn’t seem to support. Find more information on https://conversations.im/omemo</body>`+
  160. `<request xmlns="urn:xmpp:receipts"/>`+
  161. `<encrypted xmlns="eu.siacs.conversations.axolotl">`+
  162. `<header sid="123456789">`+
  163. `<key rid="482886413b977930064a5888b92134fe">YzFwaDNSNzNYNw==</key>`+
  164. `<key rid="123456789">YzFwaDNSNzNYNw==</key>`+
  165. `<key rid="555">YzFwaDNSNzNYNw==</key>`+
  166. `<iv>${sent_stanza.nodeTree.querySelector("iv").textContent}</iv>`+
  167. `</header>`+
  168. `<payload>${sent_stanza.nodeTree.querySelector("payload").textContent}</payload>`+
  169. `</encrypted>`+
  170. `<store xmlns="urn:xmpp:hints"/>`+
  171. `</message>`);
  172. // Test reception of an encrypted message
  173. let obj = await view.model.encryptMessage('This is an encrypted message from the contact')
  174. // XXX: Normally the key will be encrypted via libsignal.
  175. // However, we're mocking libsignal in the tests, so we include
  176. // it as plaintext in the message.
  177. stanza = $msg({
  178. 'from': contact_jid,
  179. 'to': _converse.connection.jid,
  180. 'type': 'chat',
  181. 'id': _converse.connection.getUniqueId()
  182. }).c('body').t('This is a fallback message').up()
  183. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  184. .c('header', {'sid': '555'})
  185. .c('key', {'rid': _converse.omemo_store.get('device_id')}).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  186. .c('iv').t(obj.iv)
  187. .up().up()
  188. .c('payload').t(obj.payload);
  189. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  190. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  191. expect(view.model.messages.length).toBe(2);
  192. expect(view.el.querySelectorAll('.chat-msg__body')[1].textContent.trim())
  193. .toBe('This is an encrypted message from the contact');
  194. // #1193 Check for a received message without <body> tag
  195. obj = await view.model.encryptMessage('Another received encrypted message without fallback')
  196. stanza = $msg({
  197. 'from': contact_jid,
  198. 'to': _converse.connection.jid,
  199. 'type': 'chat',
  200. 'id': _converse.connection.getUniqueId()
  201. }).c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  202. .c('header', {'sid': '555'})
  203. .c('key', {'rid': _converse.omemo_store.get('device_id')}).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  204. .c('iv').t(obj.iv)
  205. .up().up()
  206. .c('payload').t(obj.payload);
  207. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  208. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  209. await test_utils.waitUntil(() => view.model.messages.length > 1);
  210. expect(view.model.messages.length).toBe(3);
  211. expect(view.el.querySelectorAll('.chat-msg__body')[2].textContent.trim())
  212. .toBe('Another received encrypted message without fallback');
  213. done();
  214. }));
  215. it("enables encrypted groupchat messages to be sent and received",
  216. mock.initConverse(
  217. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  218. async function (done, _converse) {
  219. // MEMO encryption works only in members only conferences
  220. // that are non-anonymous.
  221. const features = [
  222. 'http://jabber.org/protocol/muc',
  223. 'jabber:iq:register',
  224. 'muc_passwordprotected',
  225. 'muc_hidden',
  226. 'muc_temporary',
  227. 'muc_membersonly',
  228. 'muc_unmoderated',
  229. 'muc_nonanonymous'
  230. ];
  231. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'montague.lit', 'romeo', features);
  232. const view = _converse.chatboxviews.get('lounge@montague.lit');
  233. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  234. const toolbar = view.el.querySelector('.chat-toolbar');
  235. let toggle = toolbar.querySelector('.toggle-omemo');
  236. toggle.click();
  237. expect(view.model.get('omemo_active')).toBe(true);
  238. // newguy enters the room
  239. const contact_jid = 'newguy@montague.lit';
  240. let stanza = $pres({
  241. 'to': 'romeo@montague.lit/orchard',
  242. 'from': 'lounge@montague.lit/newguy'
  243. })
  244. .c('x', {xmlns: Strophe.NS.MUC_USER})
  245. .c('item', {
  246. 'affiliation': 'none',
  247. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  248. 'role': 'participant'
  249. }).tree();
  250. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  251. // Wait for Converse to fetch newguy's device list
  252. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  253. expect(Strophe.serialize(iq_stanza)).toBe(
  254. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  255. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  256. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  257. `</pubsub>`+
  258. `</iq>`);
  259. // The server returns his device list
  260. stanza = $iq({
  261. 'from': contact_jid,
  262. 'id': iq_stanza.getAttribute('id'),
  263. 'to': _converse.bare_jid,
  264. 'type': 'result',
  265. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  266. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  267. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  268. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  269. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  270. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  271. await test_utils.waitUntil(() => _converse.omemo_store);
  272. expect(_converse.devicelists.length).toBe(2);
  273. await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  274. const devicelist = _converse.devicelists.get(contact_jid);
  275. expect(devicelist.devices.length).toBe(1);
  276. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  277. toggle = toolbar.querySelector('.toggle-omemo');
  278. expect(view.model.get('omemo_active')).toBe(true);
  279. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  280. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  281. const textarea = view.el.querySelector('.chat-textarea');
  282. textarea.value = 'This message will be encrypted';
  283. view.onKeyDown({
  284. target: textarea,
  285. preventDefault: _.noop,
  286. keyCode: 13 // Enter
  287. });
  288. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '4e30f35051b7b8b42abe083742187228'), 1000);
  289. console.log("Bundle fetched 4e30f35051b7b8b42abe083742187228");
  290. stanza = $iq({
  291. 'from': contact_jid,
  292. 'id': iq_stanza.getAttribute('id'),
  293. 'to': _converse.bare_jid,
  294. 'type': 'result',
  295. }).c('pubsub', {
  296. 'xmlns': 'http://jabber.org/protocol/pubsub'
  297. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:4e30f35051b7b8b42abe083742187228"})
  298. .c('item')
  299. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  300. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  301. .c('signedPreKeySignature').t(btoa('2222')).up()
  302. .c('identityKey').t(btoa('3333')).up()
  303. .c('prekeys')
  304. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  305. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  306. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  307. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  308. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'), 1000);
  309. console.log("Bundle fetched 482886413b977930064a5888b92134fe");
  310. stanza = $iq({
  311. 'from': _converse.bare_jid,
  312. 'id': iq_stanza.getAttribute('id'),
  313. 'to': _converse.bare_jid,
  314. 'type': 'result',
  315. }).c('pubsub', {
  316. 'xmlns': 'http://jabber.org/protocol/pubsub'
  317. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  318. .c('item')
  319. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  320. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  321. .c('signedPreKeySignature').t(btoa('200000')).up()
  322. .c('identityKey').t(btoa('300000')).up()
  323. .c('prekeys')
  324. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  325. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  326. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  327. spyOn(_converse.connection, 'send');
  328. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  329. await test_utils.waitUntil(() => _converse.connection.send.calls.count(), 1000);
  330. const sent_stanza = _converse.connection.send.calls.all()[0].args[0];
  331. expect(Strophe.serialize(sent_stanza)).toBe(
  332. `<message from="romeo@montague.lit/orchard" `+
  333. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  334. `to="lounge@montague.lit" `+
  335. `type="groupchat" `+
  336. `xmlns="jabber:client">`+
  337. `<body>This is an OMEMO encrypted message which your client doesn’t seem to support. Find more information on https://conversations.im/omemo</body>`+
  338. `<encrypted xmlns="eu.siacs.conversations.axolotl">`+
  339. `<header sid="123456789">`+
  340. `<key rid="482886413b977930064a5888b92134fe">YzFwaDNSNzNYNw==</key>`+
  341. `<key rid="123456789">YzFwaDNSNzNYNw==</key>`+
  342. `<key rid="4e30f35051b7b8b42abe083742187228">YzFwaDNSNzNYNw==</key>`+
  343. `<iv>${sent_stanza.nodeTree.querySelector("iv").textContent}</iv>`+
  344. `</header>`+
  345. `<payload>${sent_stanza.nodeTree.querySelector("payload").textContent}</payload>`+
  346. `</encrypted>`+
  347. `<store xmlns="urn:xmpp:hints"/>`+
  348. `</message>`);
  349. done();
  350. }));
  351. it("will create a new device based on a received carbon message",
  352. mock.initConverse(
  353. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  354. async function (done, _converse) {
  355. await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [Strophe.NS.SID]);
  356. let sent_stanza;
  357. test_utils.createContacts(_converse, 'current', 1);
  358. _converse.api.trigger('rosterContactsFetched');
  359. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  360. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  361. await test_utils.openChatBoxFor(_converse, contact_jid);
  362. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  363. const stanza = $iq({
  364. 'from': contact_jid,
  365. 'id': iq_stanza.getAttribute('id'),
  366. 'to': _converse.connection.jid,
  367. 'type': 'result',
  368. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  369. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  370. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  371. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  372. .c('device', {'id': '555'});
  373. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  374. await test_utils.waitUntil(() => _converse.omemo_store);
  375. const devicelist = _converse.devicelists.get({'jid': contact_jid});
  376. await test_utils.waitUntil(() => devicelist.devices.length === 1);
  377. const view = _converse.chatboxviews.get(contact_jid);
  378. view.model.set('omemo_active', true);
  379. // Test reception of an encrypted carbon message
  380. const obj = await view.model.encryptMessage('This is an encrypted carbon message from another device of mine')
  381. const carbon = u.toStanza(`
  382. <message xmlns="jabber:client" to="romeo@montague.lit/orchard" from="romeo@montague.lit" type="chat">
  383. <sent xmlns="urn:xmpp:carbons:2">
  384. <forwarded xmlns="urn:xmpp:forward:0">
  385. <message xmlns="jabber:client"
  386. from="romeo@montague.lit/gajim.HE02SW1L"
  387. xml:lang="en"
  388. to="${contact_jid}/gajim.0LATM5V2"
  389. type="chat" id="87141781-61d6-4eb3-9a31-429935a61b76">
  390. <archived xmlns="urn:xmpp:mam:tmp" by="romeo@montague.lit" id="1554033877043470"/>
  391. <stanza-id xmlns="urn:xmpp:sid:0" by="romeo@montague.lit" id="1554033877043470"/>
  392. <request xmlns="urn:xmpp:receipts"/>
  393. <active xmlns="http://jabber.org/protocol/chatstates"/>
  394. <origin-id xmlns="urn:xmpp:sid:0" id="87141781-61d6-4eb3-9a31-429935a61b76"/>
  395. <encrypted xmlns="eu.siacs.conversations.axolotl">
  396. <header sid="988349631">
  397. <key rid="${_converse.omemo_store.get('device_id')}"
  398. prekey="true">${u.arrayBufferToBase64(obj.key_and_tag)}</key>
  399. <iv>${obj.iv}</iv>
  400. </header>
  401. <payload>${obj.payload}</payload>
  402. </encrypted>
  403. <encryption xmlns="urn:xmpp:eme:0" namespace="eu.siacs.conversations.axolotl" name="OMEMO"/>
  404. <store xmlns="urn:xmpp:hints"/>
  405. </message>
  406. </forwarded>
  407. </sent>
  408. </message>
  409. `);
  410. _converse.connection._dataRecv(test_utils.createRequest(carbon));
  411. await new Promise(resolve => view.once('messageInserted', resolve));
  412. expect(view.model.messages.length).toBe(1);
  413. expect(view.el.querySelector('.chat-msg__body').textContent.trim())
  414. .toBe('This is an encrypted carbon message from another device of mine');
  415. expect(devicelist.devices.length).toBe(2);
  416. expect(devicelist.devices.at(0).get('id')).toBe('555');
  417. expect(devicelist.devices.at(1).get('id')).toBe('988349631');
  418. expect(devicelist.devices.get('988349631').get('active')).toBe(true);
  419. const textarea = view.el.querySelector('.chat-textarea');
  420. textarea.value = 'This is an encrypted message from this device';
  421. view.onKeyDown({
  422. target: textarea,
  423. preventDefault: _.noop,
  424. keyCode: 13 // Enter
  425. });
  426. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '988349631'));
  427. expect(Strophe.serialize(iq_stanza)).toBe(
  428. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${_converse.bare_jid}" type="get" xmlns="jabber:client">`+
  429. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  430. `<items node="eu.siacs.conversations.axolotl.bundles:988349631"/>`+
  431. `</pubsub>`+
  432. `</iq>`);
  433. done();
  434. }));
  435. it("gracefully handles auth errors when trying to send encrypted groupchat messages",
  436. mock.initConverse(
  437. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  438. async function (done, _converse) {
  439. // MEMO encryption works only in members only conferences
  440. // that are non-anonymous.
  441. const features = [
  442. 'http://jabber.org/protocol/muc',
  443. 'jabber:iq:register',
  444. 'muc_passwordprotected',
  445. 'muc_hidden',
  446. 'muc_temporary',
  447. 'muc_membersonly',
  448. 'muc_unmoderated',
  449. 'muc_nonanonymous'
  450. ];
  451. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'montague.lit', 'romeo', features);
  452. const view = _converse.chatboxviews.get('lounge@montague.lit');
  453. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  454. const contact_jid = 'newguy@montague.lit';
  455. let stanza = $pres({
  456. 'to': 'romeo@montague.lit/orchard',
  457. 'from': 'lounge@montague.lit/newguy'
  458. })
  459. .c('x', {xmlns: Strophe.NS.MUC_USER})
  460. .c('item', {
  461. 'affiliation': 'none',
  462. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  463. 'role': 'participant'
  464. }).tree();
  465. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  466. const toolbar = view.el.querySelector('.chat-toolbar');
  467. const toggle = toolbar.querySelector('.toggle-omemo');
  468. toggle.click();
  469. expect(view.model.get('omemo_active')).toBe(true);
  470. expect(view.model.get('omemo_supported')).toBe(true);
  471. const textarea = view.el.querySelector('.chat-textarea');
  472. textarea.value = 'This message will be encrypted';
  473. view.onKeyDown({
  474. target: textarea,
  475. preventDefault: _.noop,
  476. keyCode: 13 // Enter
  477. });
  478. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  479. expect(Strophe.serialize(iq_stanza)).toBe(
  480. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  481. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  482. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  483. `</pubsub>`+
  484. `</iq>`);
  485. stanza = $iq({
  486. 'from': contact_jid,
  487. 'id': iq_stanza.getAttribute('id'),
  488. 'to': _converse.bare_jid,
  489. 'type': 'result',
  490. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  491. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  492. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  493. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  494. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  495. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  496. await test_utils.waitUntil(() => _converse.omemo_store);
  497. expect(_converse.devicelists.length).toBe(2);
  498. const devicelist = _converse.devicelists.get(contact_jid);
  499. await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  500. expect(devicelist.devices.length).toBe(1);
  501. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  502. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  503. stanza = $iq({
  504. 'from': _converse.bare_jid,
  505. 'id': iq_stanza.getAttribute('id'),
  506. 'to': _converse.bare_jid,
  507. 'type': 'result',
  508. }).c('pubsub', {
  509. 'xmlns': 'http://jabber.org/protocol/pubsub'
  510. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  511. .c('item')
  512. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  513. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  514. .c('signedPreKeySignature').t(btoa('200000')).up()
  515. .c('identityKey').t(btoa('300000')).up()
  516. .c('prekeys')
  517. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  518. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  519. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  520. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '4e30f35051b7b8b42abe083742187228'));
  521. /* <iq xmlns="jabber:client" to="jc@opkode.com/converse.js-34183907" type="error" id="945c8ab3-b561-4d8a-92da-77c226bb1689:sendIQ" from="joris@konuro.net">
  522. * <pubsub xmlns="http://jabber.org/protocol/pubsub">
  523. * <items node="eu.siacs.conversations.axolotl.bundles:7580"/>
  524. * </pubsub>
  525. * <error code="401" type="auth">
  526. * <presence-subscription-required xmlns="http://jabber.org/protocol/pubsub#errors"/>
  527. * <not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  528. * </error>
  529. * </iq>
  530. */
  531. stanza = $iq({
  532. 'from': contact_jid,
  533. 'id': iq_stanza.getAttribute('id'),
  534. 'to': _converse.bare_jid,
  535. 'type': 'result',
  536. }).c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub'})
  537. .c('items', {'node': "eu.siacs.conversations.axolotl.bundles:4e30f35051b7b8b42abe083742187228"}).up().up()
  538. .c('error', {'code': '401', 'type': 'auth'})
  539. .c('presence-subscription-required', {'xmlns':"http://jabber.org/protocol/pubsub#errors" }).up()
  540. .c('not-authorized', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  541. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  542. await test_utils.waitUntil(() => document.querySelectorAll('.alert-danger').length, 2000);
  543. const header = document.querySelector('.alert-danger .modal-title');
  544. expect(header.textContent).toBe("Error");
  545. expect(u.ancestor(header, '.modal-content').querySelector('.modal-body p').textContent.trim())
  546. .toBe("Sorry, we're unable to send an encrypted message because newguy@montague.lit requires you "+
  547. "to be subscribed to their presence in order to see their OMEMO information");
  548. expect(view.model.get('omemo_supported')).toBe(false);
  549. expect(view.el.querySelector('.chat-textarea').value).toBe('This message will be encrypted');
  550. done();
  551. }));
  552. it("can receive a PreKeySignalMessage",
  553. mock.initConverse(
  554. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  555. async function (done, _converse) {
  556. _converse.NUM_PREKEYS = 5; // Restrict to 5, otherwise the resulting stanza is too large to easily test
  557. let view, sent_stanza;
  558. test_utils.createContacts(_converse, 'current', 1);
  559. _converse.api.trigger('rosterContactsFetched');
  560. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  561. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  562. const obj = await _converse.ChatBox.prototype.encryptMessage('This is an encrypted message from the contact');
  563. // XXX: Normally the key will be encrypted via libsignal.
  564. // However, we're mocking libsignal in the tests, so we include
  565. // it as plaintext in the message.
  566. let stanza = $msg({
  567. 'from': contact_jid,
  568. 'to': _converse.connection.jid,
  569. 'type': 'chat',
  570. 'id': 'qwerty'
  571. }).c('body').t('This is a fallback message').up()
  572. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  573. .c('header', {'sid': '555'})
  574. .c('key', {
  575. 'prekey': 'true',
  576. 'rid': _converse.omemo_store.get('device_id')
  577. }).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  578. .c('iv').t(obj.iv)
  579. .up().up()
  580. .c('payload').t(obj.payload);
  581. const generateMissingPreKeys = _converse.omemo_store.generateMissingPreKeys;
  582. spyOn(_converse.omemo_store, 'generateMissingPreKeys').and.callFake(() => {
  583. // Since it's difficult to override
  584. // decryptPreKeyWhisperMessage, where a prekey will be
  585. // removed from the store, we do it here, before the
  586. // missing prekeys are generated.
  587. _converse.omemo_store.removePreKey(1);
  588. return generateMissingPreKeys.apply(_converse.omemo_store, arguments);
  589. });
  590. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  591. let iq_stanza = await test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid));
  592. iq_stanza = await deviceListFetched(_converse, contact_jid);
  593. stanza = $iq({
  594. 'from': contact_jid,
  595. 'id': iq_stanza.getAttribute('id'),
  596. 'to': _converse.connection.jid,
  597. 'type': 'result',
  598. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  599. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  600. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  601. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  602. .c('device', {'id': '555'});
  603. // XXX: the bundle gets published twice, we want to make sure
  604. // that we wait for the 2nd, so we clear all the already sent
  605. // stanzas.
  606. _converse.connection.IQ_stanzas = [];
  607. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  608. await test_utils.waitUntil(() => _converse.omemo_store);
  609. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  610. expect(Strophe.serialize(iq_stanza)).toBe(
  611. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" type="set" xmlns="jabber:client">`+
  612. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  613. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  614. `<item>`+
  615. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  616. `<signedPreKeyPublic signedPreKeyId="0">${btoa("1234")}</signedPreKeyPublic>`+
  617. `<signedPreKeySignature>${btoa("11112222333344445555")}</signedPreKeySignature>`+
  618. `<identityKey>${btoa("1234")}</identityKey>`+
  619. `<prekeys>`+
  620. `<preKeyPublic preKeyId="0">${btoa("1234")}</preKeyPublic>`+
  621. `<preKeyPublic preKeyId="1">${btoa("1234")}</preKeyPublic>`+
  622. `<preKeyPublic preKeyId="2">${btoa("1234")}</preKeyPublic>`+
  623. `<preKeyPublic preKeyId="3">${btoa("1234")}</preKeyPublic>`+
  624. `<preKeyPublic preKeyId="4">${btoa("1234")}</preKeyPublic>`+
  625. `</prekeys>`+
  626. `</bundle>`+
  627. `</item>`+
  628. `</publish>`+
  629. `<publish-options>`+
  630. `<x type="submit" xmlns="jabber:x:data">`+
  631. `<field type="hidden" var="FORM_TYPE">`+
  632. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  633. `</field>`+
  634. `<field var="pubsub#access_model">`+
  635. `<value>open</value>`+
  636. `</field>`+
  637. `</x>`+
  638. `</publish-options>`+
  639. `</pubsub>`+
  640. `</iq>`)
  641. const own_device = _converse.devicelists.get(_converse.bare_jid).devices.get(_converse.omemo_store.get('device_id'));
  642. expect(own_device.get('bundle').prekeys.length).toBe(5);
  643. expect(_converse.omemo_store.generateMissingPreKeys).toHaveBeenCalled();
  644. done();
  645. }));
  646. it("updates device lists based on PEP messages",
  647. mock.initConverse(
  648. null, ['rosterGroupsFetched'], {'allow_non_roster_messaging': true},
  649. async function (done, _converse) {
  650. await test_utils.waitUntilDiscoConfirmed(
  651. _converse, _converse.bare_jid,
  652. [{'category': 'pubsub', 'type': 'pep'}],
  653. ['http://jabber.org/protocol/pubsub#publish-options']
  654. );
  655. test_utils.createContacts(_converse, 'current', 1);
  656. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  657. // Wait until own devices are fetched
  658. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  659. expect(Strophe.serialize(iq_stanza)).toBe(
  660. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  661. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  662. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  663. `</pubsub>`+
  664. `</iq>`);
  665. let stanza = $iq({
  666. 'from': _converse.bare_jid,
  667. 'id': iq_stanza.getAttribute('id'),
  668. 'to': _converse.bare_jid,
  669. 'type': 'result',
  670. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  671. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  672. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  673. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  674. .c('device', {'id': '555'});
  675. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  676. await test_utils.waitUntil(() => _converse.omemo_store);
  677. expect(_converse.chatboxes.length).toBe(1);
  678. expect(_converse.devicelists.length).toBe(1);
  679. const devicelist = _converse.devicelists.get(_converse.bare_jid);
  680. expect(devicelist.devices.length).toBe(2);
  681. expect(devicelist.devices.at(0).get('id')).toBe('555');
  682. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  683. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  684. stanza = $iq({
  685. 'from': _converse.bare_jid,
  686. 'id': iq_stanza.getAttribute('id'),
  687. 'to': _converse.bare_jid,
  688. 'type': 'result'});
  689. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  690. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  691. stanza = $iq({
  692. 'from': _converse.bare_jid,
  693. 'id': iq_stanza.getAttribute('id'),
  694. 'to': _converse.bare_jid,
  695. 'type': 'result'});
  696. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  697. await _converse.api.waitUntil('OMEMOInitialized');
  698. stanza = $msg({
  699. 'from': contact_jid,
  700. 'to': _converse.bare_jid,
  701. 'type': 'headline',
  702. 'id': 'update_01',
  703. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  704. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  705. .c('item')
  706. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  707. .c('device', {'id': '1234'})
  708. .c('device', {'id': '4223'})
  709. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  710. expect(_converse.devicelists.length).toBe(2);
  711. let devices = _converse.devicelists.get(contact_jid).devices;
  712. expect(devices.length).toBe(2);
  713. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('1234,4223');
  714. stanza = $msg({
  715. 'from': contact_jid,
  716. 'to': _converse.bare_jid,
  717. 'type': 'headline',
  718. 'id': 'update_02',
  719. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  720. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  721. .c('item')
  722. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  723. .c('device', {'id': '4223'})
  724. .c('device', {'id': '4224'})
  725. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  726. expect(_converse.devicelists.length).toBe(2);
  727. expect(devices.length).toBe(3);
  728. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('1234,4223,4224');
  729. expect(devices.get('1234').get('active')).toBe(false);
  730. expect(devices.get('4223').get('active')).toBe(true);
  731. expect(devices.get('4224').get('active')).toBe(true);
  732. // Check that own devicelist gets updated
  733. stanza = $msg({
  734. 'from': _converse.bare_jid,
  735. 'to': _converse.bare_jid,
  736. 'type': 'headline',
  737. 'id': 'update_03',
  738. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  739. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  740. .c('item')
  741. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  742. .c('device', {'id': '123456789'})
  743. .c('device', {'id': '555'})
  744. .c('device', {'id': '777'})
  745. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  746. expect(_converse.devicelists.length).toBe(2);
  747. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  748. expect(devices.length).toBe(3);
  749. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,555,777');
  750. expect(devices.get('123456789').get('active')).toBe(true);
  751. expect(devices.get('555').get('active')).toBe(true);
  752. expect(devices.get('777').get('active')).toBe(true);
  753. _converse.connection.IQ_stanzas = [];
  754. // Check that own device gets re-added
  755. stanza = $msg({
  756. 'from': _converse.bare_jid,
  757. 'to': _converse.bare_jid,
  758. 'type': 'headline',
  759. 'id': 'update_04',
  760. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  761. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  762. .c('item')
  763. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  764. .c('device', {'id': '444'})
  765. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  766. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  767. // Check that our own device is added again, but that removed
  768. // devices are not added.
  769. expect(Strophe.serialize(iq_stanza)).toBe(
  770. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute(`id`)}" type="set" xmlns="jabber:client">`+
  771. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  772. `<publish node="eu.siacs.conversations.axolotl.devicelist">`+
  773. `<item>`+
  774. `<list xmlns="eu.siacs.conversations.axolotl">`+
  775. `<device id="123456789"/>`+
  776. `<device id="444"/>`+
  777. `</list>`+
  778. `</item>`+
  779. `</publish>`+
  780. `<publish-options>`+
  781. `<x type="submit" xmlns="jabber:x:data">`+
  782. `<field type="hidden" var="FORM_TYPE">`+
  783. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  784. `</field>`+
  785. `<field var="pubsub#access_model">`+
  786. `<value>open</value>`+
  787. `</field>`+
  788. `</x>`+
  789. `</publish-options>`+
  790. `</pubsub>`+
  791. `</iq>`);
  792. expect(_converse.devicelists.length).toBe(2);
  793. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  794. // The device id for this device (123456789) was also generated and added to the list,
  795. // which is why we have 2 devices now.
  796. expect(devices.length).toBe(4);
  797. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,444,555,777');
  798. expect(devices.get('123456789').get('active')).toBe(true);
  799. expect(devices.get('444').get('active')).toBe(true);
  800. expect(devices.get('555').get('active')).toBe(false);
  801. expect(devices.get('777').get('active')).toBe(false);
  802. done();
  803. }));
  804. it("updates device bundles based on PEP messages",
  805. mock.initConverse(
  806. null, ['rosterGroupsFetched'], {},
  807. async function (done, _converse) {
  808. await test_utils.waitUntilDiscoConfirmed(
  809. _converse, _converse.bare_jid,
  810. [{'category': 'pubsub', 'type': 'pep'}],
  811. ['http://jabber.org/protocol/pubsub#publish-options']
  812. );
  813. test_utils.createContacts(_converse, 'current');
  814. const contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  815. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  816. expect(Strophe.serialize(iq_stanza)).toBe(
  817. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  818. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  819. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  820. `</pubsub>`+
  821. `</iq>`);
  822. let stanza = $iq({
  823. 'from': contact_jid,
  824. 'id': iq_stanza.getAttribute('id'),
  825. 'to': _converse.bare_jid,
  826. 'type': 'result',
  827. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  828. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  829. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  830. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  831. .c('device', {'id': '555'});
  832. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  833. await await test_utils.waitUntil(() => _converse.omemo_store);
  834. expect(_converse.devicelists.length).toBe(1);
  835. let devicelist = _converse.devicelists.get(_converse.bare_jid);
  836. expect(devicelist.devices.length).toBe(2);
  837. expect(devicelist.devices.at(0).get('id')).toBe('555');
  838. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  839. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  840. stanza = $iq({
  841. 'from': _converse.bare_jid,
  842. 'id': iq_stanza.getAttribute('id'),
  843. 'to': _converse.bare_jid,
  844. 'type': 'result'});
  845. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  846. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  847. stanza = $iq({
  848. 'from': _converse.bare_jid,
  849. 'id': iq_stanza.getAttribute('id'),
  850. 'to': _converse.bare_jid,
  851. 'type': 'result'});
  852. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  853. await _converse.api.waitUntil('OMEMOInitialized');
  854. stanza = $msg({
  855. 'from': contact_jid,
  856. 'to': _converse.bare_jid,
  857. 'type': 'headline',
  858. 'id': 'update_01',
  859. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  860. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  861. .c('item')
  862. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  863. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('1111').up()
  864. .c('signedPreKeySignature').t('2222').up()
  865. .c('identityKey').t('3333').up()
  866. .c('prekeys')
  867. .c('preKeyPublic', {'preKeyId': '1001'}).up()
  868. .c('preKeyPublic', {'preKeyId': '1002'}).up()
  869. .c('preKeyPublic', {'preKeyId': '1003'});
  870. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  871. expect(_converse.devicelists.length).toBe(2);
  872. devicelist = _converse.devicelists.get(contact_jid);
  873. expect(devicelist.devices.length).toBe(1);
  874. let device = devicelist.devices.at(0);
  875. expect(device.get('bundle').identity_key).toBe('3333');
  876. expect(device.get('bundle').signed_prekey.public_key).toBe('1111');
  877. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  878. expect(device.get('bundle').signed_prekey.signature).toBe('2222');
  879. expect(device.get('bundle').prekeys.length).toBe(3);
  880. expect(device.get('bundle').prekeys[0].id).toBe(1001);
  881. expect(device.get('bundle').prekeys[1].id).toBe(1002);
  882. expect(device.get('bundle').prekeys[2].id).toBe(1003);
  883. stanza = $msg({
  884. 'from': contact_jid,
  885. 'to': _converse.bare_jid,
  886. 'type': 'headline',
  887. 'id': 'update_02',
  888. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  889. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  890. .c('item')
  891. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  892. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('5555').up()
  893. .c('signedPreKeySignature').t('6666').up()
  894. .c('identityKey').t('7777').up()
  895. .c('prekeys')
  896. .c('preKeyPublic', {'preKeyId': '2001'}).up()
  897. .c('preKeyPublic', {'preKeyId': '2002'}).up()
  898. .c('preKeyPublic', {'preKeyId': '2003'});
  899. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  900. expect(_converse.devicelists.length).toBe(2);
  901. devicelist = _converse.devicelists.get(contact_jid);
  902. expect(devicelist.devices.length).toBe(1);
  903. device = devicelist.devices.at(0);
  904. expect(device.get('bundle').identity_key).toBe('7777');
  905. expect(device.get('bundle').signed_prekey.public_key).toBe('5555');
  906. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  907. expect(device.get('bundle').signed_prekey.signature).toBe('6666');
  908. expect(device.get('bundle').prekeys.length).toBe(3);
  909. expect(device.get('bundle').prekeys[0].id).toBe(2001);
  910. expect(device.get('bundle').prekeys[1].id).toBe(2002);
  911. expect(device.get('bundle').prekeys[2].id).toBe(2003);
  912. stanza = $msg({
  913. 'from': _converse.bare_jid,
  914. 'to': _converse.bare_jid,
  915. 'type': 'headline',
  916. 'id': 'update_03',
  917. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  918. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:123456789'})
  919. .c('item')
  920. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  921. .c('signedPreKeyPublic', {'signedPreKeyId': '9999'}).t('8888').up()
  922. .c('signedPreKeySignature').t('3333').up()
  923. .c('identityKey').t('1111').up()
  924. .c('prekeys')
  925. .c('preKeyPublic', {'preKeyId': '3001'}).up()
  926. .c('preKeyPublic', {'preKeyId': '3002'}).up()
  927. .c('preKeyPublic', {'preKeyId': '3003'});
  928. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  929. expect(_converse.devicelists.length).toBe(2);
  930. devicelist = _converse.devicelists.get(_converse.bare_jid);
  931. expect(devicelist.devices.length).toBe(2);
  932. expect(devicelist.devices.at(0).get('id')).toBe('555');
  933. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  934. device = devicelist.devices.at(1);
  935. expect(device.get('bundle').identity_key).toBe('1111');
  936. expect(device.get('bundle').signed_prekey.public_key).toBe('8888');
  937. expect(device.get('bundle').signed_prekey.id).toBe(9999);
  938. expect(device.get('bundle').signed_prekey.signature).toBe('3333');
  939. expect(device.get('bundle').prekeys.length).toBe(3);
  940. expect(device.get('bundle').prekeys[0].id).toBe(3001);
  941. expect(device.get('bundle').prekeys[1].id).toBe(3002);
  942. expect(device.get('bundle').prekeys[2].id).toBe(3003);
  943. done();
  944. }));
  945. it("publishes a bundle with which an encrypted session can be created",
  946. mock.initConverse(
  947. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  948. async function (done, _converse) {
  949. await test_utils.waitUntilDiscoConfirmed(
  950. _converse, _converse.bare_jid,
  951. [{'category': 'pubsub', 'type': 'pep'}],
  952. ['http://jabber.org/protocol/pubsub#publish-options']
  953. );
  954. _converse.NUM_PREKEYS = 2; // Restrict to 2, otherwise the resulting stanza is too large to easily test
  955. test_utils.createContacts(_converse, 'current', 1);
  956. _converse.api.trigger('rosterContactsFetched');
  957. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  958. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  959. let stanza = $iq({
  960. 'from': contact_jid,
  961. 'id': iq_stanza.getAttribute('id'),
  962. 'to': _converse.bare_jid,
  963. 'type': 'result',
  964. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  965. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  966. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  967. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  968. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  969. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  970. expect(_converse.devicelists.length).toBe(1);
  971. await test_utils.openChatBoxFor(_converse, contact_jid);
  972. iq_stanza = await ownDeviceHasBeenPublished(_converse);
  973. stanza = $iq({
  974. 'from': _converse.bare_jid,
  975. 'id': iq_stanza.getAttribute('id'),
  976. 'to': _converse.bare_jid,
  977. 'type': 'result'});
  978. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  979. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  980. expect(Strophe.serialize(iq_stanza)).toBe(
  981. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" type="set" xmlns="jabber:client">`+
  982. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  983. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  984. `<item>`+
  985. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  986. `<signedPreKeyPublic signedPreKeyId="0">${btoa("1234")}</signedPreKeyPublic>`+
  987. `<signedPreKeySignature>${btoa("11112222333344445555")}</signedPreKeySignature>`+
  988. `<identityKey>${btoa("1234")}</identityKey>`+
  989. `<prekeys>`+
  990. `<preKeyPublic preKeyId="0">${btoa("1234")}</preKeyPublic>`+
  991. `<preKeyPublic preKeyId="1">${btoa("1234")}</preKeyPublic>`+
  992. `</prekeys>`+
  993. `</bundle>`+
  994. `</item>`+
  995. `</publish>`+
  996. `<publish-options>`+
  997. `<x type="submit" xmlns="jabber:x:data">`+
  998. `<field type="hidden" var="FORM_TYPE">`+
  999. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  1000. `</field>`+
  1001. `<field var="pubsub#access_model">`+
  1002. `<value>open</value>`+
  1003. `</field>`+
  1004. `</x>`+
  1005. `</publish-options>`+
  1006. `</pubsub>`+
  1007. `</iq>`)
  1008. stanza = $iq({
  1009. 'from': _converse.bare_jid,
  1010. 'id': iq_stanza.getAttribute('id'),
  1011. 'to': _converse.bare_jid,
  1012. 'type': 'result'});
  1013. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1014. await _converse.api.waitUntil('OMEMOInitialized');
  1015. done();
  1016. }));
  1017. it("adds a toolbar button for starting an encrypted chat session",
  1018. mock.initConverse(
  1019. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1020. async function (done, _converse) {
  1021. await test_utils.waitUntilDiscoConfirmed(
  1022. _converse, _converse.bare_jid,
  1023. [{'category': 'pubsub', 'type': 'pep'}],
  1024. ['http://jabber.org/protocol/pubsub#publish-options']
  1025. );
  1026. test_utils.createContacts(_converse, 'current', 1);
  1027. _converse.api.trigger('rosterContactsFetched');
  1028. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1029. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  1030. expect(Strophe.serialize(iq_stanza)).toBe(
  1031. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
  1032. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1033. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1034. `</pubsub>`+
  1035. `</iq>`);
  1036. let stanza = $iq({
  1037. 'from': _converse.bare_jid,
  1038. 'id': iq_stanza.getAttribute('id'),
  1039. 'to': _converse.bare_jid,
  1040. 'type': 'result',
  1041. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1042. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1043. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1044. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1045. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  1046. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1047. await test_utils.waitUntil(() => _converse.omemo_store);
  1048. expect(_converse.devicelists.length).toBe(1);
  1049. let devicelist = _converse.devicelists.get(_converse.bare_jid);
  1050. expect(devicelist.devices.length).toBe(2);
  1051. expect(devicelist.devices.at(0).get('id')).toBe('482886413b977930064a5888b92134fe');
  1052. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  1053. // Check that own device was published
  1054. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  1055. expect(Strophe.serialize(iq_stanza)).toBe(
  1056. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute(`id`)}" type="set" xmlns="jabber:client">`+
  1057. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1058. `<publish node="eu.siacs.conversations.axolotl.devicelist">`+
  1059. `<item>`+
  1060. `<list xmlns="eu.siacs.conversations.axolotl">`+
  1061. `<device id="482886413b977930064a5888b92134fe"/>`+
  1062. `<device id="123456789"/>`+
  1063. `</list>`+
  1064. `</item>`+
  1065. `</publish>`+
  1066. `<publish-options>`+
  1067. `<x type="submit" xmlns="jabber:x:data">`+
  1068. `<field type="hidden" var="FORM_TYPE">`+
  1069. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  1070. `</field>`+
  1071. `<field var="pubsub#access_model">`+
  1072. `<value>open</value>`+
  1073. `</field>`+
  1074. `</x>`+
  1075. `</publish-options>`+
  1076. `</pubsub>`+
  1077. `</iq>`);
  1078. stanza = $iq({
  1079. 'from': _converse.bare_jid,
  1080. 'id': iq_stanza.getAttribute('id'),
  1081. 'to': _converse.bare_jid,
  1082. 'type': 'result'});
  1083. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1084. const iq_el = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  1085. expect(iq_el.getAttributeNames().sort().join()).toBe(["from", "type", "xmlns", "id"].sort().join());
  1086. expect(iq_el.querySelector('prekeys').childNodes.length).toBe(100);
  1087. const signed_prekeys = iq_el.querySelectorAll('signedPreKeyPublic');
  1088. expect(signed_prekeys.length).toBe(1);
  1089. const signed_prekey = signed_prekeys[0];
  1090. expect(signed_prekey.getAttribute('signedPreKeyId')).toBe('0')
  1091. expect(iq_el.querySelectorAll('signedPreKeySignature').length).toBe(1);
  1092. expect(iq_el.querySelectorAll('identityKey').length).toBe(1);
  1093. stanza = $iq({
  1094. 'from': _converse.bare_jid,
  1095. 'id': iq_el.getAttribute('id'),
  1096. 'to': _converse.bare_jid,
  1097. 'type': 'result'});
  1098. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1099. await _converse.api.waitUntil('OMEMOInitialized', 1000);
  1100. await test_utils.openChatBoxFor(_converse, contact_jid);
  1101. iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1102. expect(Strophe.serialize(iq_stanza)).toBe(
  1103. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1104. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1105. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1106. `</pubsub>`+
  1107. `</iq>`);
  1108. stanza = $iq({
  1109. 'from': contact_jid,
  1110. 'id': iq_stanza.getAttribute('id'),
  1111. 'to': _converse.bare_jid,
  1112. 'type': 'result',
  1113. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1114. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1115. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1116. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1117. .c('device', {'id': '368866411b877c30064a5f62b917cffe'}).up()
  1118. .c('device', {'id': '3300659945416e274474e469a1f0154c'}).up()
  1119. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  1120. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  1121. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1122. devicelist = _converse.devicelists.get(contact_jid);
  1123. await test_utils.waitUntil(() => devicelist.devices.length);
  1124. expect(_converse.devicelists.length).toBe(2);
  1125. devicelist = _converse.devicelists.get(contact_jid);
  1126. expect(devicelist.devices.length).toBe(4);
  1127. expect(devicelist.devices.at(0).get('id')).toBe('368866411b877c30064a5f62b917cffe');
  1128. expect(devicelist.devices.at(1).get('id')).toBe('3300659945416e274474e469a1f0154c');
  1129. expect(devicelist.devices.at(2).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  1130. expect(devicelist.devices.at(3).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  1131. await test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid).el.querySelector('.chat-toolbar'));
  1132. const view = _converse.chatboxviews.get(contact_jid);
  1133. const toolbar = view.el.querySelector('.chat-toolbar');
  1134. expect(view.model.get('omemo_active')).toBe(undefined);
  1135. let toggle = toolbar.querySelector('.toggle-omemo');
  1136. expect(_.isNull(toggle)).toBe(false);
  1137. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1138. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1139. spyOn(view, 'toggleOMEMO').and.callThrough();
  1140. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1141. toolbar.querySelector('.toggle-omemo').click();
  1142. expect(view.toggleOMEMO).toHaveBeenCalled();
  1143. expect(view.model.get('omemo_active')).toBe(true);
  1144. await test_utils.waitUntil(() => u.hasClass('fa-lock', toolbar.querySelector('.toggle-omemo')));
  1145. toggle = toolbar.querySelector('.toggle-omemo');
  1146. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1147. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1148. const textarea = view.el.querySelector('.chat-textarea');
  1149. textarea.value = 'This message will be sent encrypted';
  1150. view.onKeyDown({
  1151. target: textarea,
  1152. preventDefault: _.noop,
  1153. keyCode: 13
  1154. });
  1155. view.model.save({'omemo_supported': false});
  1156. toggle = toolbar.querySelector('.toggle-omemo');
  1157. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1158. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1159. expect(u.hasClass('disabled', toggle)).toBe(true);
  1160. view.model.save({'omemo_supported': true});
  1161. toggle = toolbar.querySelector('.toggle-omemo');
  1162. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1163. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1164. expect(u.hasClass('disabled', toggle)).toBe(false);
  1165. done();
  1166. }));
  1167. it("adds a toolbar button for starting an encrypted groupchat session",
  1168. mock.initConverse(
  1169. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'view_mode': 'fullscreen'},
  1170. async function (done, _converse) {
  1171. await test_utils.waitUntilDiscoConfirmed(
  1172. _converse, _converse.bare_jid,
  1173. [{'category': 'pubsub', 'type': 'pep'}],
  1174. ['http://jabber.org/protocol/pubsub#publish-options']
  1175. );
  1176. // MEMO encryption works only in members-only conferences that are non-anonymous.
  1177. const features = [
  1178. 'http://jabber.org/protocol/muc',
  1179. 'jabber:iq:register',
  1180. 'muc_passwordprotected',
  1181. 'muc_hidden',
  1182. 'muc_temporary',
  1183. 'muc_membersonly',
  1184. 'muc_unmoderated',
  1185. 'muc_nonanonymous'
  1186. ];
  1187. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'montague.lit', 'romeo', features);
  1188. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1189. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  1190. const toolbar = view.el.querySelector('.chat-toolbar');
  1191. let toggle = toolbar.querySelector('.toggle-omemo');
  1192. expect(view.model.get('omemo_active')).toBe(undefined);
  1193. expect(_.isNull(toggle)).toBe(false);
  1194. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1195. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1196. expect(u.hasClass('disabled', toggle)).toBe(false);
  1197. expect(view.model.get('omemo_supported')).toBe(true);
  1198. toggle.click();
  1199. toggle = toolbar.querySelector('.toggle-omemo');
  1200. expect(view.model.get('omemo_active')).toBe(true);
  1201. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1202. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1203. expect(u.hasClass('disabled', toggle)).toBe(false);
  1204. expect(view.model.get('omemo_supported')).toBe(true);
  1205. let contact_jid = 'newguy@montague.lit';
  1206. let stanza = $pres({
  1207. to: 'romeo@montague.lit/orchard',
  1208. from: 'lounge@montague.lit/newguy'
  1209. })
  1210. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1211. .c('item', {
  1212. 'affiliation': 'none',
  1213. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  1214. 'role': 'participant'
  1215. }).tree();
  1216. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1217. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1218. expect(Strophe.serialize(iq_stanza)).toBe(
  1219. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1220. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1221. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1222. `</pubsub>`+
  1223. `</iq>`);
  1224. stanza = $iq({
  1225. 'from': contact_jid,
  1226. 'id': iq_stanza.getAttribute('id'),
  1227. 'to': _converse.bare_jid,
  1228. 'type': 'result',
  1229. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1230. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1231. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1232. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1233. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  1234. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  1235. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1236. await test_utils.waitUntil(() => _converse.omemo_store);
  1237. expect(_converse.devicelists.length).toBe(2);
  1238. await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1239. const devicelist = _converse.devicelists.get(contact_jid);
  1240. expect(devicelist.devices.length).toBe(2);
  1241. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  1242. expect(devicelist.devices.at(1).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  1243. expect(view.model.get('omemo_active')).toBe(true);
  1244. toggle = toolbar.querySelector('.toggle-omemo');
  1245. expect(_.isNull(toggle)).toBe(false);
  1246. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1247. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1248. expect(u.hasClass('disabled', toggle)).toBe(false);
  1249. expect(view.model.get('omemo_supported')).toBe(true);
  1250. // Test that the button gets disabled when the room becomes
  1251. // anonymous or semi-anonymous
  1252. view.model.features.save({'nonanonymous': false, 'semianonymous': true});
  1253. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1254. toggle = toolbar.querySelector('.toggle-omemo');
  1255. expect(_.isNull(toggle)).toBe(true);
  1256. expect(view.model.get('omemo_supported')).toBe(false);
  1257. view.model.features.save({'nonanonymous': true, 'semianonymous': false});
  1258. await test_utils.waitUntil(() => view.model.get('omemo_supported'));
  1259. toggle = toolbar.querySelector('.toggle-omemo');
  1260. expect(_.isNull(toggle)).toBe(false);
  1261. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1262. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1263. expect(u.hasClass('disabled', toggle)).toBe(false);
  1264. // Test that the button gets disabled when the room becomes open
  1265. view.model.features.save({'membersonly': false, 'open': true});
  1266. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1267. toggle = toolbar.querySelector('.toggle-omemo');
  1268. expect(_.isNull(toggle)).toBe(true);
  1269. view.model.features.save({'membersonly': true, 'open': false});
  1270. await test_utils.waitUntil(() => view.model.get('omemo_supported'));
  1271. toggle = toolbar.querySelector('.toggle-omemo');
  1272. expect(_.isNull(toggle)).toBe(false);
  1273. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1274. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1275. expect(u.hasClass('disabled', toggle)).toBe(false);
  1276. expect(view.model.get('omemo_supported')).toBe(true);
  1277. expect(view.model.get('omemo_active')).toBe(false);
  1278. toggle.click();
  1279. expect(view.model.get('omemo_active')).toBe(true);
  1280. // Someone enters the room who doesn't have OMEMO support, while we
  1281. // have OMEMO activated...
  1282. contact_jid = 'oldguy@montague.lit';
  1283. stanza = $pres({
  1284. to: 'romeo@montague.lit/orchard',
  1285. from: 'lounge@montague.lit/oldguy'
  1286. })
  1287. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1288. .c('item', {
  1289. 'affiliation': 'none',
  1290. 'jid': `${contact_jid}/_converse.js-290929788`,
  1291. 'role': 'participant'
  1292. }).tree();
  1293. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1294. iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1295. expect(Strophe.serialize(iq_stanza)).toBe(
  1296. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1297. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1298. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1299. `</pubsub>`+
  1300. `</iq>`);
  1301. stanza = $iq({
  1302. 'from': contact_jid,
  1303. 'id': iq_stanza.getAttribute('id'),
  1304. 'to': _converse.bare_jid,
  1305. 'type': 'error'
  1306. }).c('error', {'type': 'cancel'})
  1307. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1308. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1309. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1310. expect(view.el.querySelector('.chat-error').textContent).toBe(
  1311. "oldguy doesn't appear to have a client that supports OMEMO. "+
  1312. "Encrypted chat will no longer be possible in this grouchat."
  1313. );
  1314. toggle = toolbar.querySelector('.toggle-omemo');
  1315. expect(_.isNull(toggle)).toBe(false);
  1316. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1317. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1318. expect(u.hasClass('disabled', toggle)).toBe(true);
  1319. expect( _converse.chatboxviews.el.querySelector('.modal-body p')).toBe(null);
  1320. toggle.click();
  1321. const msg = _converse.chatboxviews.el.querySelector('.modal-body p');
  1322. expect(msg.textContent).toBe(
  1323. 'Cannot use end-to-end encryption in this groupchat, '+
  1324. 'either the groupchat has some anonymity or not all participants support OMEMO.');
  1325. done();
  1326. }));
  1327. it("shows OMEMO device fingerprints in the user details modal",
  1328. mock.initConverse(
  1329. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1330. async function (done, _converse) {
  1331. await test_utils.waitUntilDiscoConfirmed(
  1332. _converse, _converse.bare_jid,
  1333. [{'category': 'pubsub', 'type': 'pep'}],
  1334. ['http://jabber.org/protocol/pubsub#publish-options']
  1335. );
  1336. test_utils.createContacts(_converse, 'current', 1);
  1337. _converse.api.trigger('rosterContactsFetched');
  1338. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1339. await test_utils.openChatBoxFor(_converse, contact_jid)
  1340. // We simply emit, to avoid doing all the setup work
  1341. _converse.api.trigger('OMEMOInitialized');
  1342. const view = _converse.chatboxviews.get(contact_jid);
  1343. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  1344. show_modal_button.click();
  1345. const modal = view.user_details_modal;
  1346. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  1347. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1348. expect(Strophe.serialize(iq_stanza)).toBe(
  1349. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="mercutio@montague.lit" type="get" xmlns="jabber:client">`+
  1350. `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub>`+
  1351. `</iq>`);
  1352. let stanza = $iq({
  1353. 'from': contact_jid,
  1354. 'id': iq_stanza.getAttribute('id'),
  1355. 'to': _converse.bare_jid,
  1356. 'type': 'result',
  1357. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1358. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1359. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1360. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1361. .c('device', {'id': '555'});
  1362. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1363. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  1364. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
  1365. expect(Strophe.serialize(iq_stanza)).toBe(
  1366. `<iq from="romeo@montague.lit" id="${iq_stanza.getAttribute("id")}" to="mercutio@montague.lit" type="get" xmlns="jabber:client">`+
  1367. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1368. `<items node="eu.siacs.conversations.axolotl.bundles:555"/>`+
  1369. `</pubsub>`+
  1370. `</iq>`);
  1371. stanza = $iq({
  1372. 'from': contact_jid,
  1373. 'id': iq_stanza.getAttribute('id'),
  1374. 'to': _converse.bare_jid,
  1375. 'type': 'result',
  1376. }).c('pubsub', {
  1377. 'xmlns': 'http://jabber.org/protocol/pubsub'
  1378. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  1379. .c('item')
  1380. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  1381. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  1382. .c('signedPreKeySignature').t(btoa('2222')).up()
  1383. .c('identityKey').t('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI').up()
  1384. .c('prekeys')
  1385. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  1386. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  1387. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  1388. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1389. await test_utils.waitUntil(() => modal.el.querySelectorAll('.fingerprints .fingerprint').length);
  1390. expect(modal.el.querySelectorAll('.fingerprints .fingerprint').length).toBe(1);
  1391. const el = modal.el.querySelector('.fingerprints .fingerprint');
  1392. expect(el.textContent.trim()).toBe(
  1393. u.formatFingerprint(u.arrayBufferToHex(u.base64ToArrayBuffer('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI')))
  1394. );
  1395. expect(modal.el.querySelectorAll('input[type="radio"]').length).toBe(2);
  1396. const devicelist = _converse.devicelists.get(contact_jid);
  1397. expect(devicelist.devices.get('555').get('trusted')).toBe(0);
  1398. let trusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="1"]');
  1399. expect(trusted_radio.checked).toBe(true);
  1400. let untrusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="-1"]');
  1401. expect(untrusted_radio.checked).toBe(false);
  1402. // Test that the device can be set to untrusted
  1403. untrusted_radio.click();
  1404. trusted_radio = document.querySelector('input[type="radio"][name="555"][value="1"]');
  1405. expect(trusted_radio.hasAttribute('checked')).toBe(false);
  1406. expect(devicelist.devices.get('555').get('trusted')).toBe(-1);
  1407. untrusted_radio = document.querySelector('input[type="radio"][name="555"][value="-1"]');
  1408. expect(untrusted_radio.hasAttribute('checked')).toBe(true);
  1409. trusted_radio.click();
  1410. expect(devicelist.devices.get('555').get('trusted')).toBe(1);
  1411. done();
  1412. }));
  1413. });
  1414. describe("A chatbox with an active OMEMO session", function() {
  1415. it("will not show the spoiler toolbar button",
  1416. mock.initConverse(
  1417. null, ['rosterGroupsFetched'], {},
  1418. function (done, _converse) {
  1419. // TODO
  1420. done()
  1421. }));
  1422. });
  1423. }));