Skip to content

Commit 2d05aec

Browse files
Add PlaybackGrant (#122)
1 parent 0fdf370 commit 2d05aec

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

client/jwt/access_token.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ func decodeGrants(grants interface{}) []BaseGrant {
169169
grant = &VideoGrant{}
170170
case "voice":
171171
grant = &VoiceGrant{}
172+
case "player":
173+
grant = &PlaybackGrant{}
172174
}
173175

174176
if errJson := json.Unmarshal(data, &grant); errJson == nil {

client/jwt/access_token_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,32 @@ func TestTaskRouterGrant(t *testing.T) {
372372
assert.Equal(t, "worker", taskRouterGrantDecoded["role"])
373373
}
374374

375+
func TestPlaybackGrant(t *testing.T) {
376+
accessToken := CreateAccessToken(Params)
377+
accessToken.AddGrant(&PlaybackGrant{
378+
"requestCredentials": nil,
379+
"playbackUrl": "https://000.us-east-1.playback.live-video.net/api/video/v1/us-east-000.channel.000?token=xxxxx",
380+
"playerStreamerSid": "VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
381+
})
382+
383+
token, err := accessToken.ToJwt()
384+
assert.Nil(t, err)
385+
assert.NotNil(t, token)
386+
387+
decodedToken, err := accessToken.FromJwt(token, "secret")
388+
assert.Nil(t, err)
389+
assert.NotNil(t, decodedToken)
390+
assert.Len(t, decodedToken.Grants, 1)
391+
payload := decodedToken.Payload()
392+
assert.Len(t, payload["grants"], 1)
393+
394+
playbackGrantDecoded := payload["grants"].(map[string]interface{})["player"].(map[string]interface{})
395+
assert.NotNil(t, playbackGrantDecoded)
396+
assert.Equal(t, nil, playbackGrantDecoded["requestCredentials"])
397+
assert.Equal(t, "https://000.us-east-1.playback.live-video.net/api/video/v1/us-east-000.channel.000?token=xxxxx", playbackGrantDecoded["playbackUrl"])
398+
assert.Equal(t, "VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", playbackGrantDecoded["playerStreamerSid"])
399+
}
400+
375401
func TestMultipleGrants(t *testing.T) {
376402
accessToken := CreateAccessToken(Params)
377403
accessToken.AddGrant(&TaskRouterGrant{
@@ -452,4 +478,7 @@ func TestGrantsToString(t *testing.T) {
452478

453479
videoGrant := &VideoGrant{}
454480
assert.True(t, strings.HasPrefix(videoGrant.ToString(), "<VideoGrant"))
481+
482+
playbackGrant := &PlaybackGrant{}
483+
assert.True(t, strings.HasPrefix(playbackGrant.ToString(), "<PlaybackGrant"))
455484
}

client/jwt/playback_grant.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package jwt
2+
3+
import "fmt"
4+
5+
type PlaybackGrant map[string]interface{}
6+
7+
func (playbackGrant *PlaybackGrant) Key() string {
8+
return "player"
9+
}
10+
11+
func (playbackGrant *PlaybackGrant) ToPayload() map[string]interface{} {
12+
return *playbackGrant
13+
}
14+
15+
func (playbackGrant *PlaybackGrant) ToString() string {
16+
return fmt.Sprintf("<%s %s>", "PlaybackGrant", playbackGrant.ToPayload())
17+
}

0 commit comments

Comments
 (0)