Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Dec 21, 2024
1 parent 54db23a commit 7ce1b2f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 79 deletions.
10 changes: 2 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@ test:

ci:
npm install
make unit
npm run test
make lint
make restart
npx lerna run prepublish
node bundle.js
make e2e
npm run e2e
make bundlesize

unit:
npx jest

e2e:
NODE_TLS_REJECT_UNAUTHORIZED=0 npx jest --runInBand --config e2e.config.cjs

clean:
make stop
rm -f server/localhost.key
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"uglify-js": "^3.19.3"
},
"scripts": {
"test": "npx jest",
"e2e": "NODE_TLS_REJECT_UNAUTHORIZED=0 npx jest --runInBand --config e2e.config.cjs",
"preversion": "make bundle"
},
"engines": {
Expand Down
8 changes: 3 additions & 5 deletions packages/sasl-ht-sha-256-none/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"use strict";

// https://datatracker.ietf.org/doc/draft-schmaus-kitten-sasl-ht/
const createHmac = require("create-hmac");
import createHmac from "create-hmac";

function Mechanism() {}

Expand All @@ -24,6 +22,6 @@ Mechanism.prototype.final = (data) => {
}
};

module.exports = function sasl2(sasl) {
export default function sasl2(sasl) {
sasl.use(Mechanism);
};
}
1 change: 1 addition & 0 deletions packages/sasl-ht-sha-256-none/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bugs": "http://github.com/xmppjs/xmpp.js/issues",
"version": "0.13.0",
"license": "ISC",
"type": "module",
"keywords": [
"XMPP",
"sasl",
Expand Down
42 changes: 19 additions & 23 deletions packages/sasl2/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";

const { encode, decode } = require("@xmpp/base64");
const SASLError = require("./lib/SASLError");
const jid = require("@xmpp/jid");
const xml = require("@xmpp/xml");
const SASLFactory = require("saslmechanisms");
import { encode, decode } from "@xmpp/base64";
import SASLError from "@xmpp/sasl/lib/SASLError.js";
import jid from "@xmpp/jid";
import xml from "@xmpp/xml";
import SASLFactory from "saslmechanisms";

// https://xmpp.org/extensions/xep-0388.html
// https://xmpp.org/extensions/xep-0386.html
Expand Down Expand Up @@ -164,7 +162,7 @@ async function authenticate(
await Promise.all([promise, ...hPromises]);
}

module.exports = function sasl2({ streamFeatures }, credentials, userAgent) {
export default function sasl2({ streamFeatures }, credentials, userAgent) {
const SASL = new SASLFactory();
const handlers = {};
const bindHandlers = {};
Expand Down Expand Up @@ -195,20 +193,18 @@ module.exports = function sasl2({ streamFeatures }, credentials, userAgent) {
.filter((mech) => mech.canFast || mech.canOther);

if (typeof credentials === "function") {
await credentials(
(creds, mech) =>
authenticate(
SASL,
handlers,
bindHandlers,
entity,
mech,
creds,
userAgent,
stanza,
),
intersection,
);
await credentials((creds, mech) => {
authenticate(
SASL,
handlers,
bindHandlers,
entity,
mech,
creds,
userAgent,
stanza,
);
}, intersection);
} else {
let mech = intersection[0]?.name;
if (!credentials.username && !credentials.password) {
Expand Down Expand Up @@ -241,4 +237,4 @@ module.exports = function sasl2({ streamFeatures }, credentials, userAgent) {
bindHandlers[feature] = handler;
},
};
};
}
6 changes: 2 additions & 4 deletions packages/sasl2/lib/SASLError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

const XMPPError = require("@xmpp/error");
import XMPPError from "@xmpp/error";

// https://xmpp.org/rfcs/rfc6120.html#sasl-errors

Expand All @@ -11,4 +9,4 @@ class SASLError extends XMPPError {
}
}

module.exports = SASLError;
export default SASLError;
2 changes: 2 additions & 0 deletions packages/sasl2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"bugs": "http://github.com/xmppjs/xmpp.js/issues",
"version": "0.13.0",
"license": "ISC",
"type": "module",
"keywords": [
"XMPP",
"sasl"
],
"dependencies": {
"@xmpp/base64": "^0.13.0",
"@xmpp/error": "^0.13.0",
"@xmpp/sasl": "^0.13.2",
"@xmpp/jid": "^0.13.0",
"@xmpp/xml": "^0.13.0",
"saslmechanisms": "^0.1.1"
Expand Down
71 changes: 32 additions & 39 deletions packages/sasl2/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
"use strict";

const test = require("ava");
const { mockClient, promise } = require("@xmpp/test");
const parse = require("@xmpp/xml/lib/parse.js");
import { mockClient, promise } from "@xmpp/test";
import parse from "@xmpp/xml/lib/parse.js";

const username = "foo";
const password = "bar";
const credentials = { username, password };

test("no compatibles mechanisms", async (t) => {
test("no compatibles mechanisms", async () => {
const { entity } = mockClient({ username, password });

entity.mockInput(
Expand All @@ -20,11 +17,11 @@ test("no compatibles mechanisms", async (t) => {
);

const error = await promise(entity, "error");
t.true(error instanceof Error);
t.is(error.message, "No compatible mechanism");
expect(error instanceof Error).toBe(true);
expect(error.message).toBe("No compatible mechanism");
});

test("with object credentials", async (t) => {
test("with object credentials", async () => {
const { entity } = mockClient({ credentials });

entity.mockInput(
Expand All @@ -35,8 +32,7 @@ test("with object credentials", async (t) => {
</features>,
);

t.deepEqual(
await promise(entity, "send"),
expect(await promise(entity, "send")).toEqual(
<authenticate xmlns="urn:xmpp:sasl:2" mechanism="PLAIN">
<initial-response>AGZvbwBiYXI=</initial-response>
</authenticate>,
Expand All @@ -48,11 +44,13 @@ test("with object credentials", async (t) => {
await promise(entity, "online");
});

test("with function credentials", async (t) => {
test("with function credentials", async () => {
const mech = "PLAIN";

function authenticate(auth, mechanisms) {
t.deepEqual(mechanisms, [{ name: mech, canFast: false, canOther: true }]);
expect(mechanisms).toEqual([
{ name: mech, canFast: false, canOther: true },
]);
return auth(credentials, mech);
}

Expand All @@ -66,8 +64,7 @@ test("with function credentials", async (t) => {
</features>,
);

t.deepEqual(
await promise(entity, "send"),
expect(await promise(entity, "send")).toEqual(
<authenticate xmlns="urn:xmpp:sasl:2" mechanism={mech}>
<initial-response>AGZvbwBiYXI=</initial-response>
</authenticate>,
Expand All @@ -79,7 +76,7 @@ test("with function credentials", async (t) => {
await promise(entity, "online");
});

test("failure", async (t) => {
test("failure", async () => {
const { entity } = mockClient({ credentials });

entity.mockInput(
Expand All @@ -90,8 +87,7 @@ test("failure", async (t) => {
</features>,
);

t.deepEqual(
await promise(entity, "send"),
expect(await promise(entity, "send")).toEqual(
<authenticate xmlns="urn:xmpp:sasl:2" mechanism="PLAIN">
<initial-response>AGZvbwBiYXI=</initial-response>
</authenticate>,
Expand All @@ -106,13 +102,13 @@ test("failure", async (t) => {
entity.mockInput(failure);

const error = await promise(entity, "error");
t.true(error instanceof Error);
t.is(error.name, "SASLError");
t.is(error.condition, "some-condition");
t.is(error.element, failure);
expect(error instanceof Error).toBe(true);
expect(error.name).toBe("SASLError");
expect(error.condition).toBe("some-condition");
expect(error.element).toBe(failure);
});

test("prefers SCRAM-SHA-1", async (t) => {
test("prefers SCRAM-SHA-1", async () => {
const { entity } = mockClient({ credentials });

entity.mockInput(
Expand All @@ -126,10 +122,10 @@ test("prefers SCRAM-SHA-1", async (t) => {
);

const result = await promise(entity, "send");
t.deepEqual(result.attrs.mechanism, "SCRAM-SHA-1");
expect(result.attrs.mechanism).toEqual("SCRAM-SHA-1");
});

test("use ANONYMOUS if username and password are not provided", async (t) => {
test("use ANONYMOUS if username and password are not provided", async () => {
const { entity } = mockClient();

entity.mockInput(
Expand All @@ -143,10 +139,10 @@ test("use ANONYMOUS if username and password are not provided", async (t) => {
);

const result = await promise(entity, "send");
t.deepEqual(result.attrs.mechanism, "ANONYMOUS");
expect(result.attrs.mechanism).toEqual("ANONYMOUS");
});

test("with whitespaces", async (t) => {
test("with whitespaces", async () => {
const { entity } = mockClient();

entity.mockInput(
Expand All @@ -164,10 +160,10 @@ test("with whitespaces", async (t) => {
);

const result = await promise(entity, "send");
t.deepEqual(result.attrs.mechanism, "ANONYMOUS");
expect(result.attrs.mechanism).toEqual("ANONYMOUS");
});

test("with bind2", async (t) => {
test("with bind2", async () => {
const { entity } = mockClient({
credentials,
clientId: "uniqueid",
Expand All @@ -186,8 +182,7 @@ test("with bind2", async (t) => {
</features>,
);

t.deepEqual(
await promise(entity, "send"),
expect(await promise(entity, "send")).toEqual(
<authenticate xmlns="urn:xmpp:sasl:2" mechanism="PLAIN">
<initial-response>AGZvbwBiYXI=</initial-response>
<user-agent id="uniqueid">
Expand All @@ -206,10 +201,10 @@ test("with bind2", async (t) => {
await promise(entity, "online");
});

test("with FAST", async (t) => {
test("with FAST", async () => {
const { entity } = mockClient({
credentials: (callback, mechanisms) => {
t.deepEqual(mechanisms, [
expect(mechanisms).toEqual([
{ canFast: true, canOther: false, name: "HT-SHA-256-NONE" },
{ canFast: false, canOther: true, name: "PLAIN" },
]);
Expand All @@ -233,8 +228,7 @@ test("with FAST", async (t) => {
</features>,
);

t.deepEqual(
await promise(entity, "send"),
expect(await promise(entity, "send")).toEqual(
<authenticate xmlns="urn:xmpp:sasl:2" mechanism="PLAIN">
<initial-response>AGZvbwBiYXI=</initial-response>
<request-token xmlns="urn:xmpp:fast:0" mechanism="HT-SHA-256-NONE" />
Expand All @@ -247,10 +241,10 @@ test("with FAST", async (t) => {
await promise(entity, "online");
});

test("with FAST token", async (t) => {
test("with FAST token", async () => {
const { entity } = mockClient({
credentials: (callback, mechanisms) => {
t.deepEqual(mechanisms, [
expect(mechanisms).toEqual([
{ canFast: true, canOther: false, name: "HT-SHA-256-NONE" },
{ canFast: false, canOther: true, name: "PLAIN" },
]);
Expand All @@ -271,8 +265,7 @@ test("with FAST token", async (t) => {
</features>,
);

t.deepEqual(
await promise(entity, "send"),
expect(await promise(entity, "send")).toEqual(
<authenticate xmlns="urn:xmpp:sasl:2" mechanism="HT-SHA-256-NONE">
<initial-response>
bnVsbAAAXAywUfR/w4Mr9SUDUtNAgPDajNI073fqfiZLMYcmfA==
Expand Down

0 comments on commit 7ce1b2f

Please sign in to comment.