-
Notifications
You must be signed in to change notification settings - Fork 228
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'livekit-client': patch | ||
--- | ||
|
||
Fix bug in isResponseExpired token expiry checking logic |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { TokenSourceResponse } from '@livekit/protocol'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { decodeTokenPayload, isResponseTokenValid } from './utils'; | ||
|
||
// Test JWTs created for test purposes only. | ||
// None of these actually auth against anything. | ||
const TOKENS = { | ||
// Nbf date set at 1234567890 seconds (Fri Feb 13 2009 23:31:30 GMT+0000) | ||
// Exp date set at 9876543210 seconds (Fri Dec 22 2282 20:13:30 GMT+0000) | ||
// A dummy roomConfig value is also set, with room_config.name = "test room name", and room_config.agents = [{"agentName": "test agent name","metadata":"test agent metadata"}] | ||
VALID: | ||
'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZXhwIjo5ODc2NTQzMjEwLCJuYmYiOjEyMzQ1Njc4OTAsImlhdCI6MTIzNDU2Nzg5MCwicm9vbUNvbmZpZyI6eyJuYW1lIjoidGVzdCByb29tIG5hbWUiLCJlbXB0eVRpbWVvdXQiOjAsImRlcGFydHVyZVRpbWVvdXQiOjAsIm1heFBhcnRpY2lwYW50cyI6MCwibWluUGxheW91dERlbGF5IjowLCJtYXhQbGF5b3V0RGVsYXkiOjAsInN5bmNTdHJlYW1zIjpmYWxzZSwiYWdlbnRzIjpbeyJhZ2VudE5hbWUiOiJ0ZXN0IGFnZW50IG5hbWUiLCJtZXRhZGF0YSI6InRlc3QgYWdlbnQgbWV0YWRhdGEifV0sIm1ldGFkYXRhIjoiIn19.EDetpHG8cSubaApzgWJaQrpCiSy9KDBlfCfVdIydbQ-_CHiNnXOK_f_mCJbTf9A-duT1jmvPOkLrkkWFT60XPQ', | ||
|
||
// Nbf date set at 9876543210 seconds (Fri Dec 22 2282 20:13:30 GMT+0000) | ||
// Exp date set at 9876543211 seconds (Fri Dec 22 2282 20:13:31 GMT+0000) | ||
NBF_IN_FUTURE: | ||
'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZXhwIjo5ODc2NTQzMjExLCJuYmYiOjk4NzY1NDMyMTAsImlhdCI6MTIzNDU2Nzg5MH0.DcMmdKrD76eJg7IUBZqoTRDvBaXtCcwtuE5h7IwVXhG_6nvgxN_ix30_AmLgnYhvhkN-x9dTRPoHg-CME72AbQ', | ||
|
||
// Nbf date set at 1234567890 seconds (Fri Feb 13 2009 23:31:30 GMT+0000) | ||
// Exp date set at 1234567891 seconds (Fri Feb 13 2009 23:31:31 GMT+0000) | ||
EXP_IN_PAST: | ||
'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZXhwIjoxMjM0NTY3ODkxLCJuYmYiOjEyMzQ1Njc4OTAsImlhdCI6MTIzNDU2Nzg5MH0.OYP1NITayotBYt0mioInLJmaIM0bHyyR-yG6iwKyQDzhoGha15qbsc7dOJlzz4za1iW5EzCgjc2_xGxqaSu5XA', | ||
}; | ||
|
||
describe('isResponseTokenValid', () => { | ||
it('should find a valid jwt not expired', () => { | ||
const isValid = isResponseTokenValid( | ||
TokenSourceResponse.fromJson({ | ||
serverUrl: 'ws://localhost:7800', | ||
participantToken: TOKENS.VALID, | ||
}), | ||
); | ||
expect(isValid).toBe(true); | ||
}); | ||
it('should find a long ago expired jwt as expired', () => { | ||
const isValid = isResponseTokenValid( | ||
TokenSourceResponse.fromJson({ | ||
serverUrl: 'ws://localhost:7800', | ||
participantToken: TOKENS.EXP_IN_PAST, | ||
}), | ||
); | ||
expect(isValid).toBe(false); | ||
}); | ||
it('should find a jwt that has not become active yet as expired', () => { | ||
const isValid = isResponseTokenValid( | ||
TokenSourceResponse.fromJson({ | ||
serverUrl: 'ws://localhost:7800', | ||
participantToken: TOKENS.NBF_IN_FUTURE, | ||
}), | ||
); | ||
expect(isValid).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('decodeTokenPayload', () => { | ||
it('should extract roomconfig metadata from a token', () => { | ||
const payload = decodeTokenPayload(TOKENS.VALID); | ||
expect(payload.roomConfig?.name).toBe('test room name'); | ||
expect(payload.roomConfig?.agents).toHaveLength(1); | ||
expect(payload.roomConfig?.agents![0].agentName).toBe('test agent name'); | ||
expect(payload.roomConfig?.agents![0].metadata).toBe('test agent metadata'); | ||
}); | ||
}); |
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
Oops, something went wrong.
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.
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: