omemo.js 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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.nodeTree.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.nodeTree.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.nodeTree.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.nodeTree.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.nodeTree.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.nodeTree.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.nodeTree.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() + '@localhost';
  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() + '@localhost';
  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.nodeTree.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.keyPressed({
  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.nodeTree.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.nodeTree.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="dummy@localhost/resource" id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  157. `to="max.frankfurter@localhost" `+
  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', 'localhost', 'dummy', features);
  232. const view = _converse.chatboxviews.get('lounge@localhost');
  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@localhost';
  240. let stanza = $pres({
  241. 'to': 'dummy@localhost/resource',
  242. 'from': 'lounge@localhost/newguy'
  243. })
  244. .c('x', {xmlns: Strophe.NS.MUC_USER})
  245. .c('item', {
  246. 'affiliation': 'none',
  247. 'jid': 'newguy@localhost/_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(iq_stanza.toLocaleString()).toBe(
  254. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.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.nodeTree.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.keyPressed({
  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.nodeTree.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.nodeTree.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="dummy@localhost/resource" `+
  333. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  334. `to="lounge@localhost" `+
  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("gracefully handles auth errors when trying to send encrypted groupchat messages",
  352. mock.initConverse(
  353. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  354. async function (done, _converse) {
  355. // MEMO encryption works only in members only conferences
  356. // that are non-anonymous.
  357. const features = [
  358. 'http://jabber.org/protocol/muc',
  359. 'jabber:iq:register',
  360. 'muc_passwordprotected',
  361. 'muc_hidden',
  362. 'muc_temporary',
  363. 'muc_membersonly',
  364. 'muc_unmoderated',
  365. 'muc_nonanonymous'
  366. ];
  367. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy', features);
  368. const view = _converse.chatboxviews.get('lounge@localhost');
  369. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  370. const contact_jid = 'newguy@localhost';
  371. let stanza = $pres({
  372. 'to': 'dummy@localhost/resource',
  373. 'from': 'lounge@localhost/newguy'
  374. })
  375. .c('x', {xmlns: Strophe.NS.MUC_USER})
  376. .c('item', {
  377. 'affiliation': 'none',
  378. 'jid': 'newguy@localhost/_converse.js-290929789',
  379. 'role': 'participant'
  380. }).tree();
  381. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  382. const toolbar = view.el.querySelector('.chat-toolbar');
  383. const toggle = toolbar.querySelector('.toggle-omemo');
  384. toggle.click();
  385. expect(view.model.get('omemo_active')).toBe(true);
  386. expect(view.model.get('omemo_supported')).toBe(true);
  387. const textarea = view.el.querySelector('.chat-textarea');
  388. textarea.value = 'This message will be encrypted';
  389. view.keyPressed({
  390. target: textarea,
  391. preventDefault: _.noop,
  392. keyCode: 13 // Enter
  393. });
  394. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  395. expect(iq_stanza.toLocaleString()).toBe(
  396. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  397. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  398. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  399. `</pubsub>`+
  400. `</iq>`);
  401. stanza = $iq({
  402. 'from': contact_jid,
  403. 'id': iq_stanza.nodeTree.getAttribute('id'),
  404. 'to': _converse.bare_jid,
  405. 'type': 'result',
  406. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  407. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  408. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  409. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  410. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  411. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  412. await test_utils.waitUntil(() => _converse.omemo_store);
  413. expect(_converse.devicelists.length).toBe(2);
  414. const devicelist = _converse.devicelists.get(contact_jid);
  415. await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  416. expect(devicelist.devices.length).toBe(1);
  417. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  418. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  419. stanza = $iq({
  420. 'from': _converse.bare_jid,
  421. 'id': iq_stanza.nodeTree.getAttribute('id'),
  422. 'to': _converse.bare_jid,
  423. 'type': 'result',
  424. }).c('pubsub', {
  425. 'xmlns': 'http://jabber.org/protocol/pubsub'
  426. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  427. .c('item')
  428. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  429. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  430. .c('signedPreKeySignature').t(btoa('200000')).up()
  431. .c('identityKey').t(btoa('300000')).up()
  432. .c('prekeys')
  433. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  434. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  435. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  436. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '4e30f35051b7b8b42abe083742187228'));
  437. /* <iq xmlns="jabber:client" to="jc@opkode.com/converse.js-34183907" type="error" id="945c8ab3-b561-4d8a-92da-77c226bb1689:sendIQ" from="joris@konuro.net">
  438. * <pubsub xmlns="http://jabber.org/protocol/pubsub">
  439. * <items node="eu.siacs.conversations.axolotl.bundles:7580"/>
  440. * </pubsub>
  441. * <error code="401" type="auth">
  442. * <presence-subscription-required xmlns="http://jabber.org/protocol/pubsub#errors"/>
  443. * <not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  444. * </error>
  445. * </iq>
  446. */
  447. stanza = $iq({
  448. 'from': contact_jid,
  449. 'id': iq_stanza.nodeTree.getAttribute('id'),
  450. 'to': _converse.bare_jid,
  451. 'type': 'result',
  452. }).c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub'})
  453. .c('items', {'node': "eu.siacs.conversations.axolotl.bundles:4e30f35051b7b8b42abe083742187228"}).up().up()
  454. .c('error', {'code': '401', 'type': 'auth'})
  455. .c('presence-subscription-required', {'xmlns':"http://jabber.org/protocol/pubsub#errors" }).up()
  456. .c('not-authorized', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  457. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  458. await test_utils.waitUntil(() => document.querySelectorAll('.alert-danger').length, 2000);
  459. const header = document.querySelector('.alert-danger .modal-title');
  460. expect(header.textContent).toBe("Error");
  461. expect(u.ancestor(header, '.modal-content').querySelector('.modal-body p').textContent.trim())
  462. .toBe("Sorry, we're unable to send an encrypted message because newguy@localhost requires you "+
  463. "to be subscribed to their presence in order to see their OMEMO information");
  464. expect(view.model.get('omemo_supported')).toBe(false);
  465. expect(view.el.querySelector('.chat-textarea').value).toBe('This message will be encrypted');
  466. done();
  467. }));
  468. it("can receive a PreKeySignalMessage",
  469. mock.initConverse(
  470. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  471. async function (done, _converse) {
  472. _converse.NUM_PREKEYS = 5; // Restrict to 5, otherwise the resulting stanza is too large to easily test
  473. let view, sent_stanza;
  474. test_utils.createContacts(_converse, 'current', 1);
  475. _converse.api.trigger('rosterContactsFetched');
  476. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  477. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  478. const obj = await _converse.ChatBox.prototype.encryptMessage('This is an encrypted message from the contact');
  479. // XXX: Normally the key will be encrypted via libsignal.
  480. // However, we're mocking libsignal in the tests, so we include
  481. // it as plaintext in the message.
  482. let stanza = $msg({
  483. 'from': contact_jid,
  484. 'to': _converse.connection.jid,
  485. 'type': 'chat',
  486. 'id': 'qwerty'
  487. }).c('body').t('This is a fallback message').up()
  488. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  489. .c('header', {'sid': '555'})
  490. .c('key', {
  491. 'prekey': 'true',
  492. 'rid': _converse.omemo_store.get('device_id')
  493. }).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  494. .c('iv').t(obj.iv)
  495. .up().up()
  496. .c('payload').t(obj.payload);
  497. const generateMissingPreKeys = _converse.omemo_store.generateMissingPreKeys;
  498. spyOn(_converse.omemo_store, 'generateMissingPreKeys').and.callFake(() => {
  499. // Since it's difficult to override
  500. // decryptPreKeyWhisperMessage, where a prekey will be
  501. // removed from the store, we do it here, before the
  502. // missing prekeys are generated.
  503. _converse.omemo_store.removePreKey(1);
  504. return generateMissingPreKeys.apply(_converse.omemo_store, arguments);
  505. });
  506. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  507. let iq_stanza = await test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid));
  508. iq_stanza = await deviceListFetched(_converse, contact_jid);
  509. stanza = $iq({
  510. 'from': contact_jid,
  511. 'id': iq_stanza.nodeTree.getAttribute('id'),
  512. 'to': _converse.connection.jid,
  513. 'type': 'result',
  514. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  515. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  516. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  517. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  518. .c('device', {'id': '555'});
  519. // XXX: the bundle gets published twice, we want to make sure
  520. // that we wait for the 2nd, so we clear all the already sent
  521. // stanzas.
  522. _converse.connection.IQ_stanzas = [];
  523. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  524. await test_utils.waitUntil(() => _converse.omemo_store);
  525. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  526. expect(iq_stanza.toLocaleString()).toBe(
  527. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" type="set" xmlns="jabber:client">`+
  528. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  529. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  530. `<item>`+
  531. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  532. `<signedPreKeyPublic signedPreKeyId="0">${btoa("1234")}</signedPreKeyPublic>`+
  533. `<signedPreKeySignature>${btoa("11112222333344445555")}</signedPreKeySignature>`+
  534. `<identityKey>${btoa("1234")}</identityKey>`+
  535. `<prekeys>`+
  536. `<preKeyPublic preKeyId="0">${btoa("1234")}</preKeyPublic>`+
  537. `<preKeyPublic preKeyId="1">${btoa("1234")}</preKeyPublic>`+
  538. `<preKeyPublic preKeyId="2">${btoa("1234")}</preKeyPublic>`+
  539. `<preKeyPublic preKeyId="3">${btoa("1234")}</preKeyPublic>`+
  540. `<preKeyPublic preKeyId="4">${btoa("1234")}</preKeyPublic>`+
  541. `</prekeys>`+
  542. `</bundle>`+
  543. `</item>`+
  544. `</publish>`+
  545. `<publish-options>`+
  546. `<x type="submit" xmlns="jabber:x:data">`+
  547. `<field type="hidden" var="FORM_TYPE">`+
  548. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  549. `</field>`+
  550. `<field var="pubsub#access_model">`+
  551. `<value>open</value>`+
  552. `</field>`+
  553. `</x>`+
  554. `</publish-options>`+
  555. `</pubsub>`+
  556. `</iq>`)
  557. const own_device = _converse.devicelists.get(_converse.bare_jid).devices.get(_converse.omemo_store.get('device_id'));
  558. expect(own_device.get('bundle').prekeys.length).toBe(5);
  559. expect(_converse.omemo_store.generateMissingPreKeys).toHaveBeenCalled();
  560. done();
  561. }));
  562. it("updates device lists based on PEP messages",
  563. mock.initConverse(
  564. null, ['rosterGroupsFetched'], {'allow_non_roster_messaging': true},
  565. async function (done, _converse) {
  566. await test_utils.waitUntilDiscoConfirmed(
  567. _converse, _converse.bare_jid,
  568. [{'category': 'pubsub', 'type': 'pep'}],
  569. ['http://jabber.org/protocol/pubsub#publish-options']
  570. );
  571. test_utils.createContacts(_converse, 'current', 1);
  572. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  573. // Wait until own devices are fetched
  574. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  575. expect(iq_stanza.toLocaleString()).toBe(
  576. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="dummy@localhost" type="get" xmlns="jabber:client">`+
  577. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  578. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  579. `</pubsub>`+
  580. `</iq>`);
  581. let stanza = $iq({
  582. 'from': _converse.bare_jid,
  583. 'id': iq_stanza.nodeTree.getAttribute('id'),
  584. 'to': _converse.bare_jid,
  585. 'type': 'result',
  586. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  587. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  588. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  589. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  590. .c('device', {'id': '555'});
  591. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  592. await test_utils.waitUntil(() => _converse.omemo_store);
  593. expect(_converse.chatboxes.length).toBe(1);
  594. expect(_converse.devicelists.length).toBe(1);
  595. const devicelist = _converse.devicelists.get(_converse.bare_jid);
  596. expect(devicelist.devices.length).toBe(2);
  597. expect(devicelist.devices.at(0).get('id')).toBe('555');
  598. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  599. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  600. stanza = $iq({
  601. 'from': _converse.bare_jid,
  602. 'id': iq_stanza.nodeTree.getAttribute('id'),
  603. 'to': _converse.bare_jid,
  604. 'type': 'result'});
  605. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  606. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  607. stanza = $iq({
  608. 'from': _converse.bare_jid,
  609. 'id': iq_stanza.nodeTree.getAttribute('id'),
  610. 'to': _converse.bare_jid,
  611. 'type': 'result'});
  612. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  613. await _converse.api.waitUntil('OMEMOInitialized');
  614. stanza = $msg({
  615. 'from': contact_jid,
  616. 'to': _converse.bare_jid,
  617. 'type': 'headline',
  618. 'id': 'update_01',
  619. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  620. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  621. .c('item')
  622. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  623. .c('device', {'id': '1234'})
  624. .c('device', {'id': '4223'})
  625. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  626. expect(_converse.devicelists.length).toBe(2);
  627. let devices = _converse.devicelists.get(contact_jid).devices;
  628. expect(devices.length).toBe(2);
  629. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('1234,4223');
  630. stanza = $msg({
  631. 'from': contact_jid,
  632. 'to': _converse.bare_jid,
  633. 'type': 'headline',
  634. 'id': 'update_02',
  635. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  636. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  637. .c('item')
  638. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  639. .c('device', {'id': '4223'})
  640. .c('device', {'id': '4224'})
  641. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  642. expect(_converse.devicelists.length).toBe(2);
  643. expect(devices.length).toBe(2);
  644. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('4223,4224');
  645. // Check that own devicelist gets updated
  646. stanza = $msg({
  647. 'from': _converse.bare_jid,
  648. 'to': _converse.bare_jid,
  649. 'type': 'headline',
  650. 'id': 'update_03',
  651. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  652. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  653. .c('item')
  654. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  655. .c('device', {'id': '123456789'})
  656. .c('device', {'id': '555'})
  657. .c('device', {'id': '777'})
  658. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  659. expect(_converse.devicelists.length).toBe(2);
  660. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  661. expect(devices.length).toBe(3);
  662. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,555,777');
  663. _converse.connection.IQ_stanzas = [];
  664. // Check that own device gets re-added
  665. stanza = $msg({
  666. 'from': _converse.bare_jid,
  667. 'to': _converse.bare_jid,
  668. 'type': 'headline',
  669. 'id': 'update_04',
  670. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  671. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  672. .c('item')
  673. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  674. .c('device', {'id': '444'})
  675. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  676. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  677. // Check that our own device is added again, but that removed
  678. // devices are not added.
  679. expect(iq_stanza.toLocaleString()).toBe(
  680. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute(`id`)}" type="set" xmlns="jabber:client">`+
  681. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  682. `<publish node="eu.siacs.conversations.axolotl.devicelist">`+
  683. `<item>`+
  684. `<list xmlns="eu.siacs.conversations.axolotl">`+
  685. `<device id="123456789"/>`+
  686. `<device id="444"/>`+
  687. `</list>`+
  688. `</item>`+
  689. `</publish>`+
  690. `<publish-options>`+
  691. `<x type="submit" xmlns="jabber:x:data">`+
  692. `<field type="hidden" var="FORM_TYPE">`+
  693. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  694. `</field>`+
  695. `<field var="pubsub#access_model">`+
  696. `<value>open</value>`+
  697. `</field>`+
  698. `</x>`+
  699. `</publish-options>`+
  700. `</pubsub>`+
  701. `</iq>`);
  702. expect(_converse.devicelists.length).toBe(2);
  703. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  704. // The device id for this device (123456789) was also generated and added to the list,
  705. // which is why we have 2 devices now.
  706. expect(devices.length).toBe(2);
  707. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,444');
  708. done();
  709. }));
  710. it("updates device bundles based on PEP messages",
  711. mock.initConverse(
  712. null, ['rosterGroupsFetched'], {},
  713. async function (done, _converse) {
  714. await test_utils.waitUntilDiscoConfirmed(
  715. _converse, _converse.bare_jid,
  716. [{'category': 'pubsub', 'type': 'pep'}],
  717. ['http://jabber.org/protocol/pubsub#publish-options']
  718. );
  719. test_utils.createContacts(_converse, 'current');
  720. const contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
  721. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  722. expect(iq_stanza.toLocaleString()).toBe(
  723. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="dummy@localhost" type="get" xmlns="jabber:client">`+
  724. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  725. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  726. `</pubsub>`+
  727. `</iq>`);
  728. let stanza = $iq({
  729. 'from': contact_jid,
  730. 'id': iq_stanza.nodeTree.getAttribute('id'),
  731. 'to': _converse.bare_jid,
  732. 'type': 'result',
  733. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  734. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  735. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  736. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  737. .c('device', {'id': '555'});
  738. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  739. await await test_utils.waitUntil(() => _converse.omemo_store);
  740. expect(_converse.devicelists.length).toBe(1);
  741. let devicelist = _converse.devicelists.get(_converse.bare_jid);
  742. expect(devicelist.devices.length).toBe(2);
  743. expect(devicelist.devices.at(0).get('id')).toBe('555');
  744. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  745. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  746. stanza = $iq({
  747. 'from': _converse.bare_jid,
  748. 'id': iq_stanza.nodeTree.getAttribute('id'),
  749. 'to': _converse.bare_jid,
  750. 'type': 'result'});
  751. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  752. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  753. stanza = $iq({
  754. 'from': _converse.bare_jid,
  755. 'id': iq_stanza.nodeTree.getAttribute('id'),
  756. 'to': _converse.bare_jid,
  757. 'type': 'result'});
  758. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  759. await _converse.api.waitUntil('OMEMOInitialized');
  760. stanza = $msg({
  761. 'from': contact_jid,
  762. 'to': _converse.bare_jid,
  763. 'type': 'headline',
  764. 'id': 'update_01',
  765. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  766. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  767. .c('item')
  768. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  769. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('1111').up()
  770. .c('signedPreKeySignature').t('2222').up()
  771. .c('identityKey').t('3333').up()
  772. .c('prekeys')
  773. .c('preKeyPublic', {'preKeyId': '1001'}).up()
  774. .c('preKeyPublic', {'preKeyId': '1002'}).up()
  775. .c('preKeyPublic', {'preKeyId': '1003'});
  776. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  777. expect(_converse.devicelists.length).toBe(2);
  778. devicelist = _converse.devicelists.get(contact_jid);
  779. expect(devicelist.devices.length).toBe(1);
  780. let device = devicelist.devices.at(0);
  781. expect(device.get('bundle').identity_key).toBe('3333');
  782. expect(device.get('bundle').signed_prekey.public_key).toBe('1111');
  783. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  784. expect(device.get('bundle').signed_prekey.signature).toBe('2222');
  785. expect(device.get('bundle').prekeys.length).toBe(3);
  786. expect(device.get('bundle').prekeys[0].id).toBe(1001);
  787. expect(device.get('bundle').prekeys[1].id).toBe(1002);
  788. expect(device.get('bundle').prekeys[2].id).toBe(1003);
  789. stanza = $msg({
  790. 'from': contact_jid,
  791. 'to': _converse.bare_jid,
  792. 'type': 'headline',
  793. 'id': 'update_02',
  794. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  795. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  796. .c('item')
  797. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  798. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('5555').up()
  799. .c('signedPreKeySignature').t('6666').up()
  800. .c('identityKey').t('7777').up()
  801. .c('prekeys')
  802. .c('preKeyPublic', {'preKeyId': '2001'}).up()
  803. .c('preKeyPublic', {'preKeyId': '2002'}).up()
  804. .c('preKeyPublic', {'preKeyId': '2003'});
  805. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  806. expect(_converse.devicelists.length).toBe(2);
  807. devicelist = _converse.devicelists.get(contact_jid);
  808. expect(devicelist.devices.length).toBe(1);
  809. device = devicelist.devices.at(0);
  810. expect(device.get('bundle').identity_key).toBe('7777');
  811. expect(device.get('bundle').signed_prekey.public_key).toBe('5555');
  812. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  813. expect(device.get('bundle').signed_prekey.signature).toBe('6666');
  814. expect(device.get('bundle').prekeys.length).toBe(3);
  815. expect(device.get('bundle').prekeys[0].id).toBe(2001);
  816. expect(device.get('bundle').prekeys[1].id).toBe(2002);
  817. expect(device.get('bundle').prekeys[2].id).toBe(2003);
  818. stanza = $msg({
  819. 'from': _converse.bare_jid,
  820. 'to': _converse.bare_jid,
  821. 'type': 'headline',
  822. 'id': 'update_03',
  823. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  824. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:123456789'})
  825. .c('item')
  826. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  827. .c('signedPreKeyPublic', {'signedPreKeyId': '9999'}).t('8888').up()
  828. .c('signedPreKeySignature').t('3333').up()
  829. .c('identityKey').t('1111').up()
  830. .c('prekeys')
  831. .c('preKeyPublic', {'preKeyId': '3001'}).up()
  832. .c('preKeyPublic', {'preKeyId': '3002'}).up()
  833. .c('preKeyPublic', {'preKeyId': '3003'});
  834. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  835. expect(_converse.devicelists.length).toBe(2);
  836. devicelist = _converse.devicelists.get(_converse.bare_jid);
  837. expect(devicelist.devices.length).toBe(2);
  838. expect(devicelist.devices.at(0).get('id')).toBe('555');
  839. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  840. device = devicelist.devices.at(1);
  841. expect(device.get('bundle').identity_key).toBe('1111');
  842. expect(device.get('bundle').signed_prekey.public_key).toBe('8888');
  843. expect(device.get('bundle').signed_prekey.id).toBe(9999);
  844. expect(device.get('bundle').signed_prekey.signature).toBe('3333');
  845. expect(device.get('bundle').prekeys.length).toBe(3);
  846. expect(device.get('bundle').prekeys[0].id).toBe(3001);
  847. expect(device.get('bundle').prekeys[1].id).toBe(3002);
  848. expect(device.get('bundle').prekeys[2].id).toBe(3003);
  849. done();
  850. }));
  851. it("publishes a bundle with which an encrypted session can be created",
  852. mock.initConverse(
  853. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  854. async function (done, _converse) {
  855. await test_utils.waitUntilDiscoConfirmed(
  856. _converse, _converse.bare_jid,
  857. [{'category': 'pubsub', 'type': 'pep'}],
  858. ['http://jabber.org/protocol/pubsub#publish-options']
  859. );
  860. _converse.NUM_PREKEYS = 2; // Restrict to 2, otherwise the resulting stanza is too large to easily test
  861. test_utils.createContacts(_converse, 'current', 1);
  862. _converse.api.trigger('rosterContactsFetched');
  863. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  864. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  865. let stanza = $iq({
  866. 'from': contact_jid,
  867. 'id': iq_stanza.nodeTree.getAttribute('id'),
  868. 'to': _converse.bare_jid,
  869. 'type': 'result',
  870. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  871. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  872. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  873. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  874. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  875. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  876. expect(_converse.devicelists.length).toBe(1);
  877. await test_utils.openChatBoxFor(_converse, contact_jid);
  878. iq_stanza = await ownDeviceHasBeenPublished(_converse);
  879. stanza = $iq({
  880. 'from': _converse.bare_jid,
  881. 'id': iq_stanza.nodeTree.getAttribute('id'),
  882. 'to': _converse.bare_jid,
  883. 'type': 'result'});
  884. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  885. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  886. expect(iq_stanza.toLocaleString()).toBe(
  887. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" type="set" xmlns="jabber:client">`+
  888. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  889. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  890. `<item>`+
  891. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  892. `<signedPreKeyPublic signedPreKeyId="0">${btoa("1234")}</signedPreKeyPublic>`+
  893. `<signedPreKeySignature>${btoa("11112222333344445555")}</signedPreKeySignature>`+
  894. `<identityKey>${btoa("1234")}</identityKey>`+
  895. `<prekeys>`+
  896. `<preKeyPublic preKeyId="0">${btoa("1234")}</preKeyPublic>`+
  897. `<preKeyPublic preKeyId="1">${btoa("1234")}</preKeyPublic>`+
  898. `</prekeys>`+
  899. `</bundle>`+
  900. `</item>`+
  901. `</publish>`+
  902. `<publish-options>`+
  903. `<x type="submit" xmlns="jabber:x:data">`+
  904. `<field type="hidden" var="FORM_TYPE">`+
  905. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  906. `</field>`+
  907. `<field var="pubsub#access_model">`+
  908. `<value>open</value>`+
  909. `</field>`+
  910. `</x>`+
  911. `</publish-options>`+
  912. `</pubsub>`+
  913. `</iq>`)
  914. stanza = $iq({
  915. 'from': _converse.bare_jid,
  916. 'id': iq_stanza.nodeTree.getAttribute('id'),
  917. 'to': _converse.bare_jid,
  918. 'type': 'result'});
  919. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  920. await _converse.api.waitUntil('OMEMOInitialized');
  921. done();
  922. }));
  923. it("adds a toolbar button for starting an encrypted chat session",
  924. mock.initConverse(
  925. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  926. async function (done, _converse) {
  927. await test_utils.waitUntilDiscoConfirmed(
  928. _converse, _converse.bare_jid,
  929. [{'category': 'pubsub', 'type': 'pep'}],
  930. ['http://jabber.org/protocol/pubsub#publish-options']
  931. );
  932. test_utils.createContacts(_converse, 'current', 1);
  933. _converse.api.trigger('rosterContactsFetched');
  934. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  935. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  936. expect(iq_stanza.toLocaleString()).toBe(
  937. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="dummy@localhost" type="get" xmlns="jabber:client">`+
  938. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  939. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  940. `</pubsub>`+
  941. `</iq>`);
  942. let stanza = $iq({
  943. 'from': _converse.bare_jid,
  944. 'id': iq_stanza.nodeTree.getAttribute('id'),
  945. 'to': _converse.bare_jid,
  946. 'type': 'result',
  947. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  948. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  949. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  950. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  951. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  952. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  953. await test_utils.waitUntil(() => _converse.omemo_store);
  954. expect(_converse.devicelists.length).toBe(1);
  955. let devicelist = _converse.devicelists.get(_converse.bare_jid);
  956. expect(devicelist.devices.length).toBe(2);
  957. expect(devicelist.devices.at(0).get('id')).toBe('482886413b977930064a5888b92134fe');
  958. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  959. // Check that own device was published
  960. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  961. expect(iq_stanza.toLocaleString()).toBe(
  962. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute(`id`)}" type="set" xmlns="jabber:client">`+
  963. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  964. `<publish node="eu.siacs.conversations.axolotl.devicelist">`+
  965. `<item>`+
  966. `<list xmlns="eu.siacs.conversations.axolotl">`+
  967. `<device id="482886413b977930064a5888b92134fe"/>`+
  968. `<device id="123456789"/>`+
  969. `</list>`+
  970. `</item>`+
  971. `</publish>`+
  972. `<publish-options>`+
  973. `<x type="submit" xmlns="jabber:x:data">`+
  974. `<field type="hidden" var="FORM_TYPE">`+
  975. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  976. `</field>`+
  977. `<field var="pubsub#access_model">`+
  978. `<value>open</value>`+
  979. `</field>`+
  980. `</x>`+
  981. `</publish-options>`+
  982. `</pubsub>`+
  983. `</iq>`);
  984. stanza = $iq({
  985. 'from': _converse.bare_jid,
  986. 'id': iq_stanza.nodeTree.getAttribute('id'),
  987. 'to': _converse.bare_jid,
  988. 'type': 'result'});
  989. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  990. const iq_el = await test_utils.waitUntil(() => _.get(bundleHasBeenPublished(_converse), 'nodeTree'));
  991. expect(iq_el.getAttributeNames().sort().join()).toBe(["from", "type", "xmlns", "id"].sort().join());
  992. expect(iq_el.querySelector('prekeys').childNodes.length).toBe(100);
  993. const signed_prekeys = iq_el.querySelectorAll('signedPreKeyPublic');
  994. expect(signed_prekeys.length).toBe(1);
  995. const signed_prekey = signed_prekeys[0];
  996. expect(signed_prekey.getAttribute('signedPreKeyId')).toBe('0')
  997. expect(iq_el.querySelectorAll('signedPreKeySignature').length).toBe(1);
  998. expect(iq_el.querySelectorAll('identityKey').length).toBe(1);
  999. stanza = $iq({
  1000. 'from': _converse.bare_jid,
  1001. 'id': iq_el.getAttribute('id'),
  1002. 'to': _converse.bare_jid,
  1003. 'type': 'result'});
  1004. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1005. await _converse.api.waitUntil('OMEMOInitialized', 1000);
  1006. await test_utils.openChatBoxFor(_converse, contact_jid);
  1007. iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1008. expect(iq_stanza.toLocaleString()).toBe(
  1009. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1010. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1011. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1012. `</pubsub>`+
  1013. `</iq>`);
  1014. stanza = $iq({
  1015. 'from': contact_jid,
  1016. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1017. 'to': _converse.bare_jid,
  1018. 'type': 'result',
  1019. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1020. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1021. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1022. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1023. .c('device', {'id': '368866411b877c30064a5f62b917cffe'}).up()
  1024. .c('device', {'id': '3300659945416e274474e469a1f0154c'}).up()
  1025. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  1026. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  1027. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1028. devicelist = _converse.devicelists.get(contact_jid);
  1029. await test_utils.waitUntil(() => devicelist.devices.length);
  1030. expect(_converse.devicelists.length).toBe(2);
  1031. devicelist = _converse.devicelists.get(contact_jid);
  1032. expect(devicelist.devices.length).toBe(4);
  1033. expect(devicelist.devices.at(0).get('id')).toBe('368866411b877c30064a5f62b917cffe');
  1034. expect(devicelist.devices.at(1).get('id')).toBe('3300659945416e274474e469a1f0154c');
  1035. expect(devicelist.devices.at(2).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  1036. expect(devicelist.devices.at(3).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  1037. await test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid).el.querySelector('.chat-toolbar'));
  1038. const view = _converse.chatboxviews.get(contact_jid);
  1039. const toolbar = view.el.querySelector('.chat-toolbar');
  1040. expect(view.model.get('omemo_active')).toBe(undefined);
  1041. let toggle = toolbar.querySelector('.toggle-omemo');
  1042. expect(_.isNull(toggle)).toBe(false);
  1043. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1044. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1045. spyOn(view, 'toggleOMEMO').and.callThrough();
  1046. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1047. toolbar.querySelector('.toggle-omemo').click();
  1048. expect(view.toggleOMEMO).toHaveBeenCalled();
  1049. expect(view.model.get('omemo_active')).toBe(true);
  1050. await test_utils.waitUntil(() => u.hasClass('fa-lock', toolbar.querySelector('.toggle-omemo')));
  1051. toggle = toolbar.querySelector('.toggle-omemo');
  1052. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1053. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1054. const textarea = view.el.querySelector('.chat-textarea');
  1055. textarea.value = 'This message will be sent encrypted';
  1056. view.keyPressed({
  1057. target: textarea,
  1058. preventDefault: _.noop,
  1059. keyCode: 13
  1060. });
  1061. view.model.save({'omemo_supported': false});
  1062. toggle = toolbar.querySelector('.toggle-omemo');
  1063. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1064. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1065. expect(u.hasClass('disabled', toggle)).toBe(true);
  1066. view.model.save({'omemo_supported': true});
  1067. toggle = toolbar.querySelector('.toggle-omemo');
  1068. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1069. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1070. expect(u.hasClass('disabled', toggle)).toBe(false);
  1071. done();
  1072. }));
  1073. it("adds a toolbar button for starting an encrypted groupchat session",
  1074. mock.initConverse(
  1075. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'view_mode': 'fullscreen'},
  1076. async function (done, _converse) {
  1077. await test_utils.waitUntilDiscoConfirmed(
  1078. _converse, _converse.bare_jid,
  1079. [{'category': 'pubsub', 'type': 'pep'}],
  1080. ['http://jabber.org/protocol/pubsub#publish-options']
  1081. );
  1082. // MEMO encryption works only in members-only conferences that are non-anonymous.
  1083. const features = [
  1084. 'http://jabber.org/protocol/muc',
  1085. 'jabber:iq:register',
  1086. 'muc_passwordprotected',
  1087. 'muc_hidden',
  1088. 'muc_temporary',
  1089. 'muc_membersonly',
  1090. 'muc_unmoderated',
  1091. 'muc_nonanonymous'
  1092. ];
  1093. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy', features);
  1094. const view = _converse.chatboxviews.get('lounge@localhost');
  1095. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  1096. const toolbar = view.el.querySelector('.chat-toolbar');
  1097. let toggle = toolbar.querySelector('.toggle-omemo');
  1098. expect(view.model.get('omemo_active')).toBe(undefined);
  1099. expect(_.isNull(toggle)).toBe(false);
  1100. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1101. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1102. expect(u.hasClass('disabled', toggle)).toBe(false);
  1103. expect(view.model.get('omemo_supported')).toBe(true);
  1104. toggle.click();
  1105. toggle = toolbar.querySelector('.toggle-omemo');
  1106. expect(view.model.get('omemo_active')).toBe(true);
  1107. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1108. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1109. expect(u.hasClass('disabled', toggle)).toBe(false);
  1110. expect(view.model.get('omemo_supported')).toBe(true);
  1111. let contact_jid = 'newguy@localhost';
  1112. let stanza = $pres({
  1113. to: 'dummy@localhost/resource',
  1114. from: 'lounge@localhost/newguy'
  1115. })
  1116. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1117. .c('item', {
  1118. 'affiliation': 'none',
  1119. 'jid': 'newguy@localhost/_converse.js-290929789',
  1120. 'role': 'participant'
  1121. }).tree();
  1122. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1123. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1124. expect(iq_stanza.toLocaleString()).toBe(
  1125. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1126. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1127. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1128. `</pubsub>`+
  1129. `</iq>`);
  1130. stanza = $iq({
  1131. 'from': contact_jid,
  1132. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1133. 'to': _converse.bare_jid,
  1134. 'type': 'result',
  1135. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1136. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1137. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1138. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1139. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  1140. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  1141. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1142. await test_utils.waitUntil(() => _converse.omemo_store);
  1143. expect(_converse.devicelists.length).toBe(2);
  1144. await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1145. const devicelist = _converse.devicelists.get(contact_jid);
  1146. expect(devicelist.devices.length).toBe(2);
  1147. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  1148. expect(devicelist.devices.at(1).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  1149. expect(view.model.get('omemo_active')).toBe(true);
  1150. toggle = toolbar.querySelector('.toggle-omemo');
  1151. expect(_.isNull(toggle)).toBe(false);
  1152. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1153. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1154. expect(u.hasClass('disabled', toggle)).toBe(false);
  1155. expect(view.model.get('omemo_supported')).toBe(true);
  1156. // Test that the button gets disabled when the room becomes
  1157. // anonymous or semi-anonymous
  1158. view.model.features.save({'nonanonymous': false, 'semianonymous': true});
  1159. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1160. toggle = toolbar.querySelector('.toggle-omemo');
  1161. expect(_.isNull(toggle)).toBe(true);
  1162. expect(view.model.get('omemo_supported')).toBe(false);
  1163. view.model.features.save({'nonanonymous': true, 'semianonymous': false});
  1164. await test_utils.waitUntil(() => view.model.get('omemo_supported'));
  1165. toggle = toolbar.querySelector('.toggle-omemo');
  1166. expect(_.isNull(toggle)).toBe(false);
  1167. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1168. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1169. expect(u.hasClass('disabled', toggle)).toBe(false);
  1170. // Test that the button gets disabled when the room becomes open
  1171. view.model.features.save({'membersonly': false, 'open': true});
  1172. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1173. toggle = toolbar.querySelector('.toggle-omemo');
  1174. expect(_.isNull(toggle)).toBe(true);
  1175. view.model.features.save({'membersonly': true, 'open': false});
  1176. await test_utils.waitUntil(() => view.model.get('omemo_supported'));
  1177. toggle = toolbar.querySelector('.toggle-omemo');
  1178. expect(_.isNull(toggle)).toBe(false);
  1179. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1180. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1181. expect(u.hasClass('disabled', toggle)).toBe(false);
  1182. expect(view.model.get('omemo_supported')).toBe(true);
  1183. expect(view.model.get('omemo_active')).toBe(false);
  1184. toggle.click();
  1185. expect(view.model.get('omemo_active')).toBe(true);
  1186. // Someone enters the room who doesn't have OMEMO support, while we
  1187. // have OMEMO activated...
  1188. contact_jid = 'oldguy@localhost';
  1189. stanza = $pres({
  1190. to: 'dummy@localhost/resource',
  1191. from: 'lounge@localhost/oldguy'
  1192. })
  1193. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1194. .c('item', {
  1195. 'affiliation': 'none',
  1196. 'jid': `${contact_jid}/_converse.js-290929788`,
  1197. 'role': 'participant'
  1198. }).tree();
  1199. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1200. iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1201. expect(iq_stanza.toLocaleString()).toBe(
  1202. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1203. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1204. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1205. `</pubsub>`+
  1206. `</iq>`);
  1207. stanza = $iq({
  1208. 'from': contact_jid,
  1209. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1210. 'to': _converse.bare_jid,
  1211. 'type': 'error'
  1212. }).c('error', {'type': 'cancel'})
  1213. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1214. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1215. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1216. expect(view.el.querySelector('.chat-error').textContent).toBe(
  1217. "oldguy doesn't appear to have a client that supports OMEMO. "+
  1218. "Encrypted chat will no longer be possible in this grouchat."
  1219. );
  1220. toggle = toolbar.querySelector('.toggle-omemo');
  1221. expect(_.isNull(toggle)).toBe(false);
  1222. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1223. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1224. expect(u.hasClass('disabled', toggle)).toBe(true);
  1225. expect( _converse.chatboxviews.el.querySelector('.modal-body p')).toBe(null);
  1226. toggle.click();
  1227. const msg = _converse.chatboxviews.el.querySelector('.modal-body p');
  1228. expect(msg.textContent).toBe(
  1229. 'Cannot use end-to-end encryption in this groupchat, '+
  1230. 'either the groupchat has some anonymity or not all participants support OMEMO.');
  1231. done();
  1232. }));
  1233. it("shows OMEMO device fingerprints in the user details modal",
  1234. mock.initConverse(
  1235. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1236. async function (done, _converse) {
  1237. await test_utils.waitUntilDiscoConfirmed(
  1238. _converse, _converse.bare_jid,
  1239. [{'category': 'pubsub', 'type': 'pep'}],
  1240. ['http://jabber.org/protocol/pubsub#publish-options']
  1241. );
  1242. test_utils.createContacts(_converse, 'current', 1);
  1243. _converse.api.trigger('rosterContactsFetched');
  1244. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  1245. await test_utils.openChatBoxFor(_converse, contact_jid)
  1246. // We simply emit, to avoid doing all the setup work
  1247. _converse.api.trigger('OMEMOInitialized');
  1248. const view = _converse.chatboxviews.get(contact_jid);
  1249. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  1250. show_modal_button.click();
  1251. const modal = view.user_details_modal;
  1252. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  1253. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1254. expect(iq_stanza.toLocaleString()).toBe(
  1255. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="max.frankfurter@localhost" type="get" xmlns="jabber:client">`+
  1256. `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub>`+
  1257. `</iq>`);
  1258. let stanza = $iq({
  1259. 'from': contact_jid,
  1260. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1261. 'to': _converse.bare_jid,
  1262. 'type': 'result',
  1263. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1264. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1265. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1266. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1267. .c('device', {'id': '555'});
  1268. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1269. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  1270. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
  1271. expect(iq_stanza.toLocaleString()).toBe(
  1272. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="max.frankfurter@localhost" type="get" xmlns="jabber:client">`+
  1273. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1274. `<items node="eu.siacs.conversations.axolotl.bundles:555"/>`+
  1275. `</pubsub>`+
  1276. `</iq>`);
  1277. stanza = $iq({
  1278. 'from': contact_jid,
  1279. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1280. 'to': _converse.bare_jid,
  1281. 'type': 'result',
  1282. }).c('pubsub', {
  1283. 'xmlns': 'http://jabber.org/protocol/pubsub'
  1284. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  1285. .c('item')
  1286. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  1287. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  1288. .c('signedPreKeySignature').t(btoa('2222')).up()
  1289. .c('identityKey').t('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI').up()
  1290. .c('prekeys')
  1291. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  1292. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  1293. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  1294. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1295. await test_utils.waitUntil(() => modal.el.querySelectorAll('.fingerprints .fingerprint').length);
  1296. expect(modal.el.querySelectorAll('.fingerprints .fingerprint').length).toBe(1);
  1297. const el = modal.el.querySelector('.fingerprints .fingerprint');
  1298. expect(el.textContent.trim()).toBe(
  1299. u.formatFingerprint(u.arrayBufferToHex(u.base64ToArrayBuffer('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI')))
  1300. );
  1301. expect(modal.el.querySelectorAll('input[type="radio"]').length).toBe(2);
  1302. const devicelist = _converse.devicelists.get(contact_jid);
  1303. expect(devicelist.devices.get('555').get('trusted')).toBe(0);
  1304. let trusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="1"]');
  1305. expect(trusted_radio.checked).toBe(true);
  1306. let untrusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="-1"]');
  1307. expect(untrusted_radio.checked).toBe(false);
  1308. // Test that the device can be set to untrusted
  1309. untrusted_radio.click();
  1310. trusted_radio = document.querySelector('input[type="radio"][name="555"][value="1"]');
  1311. expect(trusted_radio.hasAttribute('checked')).toBe(false);
  1312. expect(devicelist.devices.get('555').get('trusted')).toBe(-1);
  1313. untrusted_radio = document.querySelector('input[type="radio"][name="555"][value="-1"]');
  1314. expect(untrusted_radio.hasAttribute('checked')).toBe(true);
  1315. trusted_radio.click();
  1316. expect(devicelist.devices.get('555').get('trusted')).toBe(1);
  1317. done();
  1318. }));
  1319. });
  1320. describe("A chatbox with an active OMEMO session", function() {
  1321. it("will not show the spoiler toolbar button",
  1322. mock.initConverse(
  1323. null, ['rosterGroupsFetched'], {},
  1324. function (done, _converse) {
  1325. // TODO
  1326. done()
  1327. }));
  1328. });
  1329. }));