Add some token source utility function tests + fix some bugs#1683
Merged
Add some token source utility function tests + fix some bugs#1683
Conversation
🦋 Changeset detectedLatest commit: 9e42aad The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
size-limit report 📦
|
1egoman
commented
Oct 10, 2025
src/room/token-source/utils.ts
Outdated
Comment on lines
+17
to
+22
| return expiresAt >= now; | ||
|
|
||
| const nbfInMilliseconds = jwtPayload.nbf * ONE_SECOND_IN_MILLISECONDS; | ||
| const nbfDate = new Date(nbfInMilliseconds); | ||
|
|
||
| const expInMilliseconds = jwtPayload.exp * ONE_SECOND_IN_MILLISECONDS; | ||
| const expDate = new Date(expInMilliseconds - ONE_MINUTE_IN_MILLISECONDS); | ||
|
|
||
| const isValid = nbfDate <= now && expDate > now; |
Contributor
Author
There was a problem hiding this comment.
The main bug was this return expiresAt >= now; line was backwards. It should have actually been return expiresAt < now;.
So, I fixed this and also at the same time added some logic to check the nbf field in the jwt to make sure that it's valid in the other direction.
…w that it isn't just checking expiry
xianshijing-lk
approved these changes
Oct 10, 2025
Contributor
xianshijing-lk
left a comment
There was a problem hiding this comment.
lgtm, just one nit, please feel free to ignore
| const expInMilliseconds = jwtPayload.exp * ONE_SECOND_IN_MILLISECONDS; | ||
| const expDate = new Date(expInMilliseconds - ONE_MINUTE_IN_MILLISECONDS); | ||
|
|
||
| return nbfDate <= now && expDate > now; |
Contributor
There was a problem hiding this comment.
nit, I think it will be slightly cleaner if we use sec rather than ms, for instance:
const nowSec = Date.now() / 1000;
const validFrom = jwtPayload.nbf;
const validUntil = jwtPayload.exp - 60; // 1 minute buffer
return nowSec >= validFrom && nowSec < validUntil;
lukasIO
approved these changes
Oct 13, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I happened to notice a bug in the token source token expiry checking logic that slipped through the original pull request, so I fixed it and added some tests (which I really should have done in the first place 😬 ) to make sure this doesn't regress again.