diff --git a/tests/unit/jwt/test_access_token.py b/tests/unit/jwt/test_access_token.py index cf54c1383a..e58f4b0e16 100644 --- a/tests/unit/jwt/test_access_token.py +++ b/tests/unit/jwt/test_access_token.py @@ -12,7 +12,8 @@ VideoGrant, ConversationsGrant, TaskRouterGrant, - ChatGrant + ChatGrant, + PlaybackGrant, ) ACCOUNT_SID = 'AC123' @@ -242,6 +243,22 @@ def test_task_router_grant(self): 'role': 'worker' }, decoded_token.payload['grants']['task_router']) + def test_playback_grant(self): + """Test that PlaybackGrants are created and decoded correctly.""" + grant = { + 'requestCredentials': None, + 'playbackUrl': 'https://000.us-east-1.playback.live-video.net/api/video/v1/us-east-000.channel.000?token=xxxxx', + 'playerStreamerSid': 'VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' + } + scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') + scat.add_grant(PlaybackGrant(grant=grant)) + token = scat.to_jwt() + assert_is_not_none(token) + decoded_token = AccessToken.from_jwt(token, 'secret') + self._validate_claims(decoded_token.payload) + assert_equal(1, len(decoded_token.payload['grants'])) + assert_equal(grant, decoded_token.payload['grants']['player']) + def test_pass_grants_in_constructor(self): grants = [ VideoGrant(), diff --git a/twilio/jwt/access_token/grants.py b/twilio/jwt/access_token/grants.py index 1f42b6e56b..e59b2ac768 100644 --- a/twilio/jwt/access_token/grants.py +++ b/twilio/jwt/access_token/grants.py @@ -196,3 +196,20 @@ def to_payload(self): grant['role'] = self.role return grant + + +class PlaybackGrant(AccessTokenGrant): + """Grant to access Twilio Live stream""" + + def __init__(self, grant=None): + """Initialize a PlaybackGrant with a grant retrieved from the Twilio API.""" + self.grant = grant + + @property + def key(self): + """Return the grant's key.""" + return "player" + + def to_payload(self): + """Return the grant.""" + return self.grant