From b0dd761e9d5a9667519a0f9ebbf74c267ad7638f Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Wed, 8 Jan 2025 21:11:26 +0100 Subject: [PATCH] fast: Fix configurable saveToken and fetchToken --- packages/client-core/src/fast/fast.js | 43 +++++++++------------ packages/client/lib/createOnAuthenticate.js | 2 +- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/packages/client-core/src/fast/fast.js b/packages/client-core/src/fast/fast.js index 5af90be5..9b064b05 100644 --- a/packages/client-core/src/fast/fast.js +++ b/packages/client-core/src/fast/fast.js @@ -5,32 +5,31 @@ import SASLFactory from "saslmechanisms"; const NS = "urn:xmpp:fast:0"; -export default function fast({ sasl2 }, { saveToken, fetchToken } = {}) { +export default function fast({ sasl2 }) { const saslFactory = new SASLFactory(); - const fast = new EventEmitter(); - let token; - saveToken ??= async function saveToken(t) { - token = t; - }; - fetchToken ??= async function fetchToken() { - return token; - }; + const fast = new EventEmitter(); Object.assign(fast, { mechanism: null, mechanisms: [], - async saveToken() { + async saveToken(t) { + token = t; + }, + async fetchToken() { + return token; + }, + async save(token) { try { - await saveToken(); + await this.saveToken(token); } catch (err) { fast.emit("error", err); } }, - async fetchToken() { + async fetch() { try { - return await fetchToken(); + return this.fetchToken(); } catch (err) { fast.emit("error", err); } @@ -107,17 +106,13 @@ export default function fast({ sasl2 }, { saveToken, fetchToken } = {}) { }, async (element) => { if (element.is("token", NS)) { - try { - await saveToken({ - // The token is bound by the mechanism - // > Servers MUST bind tokens to the mechanism selected by the client in its original request, and reject attempts to use them with other mechanisms. - mechanism: fast.mechanism, - token: element.attrs.token, - expiry: element.attrs.expiry, - }); - } catch (err) { - fast.emit("error", err); - } + await fast.save({ + // The token is bound by the mechanism + // > Servers MUST bind tokens to the mechanism selected by the client in its original request, and reject attempts to use them with other mechanisms. + mechanism: fast.mechanism, + token: element.attrs.token, + expiry: element.attrs.expiry, + }); } }, ); diff --git a/packages/client/lib/createOnAuthenticate.js b/packages/client/lib/createOnAuthenticate.js index e248eb8f..907a180b 100644 --- a/packages/client/lib/createOnAuthenticate.js +++ b/packages/client/lib/createOnAuthenticate.js @@ -16,7 +16,7 @@ export default function createOnAuthenticate(credentials, userAgent) { return; } - credentials.token = await fast?.fetchToken?.(); + credentials.token = await fast?.fetch?.(); await authenticate(credentials, mechanisms[0], userAgent); };