From 07efd601da909bc894c8e6a441090261d33dc501 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 14 Jul 2022 20:34:15 +0200 Subject: [PATCH] Use the native crypto library for SHA-1 instead of the one from Strophe --- src/headless/plugins/caps/index.js | 6 +++--- src/headless/plugins/caps/utils.js | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/headless/plugins/caps/index.js b/src/headless/plugins/caps/index.js index 048585f93..003bf577d 100644 --- a/src/headless/plugins/caps/index.js +++ b/src/headless/plugins/caps/index.js @@ -3,7 +3,7 @@ * @license Mozilla Public License (MPLv2) */ import { api, converse } from '@converse/headless/core'; -import { createCapsNode } from './utils.js'; +import { addCapsNode } from './utils.js'; const { Strophe } = converse.env; @@ -15,7 +15,7 @@ converse.plugins.add('converse-caps', { dependencies: ['converse-status'], initialize () { - api.listen.on('constructedPresence', (_, p) => (p.root().cnode(createCapsNode()).up() && p)); - api.listen.on('constructedMUCPresence', (_, p) => (p.root().cnode(createCapsNode()).up() && p)); + api.listen.on('constructedPresence', (_, p) => addCapsNode(p)); + api.listen.on('constructedMUCPresence', (_, p) => addCapsNode(p)); } }); diff --git a/src/headless/plugins/caps/utils.js b/src/headless/plugins/caps/utils.js index 081e02d3b..35f1b211e 100644 --- a/src/headless/plugins/caps/utils.js +++ b/src/headless/plugins/caps/utils.js @@ -1,5 +1,5 @@ -import SHA1 from 'strophe.js/src/sha1'; -import { _converse, converse } from '@converse/headless/core'; +import { _converse, converse } from '@converse/headless/core.js'; +import { arrayBufferToBase64 } from '@converse/headless/utils/arraybuffer.js'; const { Strophe, $build } = converse.env; @@ -7,7 +7,7 @@ function propertySort (array, property) { return array.sort((a, b) => { return a[property] > b[property] ? -1 : 1 }); } -function generateVerificationString () { +async function generateVerificationString () { const identities = _converse.api.disco.own.identities.get(); const features = _converse.api.disco.own.features.get(); @@ -20,14 +20,26 @@ function generateVerificationString () { let S = identities.reduce((result, id) => `${result}${id.category}/${id.type}/${id?.lang ?? ''}/${id.name}<`, ""); features.sort(); S = features.reduce((result, feature) => `${result}${feature}<`, S); - return SHA1.b64_sha1(S); + + const ab = await crypto.subtle.digest('SHA-1', S); + return arrayBufferToBase64(ab); } -export function createCapsNode () { +async function createCapsNode () { return $build("c", { 'xmlns': Strophe.NS.CAPS, 'hash': "sha-1", 'node': "https://conversejs.org", - 'ver': generateVerificationString() + 'ver': await generateVerificationString() }).nodeTree; } + + +/** + * Given a stanza, adds a XEP-0115 CAPS element + * @param { XMLElement } stanza + */ +export async function addCapsNode (stanza) { + const caps_el = await createCapsNode(); + return stanza.root().cnode(caps_el).up() && stanza; +}