Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 1, 2025
1 parent 434072b commit 876b6f9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
1 change: 0 additions & 1 deletion packages/client-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@xmpp/connection": "^0.14.0",
"@xmpp/jid": "^0.14.0",
"@xmpp/sasl": "^0.14.0",
"@xmpp/sasl2": "^0.14.0",
"@xmpp/xml": "^0.14.0",
"saslmechanisms": "^0.1.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/client-core/src/fast/fast.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAvailableMechanisms } from "@xmpp/sasl2";
import { getAvailableMechanisms } from "@xmpp/sasl";
import xml from "@xmpp/xml";
import SASLFactory from "saslmechanisms";

Expand Down
17 changes: 9 additions & 8 deletions packages/sasl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import { procedure } from "@xmpp/events";

const NS = "urn:ietf:params:xml:ns:xmpp-sasl";

function getMechanismNames(element) {
return element.getChildElements().map((el) => el.text());
export function getAvailableMechanisms(element, NS, saslFactory) {
const offered = new Set(
element.getChildren("mechanism", NS).map((m) => m.text()),
);
const supported = saslFactory._mechs.map(({ name }) => name);
return supported.filter((mech) => offered.has(mech));
}

async function authenticate({ saslFactory, entity, mechanism, credentials }) {
Expand Down Expand Up @@ -66,11 +70,8 @@ async function authenticate({ saslFactory, entity, mechanism, credentials }) {

export default function sasl({ streamFeatures, saslFactory }, onAuthenticate) {
streamFeatures.use("mechanisms", NS, async ({ entity }, _next, element) => {
const offered = getMechanismNames(element);
const supported = saslFactory._mechs.map(({ name }) => name);
const intersection = supported.filter((mech) => offered.includes(mech));

if (intersection.length === 0) {
const mechanisms = getAvailableMechanisms(element, NS, saslFactory);
if (mechanisms.length === 0) {
throw new SASLError("SASL: No compatible mechanism available.");
}

Expand All @@ -83,7 +84,7 @@ export default function sasl({ streamFeatures, saslFactory }, onAuthenticate) {
});
}

await onAuthenticate(done, intersection);
await onAuthenticate(done, mechanisms);

await entity.restart();
});
Expand Down
9 changes: 1 addition & 8 deletions packages/sasl2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ import { encode, decode } from "@xmpp/base64";
import SASLError from "@xmpp/sasl/lib/SASLError.js";
import xml from "@xmpp/xml";
import { procedure } from "@xmpp/events";
import { getAvailableMechanisms } from "@xmpp/sasl";

// https://xmpp.org/extensions/xep-0388.html

const NS = "urn:xmpp:sasl:2";

export function getAvailableMechanisms(element, NS, saslFactory) {
const offered = new Set(
element.getChildren("mechanism", NS).map((m) => m.text()),
);
const supported = saslFactory._mechs.map(({ name }) => name);
return supported.filter((mech) => offered.has(mech));
}

async function authenticate({
saslFactory,
entity,
Expand Down

0 comments on commit 876b6f9

Please sign in to comment.