Skip to content

Commit

Permalink
Move sasl factory to client
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Dec 22, 2024
1 parent 2e1782f commit c7e17c9
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 80 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.

36 changes: 19 additions & 17 deletions packages/client/browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { xml, jid, Client } from "@xmpp/client-core";
import getDomain from "./lib/getDomain.js";
import SASLFactory from "saslmechanisms";

import _reconnect from "@xmpp/reconnect";
import _websocket from "@xmpp/websocket";
Expand All @@ -18,16 +19,8 @@ import plain from "@xmpp/sasl-plain";
import anonymous from "@xmpp/sasl-anonymous";

function client(options = {}) {
const {
resource,
credentials,
username,
password,
clientId,
software,
device,
...params
} = options;
const { resource, credentials, username, password, ...params } = options;
const { clientId, software, device } = params;

const { domain, service } = params;
if (!domain && service) {
Expand All @@ -45,12 +38,25 @@ function client(options = {}) {
const iqCallee = _iqCallee({ middleware, entity });
const resolve = _resolve({ entity });
// Stream features - order matters and define priority

const saslFactory = new SASLFactory();
// SASL mechanisms - order matters and define priority
const mechanisms = Object.entries({
plain,
anonymous,
}).map(([k, v]) => ({ [k]: v(saslFactory) }));

const sasl2 = _sasl2(
{ streamFeatures },
{ streamFeatures, saslFactory },
credentials || { username, password },
{ clientId, software, device },
);
const sasl = _sasl({ streamFeatures }, credentials || { username, password });

const sasl = _sasl(
{ streamFeatures, saslFactory },
credentials || { username, password },
);

const streamManagement = _streamManagement({
streamFeatures,
entity,
Expand All @@ -65,11 +71,6 @@ function client(options = {}) {
iqCaller,
streamFeatures,
});
// SASL mechanisms - order matters and define priority
const mechanisms = Object.entries({
plain,
anonymous,
}).map(([k, v]) => ({ [k]: [v(sasl2), v(sasl)] }));

return Object.assign(entity, {
entity,
Expand All @@ -80,6 +81,7 @@ function client(options = {}) {
iqCaller,
iqCallee,
resolve,
saslFactory,
sasl2,
sasl,
resourceBinding,
Expand Down
28 changes: 19 additions & 9 deletions packages/client/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { xml, jid, Client } from "@xmpp/client-core";
import getDomain from "./lib/getDomain.js";
import SASLFactory from "saslmechanisms";

import _reconnect from "@xmpp/reconnect";
import _websocket from "@xmpp/websocket";
Expand Down Expand Up @@ -45,12 +46,27 @@ function client(options = {}) {
const resolve = _resolve({ entity });
// Stream features - order matters and define priority
const starttls = _starttls({ streamFeatures });

const saslFactory = new SASLFactory();
// SASL mechanisms - order matters and define priority
const mechanisms = Object.entries({
scramsha1,
htsha256,
plain,
anonymous,
}).map(([k, v]) => ({ [k]: v(saslFactory) }));

const sasl2 = _sasl2(
{ streamFeatures },
{ streamFeatures, saslFactory },
credentials || { username, password },
{ clientId, software, device },
);
const sasl = _sasl({ streamFeatures }, credentials || { username, password });

const sasl = _sasl(
{ streamFeatures, saslFactory },
credentials || { username, password },
);

const streamManagement = _streamManagement({
streamFeatures,
entity,
Expand All @@ -65,13 +81,6 @@ function client(options = {}) {
iqCaller,
streamFeatures,
});
// SASL mechanisms - order matters and define priority
const mechanisms = Object.entries({
scramsha1,
htsha256,
plain,
anonymous,
}).map(([k, v]) => ({ [k]: [v(sasl2), v(sasl)] }));

return Object.assign(entity, {
entity,
Expand All @@ -85,6 +94,7 @@ function client(options = {}) {
iqCallee,
resolve,
starttls,
saslFactory,
sasl2,
sasl,
resourceBinding,
Expand Down
7 changes: 4 additions & 3 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
"@xmpp/reconnect": "^0.13.2",
"@xmpp/resolve": "^0.13.2",
"@xmpp/resource-binding": "^0.13.2",
"@xmpp/sasl2": "^0.13.0",
"@xmpp/sasl": "^0.13.2",
"@xmpp/sasl-anonymous": "^0.13.2",
"@xmpp/sasl-ht-sha-256-none": "^0.13.0",
"@xmpp/sasl-plain": "^0.13.2",
"@xmpp/sasl-scram-sha-1": "^0.13.2",
"@xmpp/sasl-ht-sha-256-none": "^0.13.0",
"@xmpp/sasl2": "^0.13.0",
"@xmpp/session-establishment": "^0.13.2",
"@xmpp/starttls": "^0.13.2",
"@xmpp/stream-features": "^0.13.2",
"@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
37 changes: 19 additions & 18 deletions packages/client/react-native.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { xml, jid, Client } from "@xmpp/client-core";
import getDomain from "./lib/getDomain.js";
import SASLFactory from "saslmechanisms";

import _reconnect from "@xmpp/reconnect";
import _websocket from "@xmpp/websocket";
Expand All @@ -8,7 +9,6 @@ import _streamFeatures from "@xmpp/stream-features";
import _iqCaller from "@xmpp/iq/caller.js";
import _iqCallee from "@xmpp/iq/callee.js";
import _resolve from "@xmpp/resolve";

import _sasl2 from "@xmpp/sasl2";
import _sasl from "@xmpp/sasl";
import _resourceBinding from "@xmpp/resource-binding";
Expand All @@ -19,16 +19,8 @@ import plain from "@xmpp/sasl-plain";
import anonymous from "@xmpp/sasl-anonymous";

function client(options = {}) {
const {
resource,
credentials,
username,
password,
clientId,
software,
device,
...params
} = options;
const { resource, credentials, username, password, ...params } = options;
const { clientId, software, device } = params;

const { domain, service } = params;
if (!domain && service) {
Expand All @@ -46,12 +38,25 @@ function client(options = {}) {
const iqCallee = _iqCallee({ middleware, entity });
const resolve = _resolve({ entity });
// Stream features - order matters and define priority

const saslFactory = new SASLFactory();
// SASL mechanisms - order matters and define priority
const mechanisms = Object.entries({
plain,
anonymous,
}).map(([k, v]) => ({ [k]: v(saslFactory) }));

const sasl2 = _sasl2(
{ streamFeatures },
{ streamFeatures, saslFactory },
credentials || { username, password },
{ clientId, software, device },
);
const sasl = _sasl({ streamFeatures }, credentials || { username, password });

const sasl = _sasl(
{ streamFeatures, saslFactory },
credentials || { username, password },
);

const streamManagement = _streamManagement({
streamFeatures,
entity,
Expand All @@ -66,11 +71,6 @@ function client(options = {}) {
iqCaller,
streamFeatures,
});
// SASL mechanisms - order matters and define priority
const mechanisms = Object.entries({
plain,
anonymous,
}).map(([k, v]) => ({ [k]: [v(sasl2), v(sasl)] }));

return Object.assign(entity, {
entity,
Expand All @@ -81,6 +81,7 @@ function client(options = {}) {
iqCaller,
iqCallee,
resolve,
saslFactory,
sasl2,
sasl,
resourceBinding,
Expand Down
2 changes: 1 addition & 1 deletion packages/sasl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { xmpp } from "@xmpp/client";

const client = xmpp({ credentials: authenticate });

async function authenticate(auth, mechanism) {
async function authenticate(saslFactory, mechanism) {
console.debug("authenticate", mechanism);
const credentials = {
username: await prompt("enter username"),
Expand Down
21 changes: 7 additions & 14 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 All @@ -14,8 +13,8 @@ function getMechanismNames(features) {
.map((el) => el.text());
}

async function authenticate(SASL, entity, mechname, credentials) {
const mech = SASL.create([mechname]);
async function authenticate(saslFactory, entity, mechname, credentials) {
const mech = saslFactory.create([mechname]);
if (!mech) {
throw new Error("No compatible mechanism");
}
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,19 @@ 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);
},
};
return {};
}
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
Loading

0 comments on commit c7e17c9

Please sign in to comment.