From cef162cea93ad7e2a11ab686a0c5247257bc4081 Mon Sep 17 00:00:00 2001 From: Sarah Stringer Date: Wed, 13 Oct 2021 16:23:08 -0700 Subject: [PATCH 1/2] Add PlaybackGrant. --- tests/unit/jwt/test_access_token.py | 18 +++++++++++++++++- twilio/jwt/access_token/grants.py | 13 +++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/unit/jwt/test_access_token.py b/tests/unit/jwt/test_access_token.py index cf54c1383a..9779808e8b 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,21 @@ def test_task_router_grant(self): 'role': 'worker' }, decoded_token.payload['grants']['task_router']) + def test_playback_grant(self): + 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..a487fc5996 100644 --- a/twilio/jwt/access_token/grants.py +++ b/twilio/jwt/access_token/grants.py @@ -196,3 +196,16 @@ def to_payload(self): grant['role'] = self.role return grant + + +class PlaybackGrant(AccessTokenGrant): + """Grant to access Twilio Live stream""" + def __init__(self, grant=None): + self.grant = grant + + @property + def key(self): + return "player" + + def to_payload(self): + return self.grant From 7dc8195d13249f844be8a50c9ca70dec75c941fc Mon Sep 17 00:00:00 2001 From: Sarah Stringer Date: Wed, 13 Oct 2021 16:41:39 -0700 Subject: [PATCH 2/2] Add docstrings. --- tests/unit/jwt/test_access_token.py | 1 + twilio/jwt/access_token/grants.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/tests/unit/jwt/test_access_token.py b/tests/unit/jwt/test_access_token.py index 9779808e8b..e58f4b0e16 100644 --- a/tests/unit/jwt/test_access_token.py +++ b/tests/unit/jwt/test_access_token.py @@ -244,6 +244,7 @@ def test_task_router_grant(self): }, 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', diff --git a/twilio/jwt/access_token/grants.py b/twilio/jwt/access_token/grants.py index a487fc5996..e59b2ac768 100644 --- a/twilio/jwt/access_token/grants.py +++ b/twilio/jwt/access_token/grants.py @@ -200,12 +200,16 @@ def to_payload(self): 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