diff --git a/package-lock.json b/package-lock.json index ceef3302..3ffd095f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13095,6 +13095,8 @@ }, "node_modules/saslmechanisms": { "version": "0.1.1", + "resolved": "https://registry.npmjs.org/saslmechanisms/-/saslmechanisms-0.1.1.tgz", + "integrity": "sha512-pVlvK5ysevz8MzybRnDIa2YMxn0OJ7b9lDiWhMoaKPoJ7YkAg/7YtNjUgaYzElkwHxsw8dBMhaEn7UP6zxEwPg==", "engines": { "node": ">= 0.4.0" } @@ -14565,7 +14567,8 @@ "@xmpp/stream-management": "^0.13.2", "@xmpp/tcp": "^0.13.2", "@xmpp/tls": "^0.13.2", - "@xmpp/websocket": "^0.13.2" + "@xmpp/websocket": "^0.13.2", + "saslmechanisms": "^0.1.1" }, "engines": { "node": ">= 20" @@ -14753,8 +14756,7 @@ "dependencies": { "@xmpp/base64": "^0.13.2", "@xmpp/error": "^0.13.2", - "@xmpp/xml": "^0.13.2", - "saslmechanisms": "^0.1.1" + "@xmpp/xml": "^0.13.2" }, "engines": { "node": ">= 20" diff --git a/packages/client/index.js b/packages/client/index.js index e944db40..36dbc494 100644 --- a/packages/client/index.js +++ b/packages/client/index.js @@ -17,6 +17,7 @@ import _resourceBinding from "@xmpp/resource-binding"; import _sessionEstablishment from "@xmpp/session-establishment"; import _streamManagement from "@xmpp/stream-management"; +import SASLFactory from "saslmechanisms"; import scramsha1 from "@xmpp/sasl-scram-sha-1"; import plain from "@xmpp/sasl-plain"; import anonymous from "@xmpp/sasl-anonymous"; @@ -41,9 +42,21 @@ function client(options = {}) { const iqCaller = _iqCaller({ middleware, entity }); const iqCallee = _iqCallee({ middleware, entity }); const resolve = _resolve({ entity }); + + // SASL mechanisms - order matters and define priority + const saslFactory = new SASLFactory(); + const mechanisms = Object.entries({ + scramsha1, + plain, + anonymous, + }).map(([k, v]) => ({ [k]: v(saslFactory) })); + // Stream features - order matters and define priority const starttls = _starttls({ streamFeatures }); - const sasl = _sasl({ streamFeatures }, credentials || { username, password }); + const sasl = _sasl( + { streamFeatures, saslFactory }, + credentials || { username, password }, + ); const streamManagement = _streamManagement({ streamFeatures, entity, @@ -57,12 +70,6 @@ function client(options = {}) { iqCaller, streamFeatures, }); - // SASL mechanisms - order matters and define priority - const mechanisms = Object.entries({ - scramsha1, - plain, - anonymous, - }).map(([k, v]) => ({ [k]: v(sasl) })); return Object.assign(entity, { entity, @@ -81,6 +88,7 @@ function client(options = {}) { sessionEstablishment, streamManagement, mechanisms, + saslFactory, }); } diff --git a/packages/client/package.json b/packages/client/package.json index 59b6cdfb..e63da41f 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -24,7 +24,8 @@ "@xmpp/stream-management": "^0.13.2", "@xmpp/tcp": "^0.13.2", "@xmpp/tls": "^0.13.2", - "@xmpp/websocket": "^0.13.2" + "@xmpp/websocket": "^0.13.2", + "saslmechanisms": "^0.1.1" }, "browser": "./browser.js", "react-native": "./react-native.js", diff --git a/packages/sasl/index.js b/packages/sasl/index.js index 42f6297d..cbbfc37f 100644 --- a/packages/sasl/index.js +++ b/packages/sasl/index.js @@ -1,7 +1,6 @@ import { encode, decode } from "@xmpp/base64"; import SASLError from "./lib/SASLError.js"; import xml from "@xmpp/xml"; -import SASLFactory from "saslmechanisms"; // https://xmpp.org/rfcs/rfc6120.html#sasl @@ -74,12 +73,10 @@ async function authenticate(SASL, entity, mechname, credentials) { }); } -export default function sasl({ streamFeatures }, credentials) { - const SASL = new SASLFactory(); - +export default function sasl({ streamFeatures, saslFactory }, credentials) { streamFeatures.use("mechanisms", NS, async ({ stanza, entity }) => { const offered = getMechanismNames(stanza); - const supported = SASL._mechs.map(({ name }) => name); + const supported = saslFactory._mechs.map(({ name }) => name); // eslint-disable-next-line unicorn/prefer-array-find const intersection = supported.filter((mech) => { return offered.includes(mech); @@ -88,7 +85,7 @@ export default function sasl({ streamFeatures }, credentials) { if (typeof credentials === "function") { await credentials( - (creds) => authenticate(SASL, entity, mech, creds, stanza), + (creds) => authenticate(saslFactory, entity, mech, creds, stanza), mech, ); } else { @@ -96,15 +93,9 @@ export default function sasl({ streamFeatures }, credentials) { mech = "ANONYMOUS"; } - await authenticate(SASL, entity, mech, credentials, stanza); + await authenticate(saslFactory, entity, mech, credentials, stanza); } await entity.restart(); }); - - return { - use(...args) { - return SASL.use(...args); - }, - }; } diff --git a/packages/sasl/package.json b/packages/sasl/package.json index 8bb5ef1b..ec63bb0b 100644 --- a/packages/sasl/package.json +++ b/packages/sasl/package.json @@ -15,8 +15,7 @@ "dependencies": { "@xmpp/base64": "^0.13.2", "@xmpp/error": "^0.13.2", - "@xmpp/xml": "^0.13.2", - "saslmechanisms": "^0.1.1" + "@xmpp/xml": "^0.13.2" }, "engines": { "node": ">= 20"