-
Notifications
You must be signed in to change notification settings - Fork 227
Add some token source utility function tests + fix some bugs #1683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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 |
size-limit report 📦
|
src/room/token-source/utils.ts
Outdated
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
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.