8000 Add ChatGrant, deprecate IpMessagingGrant (#379) · SituProbability/twilio-python@d9b12a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9b12a4

Browse files
jingmingjmctwilio
authored andcommitted
Add ChatGrant, deprecate IpMessagingGrant (twilio#379)
1 parent 9321baf commit d9b12a4

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

tests/unit/jwt/test_access_token.py

Lines changed: 16 additions & 1 deletion
122
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
VoiceGrant,
1212
VideoGrant,
1313
ConversationsGrant,
14-
TaskRouterGrant
14+
TaskRouterGrant,
15+
ChatGrant
1516
)
1617

1718
ACCOUNT_SID = 'AC123'
@@ -121,6 +122,20 @@ def test_ip_messaging_grant(self):
121
'push_credential_sid': 'CR123'
122123
}, decoded_token.payload['grants']['ip_messaging'])
123124

125+
def test_chat_grant(self):
126+
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
127+
scat.add_grant(ChatGrant(service_sid='IS123', push_credential_sid='CR123'))
128+
129+
token = scat.to_jwt()
130+
assert_is_not_none(token)
131+
decoded_token = AccessToken.from_jwt(token, 'secret')
132+
self._validate_claims(decoded_token.payload)
133+
assert_equal(1, len(decoded_token.payload['grants']))
134+
assert_equal({
135+
'service_sid': 'IS123',
136+
'push_credential_sid': 'CR123'
137+
}, decoded_token.payload['grants']['chat'])
138+
124139
def test_sync_grant(self):
125140
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
126141
scat.identity = "bender"

twilio/jwt/access_token/grants.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,38 @@ def new_func(*args, **kwargs):
1818
return new_func
1919

2020

21+
class ChatGrant(AccessTokenGrant):
22+
"""Grant to access Twilio Chat"""
23+
24+
def __init__(self, service_sid=None, endpoint_id=None,
25+
deployment_role_sid=None, push_credential_sid=None):
26+
self.service_sid = service_sid
27+
self.endpoint_id = endpoint_id
28+
self.deployment_role_sid = deployment_role_sid
29+
self.push_credential_sid = push_credential_sid
30+
31+
@property
32+
def key(self):
33+
return "chat"
34+
35+
def to_payload(self):
36+
grant = {}
37+
if self.service_sid:
38+
grant['service_sid'] = self.service_sid
39+
if self.endpoint_id:
40+
grant['endpoint_id'] = self.endpoint_id
41+
if self.deployment_role_sid:
42+
grant['deployment_role_sid'] = self.deployment_role_sid
43+
if self.push_credential_sid:
44+
grant['push_credential_sid'] = self.push_credential_sid
45+
46+
return grant
47+
48+
2149
class IpMessagingGrant(AccessTokenGrant):
2250
"""Grant to access Twilio IP Messaging"""
51+
52+
@deprecated
2353
def __init__(self, service_sid=None, endpoint_id=None,
2454
deployment_role_sid=None, push_credential_sid=None):
2555
self.service_sid = service_sid

0 commit comments

Comments
 (0)
0