Skip to content

Commit

Permalink
much improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Dec 30, 2024
1 parent bf95c32 commit d887785
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 50 deletions.
4 changes: 3 additions & 1 deletion packages/client-core/src/bind2/bind2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import xml from "@xmpp/xml";

const NS_BIND = "urn:xmpp:bind:0";

export default function bind2({ sasl2 }, tag) {
export default function bind2({ sasl2, entity }, tag) {
const features = new Map();

sasl2.use(
Expand All @@ -22,6 +22,8 @@ export default function bind2({ sasl2 }, tag) {
);
},
(element) => {
if (!element.is("bound")) return;
entity._ready(false);
for (const child of element.getChildElements()) {
const feature = features.get(child.getNS());
feature?.[1]?.(child);
Expand Down
8 changes: 1 addition & 7 deletions packages/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import _starttls from "@xmpp/starttls";
import _sasl2 from "@xmpp/sasl2";
import _sasl from "@xmpp/sasl";
import _resourceBinding from "@xmpp/resource-binding";
import _sessionEstablishment from "@xmpp/session-establishment";
import _streamManagement from "@xmpp/stream-management";
import _bind2 from "@xmpp/client-core/src/bind2/bind2.js";

Expand Down Expand Up @@ -65,7 +64,7 @@ function client(options = {}) {
);

// SASL2 inline features
const bind2 = _bind2({ sasl2 }, resource);
const bind2 = _bind2({ sasl2, entity }, resource);

// Stream features - order matters and define priority
const sasl = _sasl(
Expand All @@ -83,10 +82,6 @@ function client(options = {}) {
{ iqCaller, streamFeatures },
resource,
);
const sessionEstablishment = _sessionEstablishment({
iqCaller,
streamFeatures,
});

return Object.assign(entity, {
entity,
Expand All @@ -104,7 +99,6 @@ function client(options = {}) {
sasl2,
sasl,
resourceBinding,
sessionEstablishment,
streamManagement,
mechanisms,
bind2,
Expand Down
2 changes: 1 addition & 1 deletion packages/component-core/lib/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Component extends Connection {
}

this._jid(this.options.domain);
this._status("online", this.jid);
this._ready(false);
}
}

Expand Down
9 changes: 8 additions & 1 deletion packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Connection extends EventEmitter {
}

_reset() {
this.jid = this.jid?.bare() ?? null;
this.status = "offline";
this._detachSocket();
this._detachParser();
Expand Down Expand Up @@ -185,6 +184,14 @@ class Connection extends EventEmitter {
this.emit(status, ...args);
}

_ready(resumed = false) {
if (resumed) {
this.status = "online";
} else {
this._status("online", this.jid);
}
}

async _end() {
let el;
try {
Expand Down
1 change: 1 addition & 0 deletions packages/resource-binding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function bind(entity, iqCaller, resource) {
const result = await iqCaller.set(makeBindElement(resource));
const jid = result.getChildText("jid");
entity._jid(jid);
entity._ready(false);
return jid;
}

Expand Down
5 changes: 2 additions & 3 deletions packages/sasl/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ test("with object credentials", async () => {
);

entity.mockInput(<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl" />);

await promise(entity, "online");
});

test("with function credentials", async () => {
expect.assertions(2);

const mech = "PLAIN";
let promise_authenticate;

async function onAuthenticate(authenticate, mechanisms) {
expect(mechanisms).toEqual([mech]);
Expand Down Expand Up @@ -79,7 +78,7 @@ test("with function credentials", async () => {

entity.mockInput(<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl" />);

await promise(entity, "online");
await promise_authenticate;
});

test("Mechanism not found", async () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/sasl2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ async function authenticate({
}

// https://xmpp.org/extensions/xep-0388.html#success
// this is a bare JID, unless resource binding has occurred, in which case it is a full JID.
// this is a bare JID, unless resource binding or stream resumption has occurred, in which case it is a full JID.
const aid = element.getChildText("authorization-identifier");
if (aid) entity._jid(aid);
if (aid) {
entity._jid(aid);
}

for (const child of element.getChildElements()) {
const feature = features.get(child.getNS());
Expand Down Expand Up @@ -132,8 +134,6 @@ export default function sasl2({ streamFeatures, saslFactory }, onAuthenticate) {
}

await onAuthenticate(done, intersection);
// Not online yet, wait for next features
return true;
},
);

Expand Down
26 changes: 20 additions & 6 deletions packages/sasl2/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ test("with object credentials", async () => {
</authenticate>,
);

entity.mockInput(<success xmlns="urn:xmpp:sasl:2" />);
entity.mockInput(<features xmlns="http://etherx.jabber.org/streams" />);
const jid = "username@localhost/example~Ln8YSSzsyK-b_3-vIFvOJNnE";

await promise(entity, "online");
expect(entity.jid?.toString()).not.toBe(jid);

entity.mockInput(
<success xmlns="urn:xmpp:sasl:2">
<authorization-identifier>{jid}</authorization-identifier>
</success>,
);

expect(entity.jid.toString()).toBe(jid);
});

test("with function credentials", async () => {
Expand Down Expand Up @@ -74,10 +81,17 @@ test("with function credentials", async () => {
</authenticate>,
);

entity.mockInput(<success xmlns="urn:xmpp:sasl:2" />);
entity.mockInput(<features xmlns="http://etherx.jabber.org/streams" />);
const jid = "username@localhost/example~Ln8YSSzsyK-b_3-vIFvOJNnE";

expect(entity.jid?.toString()).not.toBe(jid);

entity.mockInput(
<success xmlns="urn:xmpp:sasl:2">
<authorization-identifier>{jid}</authorization-identifier>
</success>,
);

await promise(entity, "online");
expect(entity.jid.toString()).toBe(jid);
});

test("failure", async () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/session-establishment/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { mockClient, promise, timeout } from "@xmpp/test";
import sessionEstablishment from "./index.js";

test("mandatory", async () => {
const { entity } = mockClient();
sessionEstablishment(entity);

entity.mockInput(
<features xmlns="http://etherx.jabber.org/streams">
Expand All @@ -15,12 +17,11 @@ test("mandatory", async () => {
expect(child).toEqual(
<session xmlns="urn:ietf:params:xml:ns:xmpp-session" />,
);

await promise(entity, "online");
});

test("optional", async () => {
const { entity } = mockClient();
sessionEstablishment(entity);

entity.mockInput(
<features xmlns="http://etherx.jabber.org/streams">
Expand All @@ -32,8 +33,6 @@ test("optional", async () => {

const promiseSend = promise(entity, "send");

await promise(entity, "online");

await timeout(promiseSend, 0).catch((err) => {
expect(err.name).toBe("TimeoutError");
});
Expand Down
4 changes: 0 additions & 4 deletions packages/stream-features/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import route from "./route.js";

/**
* References
* https://xmpp.org/rfcs/rfc6120.html#streams-negotiation Stream Negotiation
Expand All @@ -8,8 +6,6 @@ import route from "./route.js";
*/

export default function streamFeatures({ middleware }) {
middleware.use(route());

function use(name, xmlns, handler) {
return middleware.use((ctx, next) => {
const { stanza } = ctx;
Expand Down
12 changes: 0 additions & 12 deletions packages/stream-features/route.js

This file was deleted.

10 changes: 3 additions & 7 deletions packages/stream-management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ export default function streamManagement({
max: null,
};

let address = null;

function resumed() {
sm.enabled = true;
if (address) entity.jid = address;
entity.status = "online";
entity._ready(true);
}

function failed() {
Expand All @@ -83,8 +80,7 @@ export default function streamManagement({
sm.max = max;
}

entity.on("online", (jid) => {
address = jid;
entity.on("online", () => {
sm.outbound = 0;
sm.inbound = 0;
});
Expand Down Expand Up @@ -147,7 +143,7 @@ function setupStreamFeature({
try {
await resume(entity, sm);
resumed();
return true;
return;
// If resumption fails, continue with session establishment
} catch {
failed();
Expand Down

0 comments on commit d887785

Please sign in to comment.