Skip to content

Commit

Permalink
fast: Fix configurable saveToken and fetchToken
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 8, 2025
1 parent 8337bcc commit b0dd761
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
43 changes: 19 additions & 24 deletions packages/client-core/src/fast/fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@ import SASLFactory from "saslmechanisms";

const NS = "urn:xmpp:fast:0";

export default function fast({ sasl2 }, { saveToken, fetchToken } = {}) {
export default function fast({ sasl2 }) {
const saslFactory = new SASLFactory();

const fast = new EventEmitter();

let token;
saveToken ??= async function saveToken(t) {
token = t;
};
fetchToken ??= async function fetchToken() {
return token;
};

const fast = new EventEmitter();
Object.assign(fast, {
mechanism: null,
mechanisms: [],
async saveToken() {
async saveToken(t) {
token = t;
},
async fetchToken() {
return token;
},
async save(token) {
try {
await saveToken();
await this.saveToken(token);
} catch (err) {
fast.emit("error", err);
}
},
async fetchToken() {
async fetch() {
try {
return await fetchToken();
return this.fetchToken();
} catch (err) {
fast.emit("error", err);
}
Expand Down Expand Up @@ -107,17 +106,13 @@ export default function fast({ sasl2 }, { saveToken, fetchToken } = {}) {
},
async (element) => {
if (element.is("token", NS)) {
try {
await saveToken({
// The token is bound by the mechanism
// > Servers MUST bind tokens to the mechanism selected by the client in its original request, and reject attempts to use them with other mechanisms.
mechanism: fast.mechanism,
token: element.attrs.token,
expiry: element.attrs.expiry,
});
} catch (err) {
fast.emit("error", err);
}
await fast.save({
// The token is bound by the mechanism
// > Servers MUST bind tokens to the mechanism selected by the client in its original request, and reject attempts to use them with other mechanisms.
mechanism: fast.mechanism,
token: element.attrs.token,
expiry: element.attrs.expiry,
});
}
},
);
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/createOnAuthenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function createOnAuthenticate(credentials, userAgent) {
return;
}

credentials.token = await fast?.fetchToken?.();
credentials.token = await fast?.fetch?.();

await authenticate(credentials, mechanisms[0], userAgent);
};
Expand Down

0 comments on commit b0dd761

Please sign in to comment.