Skip to content
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

Expose kind and attributes in AccessToken #215

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/livekit-server-sdk/src/AccessToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as jose from 'jose';
import { describe, expect, it } from 'vitest';
import { AccessToken, TokenVerifier } from './AccessToken';
import { ClaimGrants } from './grants';
import type { ClaimGrants } from './grants';

const testApiKey = 'abcdefg';
const testSecret = 'abababa';
Expand Down Expand Up @@ -63,14 +63,18 @@ describe('verify token is valid', () => {
it('can decode encoded token', async () => {
const t = new AccessToken(testApiKey, testSecret);
t.sha256 = 'abcdefg';
t.kind = 'agent';
t.addGrant({ roomCreate: true });
t.attributes = { foo: 'bar', live: 'kit' };

const v = new TokenVerifier(testApiKey, testSecret);
const decoded = await v.verify(await t.toJwt());

expect(decoded).not.toBe(undefined);
expect(decoded.sha256).toEqual('abcdefg');
expect(decoded.video?.roomCreate).toBeTruthy();
expect(decoded.kind).toEqual('agent');
expect(decoded.attributes).toEqual(t.attributes);
});
});

Expand Down
33 changes: 29 additions & 4 deletions packages/livekit-server-sdk/src/AccessToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//
// SPDX-License-Identifier: Apache-2.0
import * as jose from 'jose';
import { ClaimGrants, VideoGrant, claimsToJwtPayload } from './grants.js';
import type { ClaimGrants, VideoGrant } from './grants.js';
import { claimsToJwtPayload } from './grants.js';

// 6 hours
const defaultTTL = `6h`;
Expand Down Expand Up @@ -44,8 +45,8 @@

/**
* Creates a new AccessToken
* @param apiKey API Key, can be set in env LIVEKIT_API_KEY

Check warning on line 48 in packages/livekit-server-sdk/src/AccessToken.ts

View workflow job for this annotation

GitHub Actions / Lint & Test

tsdoc-param-tag-missing-hyphen: The @param block should be followed by a parameter name and then a hyphen
* @param apiSecret Secret, can be set in env LIVEKIT_API_SECRET

Check warning on line 49 in packages/livekit-server-sdk/src/AccessToken.ts

View workflow job for this annotation

GitHub Actions / Lint & Test

tsdoc-param-tag-missing-hyphen: The @param block should be followed by a parameter name and then a hyphen
*/
constructor(apiKey?: string, apiSecret?: string, options?: AccessTokenOptions) {
if (!apiKey) {
Expand Down Expand Up @@ -82,21 +83,45 @@

/**
* Adds a video grant to this token.
* @param grant
* @param grant -
*/
addGrant(grant: VideoGrant) {
this.grants.video = { ...(this.grants.video ?? {}), ...grant };
}

get name(): string | undefined {
return this.grants.name;
}

set name(name: string) {
this.grants.name = name;
}

get metadata(): string | undefined {
return this.grants.metadata;
}

/**
* Set metadata to be passed to the Participant, used only when joining the room
*/
set metadata(md: string) {
this.grants.metadata = md;
}

set name(name: string) {
this.grants.name = name;
get attributes(): Record<string, string> | undefined {
return this.grants.attributes;
}

set attributes(attrs: Record<string, string>) {
this.grants.attributes = attrs;
}

get kind(): string | undefined {
return this.grants.kind;
}

set kind(kind: string) {
this.grants.kind = kind;
}

get sha256(): string | undefined {
Expand Down
6 changes: 4 additions & 2 deletions packages/livekit-server-sdk/src/grants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0
import { TrackSource } from '@livekit/protocol';
import { JWTPayload } from 'jose';
import type { JWTPayload } from 'jose';

export function trackSourceToString(source: TrackSource) {
switch (source) {
Expand Down Expand Up @@ -83,14 +83,16 @@ export interface VideoGrant {
/** participant is recording the room, when set, allows room to indicate it's being recorded */
recorder?: boolean;

/** participant acts as a server side participant/bot/agent */
/** participant allowed to connect to LiveKit as Agent Framework worker */
agent?: boolean;
}

/** @internal */
export interface ClaimGrants extends JWTPayload {
name?: string;
video?: VideoGrant;
kind?: string;
metadata?: string;
attributes?: Record<string, string>;
sha256?: string;
}
Loading