Skip to content

Commit

Permalink
Expose protobuf TrackSource and map TrackSource claims to string (liv…
Browse files Browse the repository at this point in the history
…ekit#145)

* Expose protobuf TrackSource and map TrackSource claims to string

* Create heavy-chefs-refuse.md

* Fix implementation and add unit test
  • Loading branch information
lukasIO authored Feb 29, 2024
1 parent ca1fb83 commit 0eb8152
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-chefs-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-server-sdk": minor
---

Expose protobuf TrackSource and map TrackSource claims to string
5 changes: 3 additions & 2 deletions src/AccessToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as jose from 'jose';
import { ClaimGrants, VideoGrant } from './grants.js';
import { ClaimGrants, VideoGrant, claimsToJwtPayload } from './grants.js';

// 6 hours
const defaultTTL = `6h`;
Expand Down Expand Up @@ -111,7 +111,8 @@ export class AccessToken {
// TODO: check for video grant validity

const secret = new TextEncoder().encode(this.apiSecret);
const jwt = new jose.SignJWT(this.grants)

const jwt = new jose.SignJWT(claimsToJwtPayload(this.grants))
.setProtectedHeader({ alg: 'HS256' })
.setIssuer(this.apiKey)
.setExpirationTime(this.ttl)
Expand Down
27 changes: 27 additions & 0 deletions src/grants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest';
import { ClaimGrants, VideoGrant, claimsToJwtPayload } from './grants';
import { TrackSource } from './proto/livekit_models_pb';

describe('ClaimGrants are parsed correctly', () => {
it('parses TrackSource correctly to strings', () => {
const grant: VideoGrant = {
canPublishSources: [
TrackSource.CAMERA,
TrackSource.MICROPHONE,
TrackSource.SCREEN_SHARE,
TrackSource.SCREEN_SHARE_AUDIO,
],
};

const claim: ClaimGrants = { video: grant };

const jwtPayload = claimsToJwtPayload(claim);
expect(jwtPayload.video).toBeTypeOf('object');
expect(jwtPayload.video?.canPublishSources).toEqual([
'camera',
'microphone',
'screen_share',
'screen_share_audio',
]);
});
});
30 changes: 25 additions & 5 deletions src/grants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import { JWTPayload } from 'jose';
import { TrackSource } from './proto/livekit_models_pb';

export function trackSourceToString(source: TrackSource) {
switch (source) {
case TrackSource.CAMERA:
return 'camera';
case TrackSource.MICROPHONE:
return 'microphone';
case TrackSource.SCREEN_SHARE:
return 'screen_share';
case TrackSource.SCREEN_SHARE_AUDIO:
return 'screen_share_audio';
default:
throw new TypeError(`Cannot convert TrackSource ${source} to string`);
}
}

export enum TrackSource {
CAMERA = 'camera',
MICROPHONE = 'microphone',
SCREEN_SHARE = 'screen_share',
SCREEN_SHARE_AUDIO = 'screen_share_audio',
export function claimsToJwtPayload(
grant: ClaimGrants,
): JWTPayload & { video?: Record<string, unknown> } {
const claim: Record<string, any> = { ...grant };
// eslint-disable-next-line no-restricted-syntax
if (Array.isArray(claim.video?.canPublishSources)) {
claim.video.canPublishSources = claim.video.canPublishSources.map(trackSourceToString);
}
return claim;
}

export interface VideoGrant {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export {
Room,
TrackInfo,
TrackType,
TrackSource,
} from './proto/livekit_models_pb.js';

0 comments on commit 0eb8152

Please sign in to comment.