Skip to content

Commit

Permalink
Fix isTokenValid
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 16, 2025
1 parent 2534ed4 commit e1f5c42
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
33 changes: 23 additions & 10 deletions packages/client-core/src/fast/fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ export default function fast({ sasl2, entity }) {
authenticate,
entity,
userAgent,
token,
credentials,
streamFeatures,
features,
}) {
// Unavailable
if (!fast.mechanism) {
return false;
}

const { token } = credentials;
// Invalid or unavailable token
if (!isTokenValid(token, fast.mechanisms)) {
requestToken(streamFeatures);
return false;
}

Expand Down Expand Up @@ -83,23 +90,25 @@ export default function fast({ sasl2, entity }) {
err instanceof SASLError &&
["not-authorized", "credentials-expired"].includes(err.condition)
) {
this.delete();
await this.delete();
requestToken(streamFeatures);
return false;
}
entity.emit("error", err);
return false;
}
},
_requestToken(streamFeatures) {
streamFeatures.push(
xml("request-token", {
xmlns: NS,
mechanism: fast.mechanism,
}),
);
},
});

function requestToken(streamFeatures) {
streamFeatures.push(
xml("request-token", {
xmlns: NS,
mechanism: fast.mechanism,
}),
);
}

function reset() {
fast.mechanism = null;
fast.mechanisms = [];
Expand Down Expand Up @@ -139,13 +148,17 @@ export default function fast({ sasl2, entity }) {
}

export function isTokenValid(token, mechanisms) {
if (!token) return false;

// Avoid an error round trip if the server does not support the token mechanism anymore
if (!mechanisms.includes(token.mechanism)) {
return false;
}

// Avoid an error round trip if the token is already expired
if (new Date(token.expiry) <= new Date()) {
return false;
}

return true;
}
21 changes: 13 additions & 8 deletions packages/client-core/src/fast/isTokenValid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,45 @@ it("returns false if the token.mechanism is not available", async () => {
expect(
isTokenValid(
{
expires: datetime(tomorrow),
expiry: datetime(tomorrow),
mechanism: "bar",
},
["foo"],
),
);
).toBe(false);
});

it("returns true if the token.mechanism is available", async () => {
expect(
isTokenValid({ expires: datetime(tomorrow), mechanism: "foo" }, ["foo"]),
);
isTokenValid({ expiry: datetime(tomorrow), mechanism: "foo" }, ["foo"]),
).toBe(true);
});

it("returns false if the token is expired", async () => {
expect(
isTokenValid(
{
expires: datetime(yesterday),
expiry: datetime(yesterday),
mechanism: "foo",
},
["foo"],
),
);
).toBe(false);
});

it("returns true if the token is not expired", async () => {
expect(
isTokenValid(
{
expires: datetime(tomorrow),
expiry: datetime(tomorrow),
mechanism: "foo",
},
["foo"],
),
);
).toBe(true);
});

it("returns false if the token is nullish", async () => {
expect(isTokenValid(null)).toBe(false);
expect(isTokenValid(undefined)).toBe(false);
});
32 changes: 13 additions & 19 deletions packages/sasl2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,20 @@ export default function sasl2({ streamFeatures, saslFactory }, onAuthenticate) {
);

async function done(credentials, mechanism, userAgent) {
if (fast_available) {
const { token } = credentials;
// eslint-disable-next-line unicorn/no-negated-condition
if (!token) {
fast._requestToken(streamFeatures);
} else {
const success = await fast.auth({
authenticate,
entity,
userAgent,
token,
streamFeatures,
features,
credentials,
});
if (success) return;
// If fast authentication fails, continue and try with sasl
}
}
// Try fast
const success = await fast.auth({
authenticate,
entity,
userAgent,
streamFeatures,
features,
credentials,
});
if (success) return;

// fast.auth may mutate streamFeatures to request a token

// If fast authentication fails, continue and try without
await authenticate({
entity,
userAgent,
Expand Down

0 comments on commit e1f5c42

Please sign in to comment.