Skip to content

Commit

Permalink
Move saslfactory from sasl to client
Browse files Browse the repository at this point in the history
Will be needed to share the factory with sasl2
  • Loading branch information
sonnyp committed Dec 22, 2024
1 parent c9032bd commit 17b4373
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
8 changes: 5 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions packages/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -81,6 +88,7 @@ function client(options = {}) {
sessionEstablishment,
streamManagement,
mechanisms,
saslFactory,
});
}

Expand Down
3 changes: 2 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 4 additions & 13 deletions packages/sasl/index.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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);
Expand All @@ -88,23 +85,17 @@ 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 {
if (!credentials.username && !credentials.password) {
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);
},
};
}
3 changes: 1 addition & 2 deletions packages/sasl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 17b4373

Please sign in to comment.