Skip to content

Commit

Permalink
Add PlaybackGrant.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahcstringer committed Oct 14, 2021
1 parent 10e7a9b commit 3fea908
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/jwt/AccessToken.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ declare namespace AccessToken {
endpointId?: string;
}

export interface PlaybackGrantOptions {
grant?: object;
}

export interface PlaybackGrantPayload {
grant?: object;
}

export class PlaybackGrant extends Grant<
PlaybackGrantOptions,
PlaybackGrantPayload,
'player'
> implements PlaybackGrantOptions {
grant?: object
}

export interface AccessTokenOptions {
/**
* Time to live in seconds
Expand Down
20 changes: 20 additions & 0 deletions lib/jwt/AccessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ _.extend(VoiceGrant.prototype, {
}
});

/**
* @constructor
* @param {object} options - ...
* @param {string} options.grant - The PlaybackGrant retrieved from Twilio's API
*/
function PlaybackGrant(options) {
options = options || {};
this.grant = options.grant;
}

_.extend(PlaybackGrant.prototype, {
key: 'player',
toPayload: function() {
var grant = {};
if (this.grant) { grant = this.grant; }
return grant;
}
});

/**
* @constructor
* @param {string} accountSid - The account's unique ID to which access is scoped
Expand Down Expand Up @@ -234,6 +253,7 @@ AccessToken.SyncGrant = SyncGrant;
AccessToken.VideoGrant = VideoGrant;
AccessToken.ConversationsGrant = util.deprecate(ConversationsGrant, 'ConversationsGrant is deprecated, use VideoGrant instead.');
AccessToken.TaskRouterGrant = TaskRouterGrant;
AccessToken.PlaybackGrant = PlaybackGrant;
AccessToken.DEFAULT_ALGORITHM = 'HS256';
AccessToken.ALGORITHMS = [
'HS256',
Expand Down
37 changes: 37 additions & 0 deletions spec/unit/jwt/AccessToken.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ describe('AccessToken', function() {
});
});

it('should create token with playback grant', function() {
var token = new twilio.jwt.AccessToken(accountSid, keySid, 'secret');
token.identity = 'ID@example.com';

var playbackGrant = {
'requestCredentials': null,
'playbackUrl': 'https://000.us-east-1.playback.live-video.net/api/video/v1/us-east-000.channel.000?token=xxxxx',
'playerStreamerSid': 'VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}

var grant = new twilio.jwt.AccessToken.PlaybackGrant();
grant.grant = playbackGrant;
token.addGrant(grant);

var decoded = jwt.verify(token.toJwt(), 'secret');
expect(decoded.grants).toEqual({
identity: 'ID@example.com',
player: playbackGrant
});
});

it('should create token with multiple grants', function() {
var token = new twilio.jwt.AccessToken(accountSid, keySid, 'secret');
token.identity = 'ID@example.com';
Expand Down Expand Up @@ -518,6 +539,22 @@ describe('AccessToken', function() {
});
});
});

describe('PlaybackGrant', function() {
it('should only populate set properties', function() {
var grant = new twilio.jwt.AccessToken.PlaybackGrant();
expect(grant.toPayload()).toEqual({});

var playbackGrant = {
'requestCredentials': null,
'playbackUrl': 'https://000.us-east-1.playback.live-video.net/api/video/v1/us-east-000.channel.000?token=xxxxx',
'playerStreamerSid': 'VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}

grant.grant = playbackGrant;
expect(grant.toPayload()).toEqual(playbackGrant);
});
});
});

});

0 comments on commit 3fea908

Please sign in to comment.