8000 Merge pull request #278 from twilio/next-gen-prv-grant · devsec101/twilio-python@12023f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12023f8

Browse files
authored
Merge pull request twilio#278 from twilio/next-gen-prv-grant
Add programmable voice grant
2 parents f1ce985 + bb59738 commit 12023f8

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

tests/unit/jwt/test_access_token.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datetime import datetime
55
from nose.tools import assert_equal
66
from twilio.jwt import decode
7-
from twilio.jwt.access_token import AccessToken, ConversationsGrant, IpMessagingGrant
7+
from twilio.jwt.access_token import AccessToken, ConversationsGrant, IpMessagingGrant, VoiceGrant
88

99
ACCOUNT_SID = 'AC123'
1010
SIGNING_KEY_SID = 'SK123'
@@ -105,3 +105,28 @@ def test_grants(self):
105105
assert_equal(2, len(payload['grants']))
106106
assert_equal({}, payload['grants']['rtc'])
107107
assert_equal({}, payload['grants']['ip_messaging'])
108+
109+
def test_programmable_voice_grant(self):
110+
grant = VoiceGrant(
111+
outgoing_application_sid='AP123',
112+
outgoing_application_params={
113+
'foo': 'bar'
114+
}
115+
)
116+
117+
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
118+
scat.add_grant(grant)
119+
120+
token = str(scat)
121+
assert_is_not_none(token)
122+
payload = decode(token, 'secret')
123+
self._validate_claims(payload)
124+
assert_equal(1, len(payload['grants']))
125+
assert_equal({
126+
'outgoing': {
127+
'application_sid': 'AP123',
128+
'params': {
129+
'foo': 'bar'
130+
}
131+
}
132+
}, payload['grants']['voice'])

twilio/jwt/access_token.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import time
23
from twilio import jwt
34

@@ -46,6 +47,44 @@ def to_payload(self):
4647
return grant
4748

4849

50+
class VoiceGrant(object):
51+
""" Grant to access Twilio Programmable Voice"""
52+
def __init__(self,
53+
outgoing_application_sid=None,
54+
outgoing_application_params=None,
55+
push_credential_sid=None,
56+
endpoint_id=None):
57+
self.outgoing_application_sid = outgoing_application_sid
58+
""" :type : str """
59+
self.outgoing_application_params = outgoing_application_params
60+
""" :type : dict """
61+
self.push_credential_sid = push_credential_sid
62+
""" :type : str """
63+
self.endpoint_id = endpoint_id
64+
""" :type : str """
65+
66+
@property
67+
def key(self):
68+
return "voice"
69+
70+
def to_payload(self):
71+
grant = {}
72+
if self.outgoing_application_sid:
73+
grant['outgoing'] = {}
74+
grant['outgoing']['application_sid'] = self.outgoing_application_sid
75+
76+
if self.outgoing_application_params:
77+
grant['outgoing']['params'] = self.outgoing_application_params
78+
79+
if self.push_credential_sid:
80+
grant['push_credential_sid'] = self.push_credential_sid
81+
82+
if self.endpoint_id:
83+
grant['endpoint_id'] = self.endpoint_id
84+
85+
return grant
86+
87+
4988
class AccessToken(object):
5089
""" Access Token used to access Twilio Resources """
5190
def __init__(self, account_sid, signing_key_sid, secret,

0 commit comments

Comments
 (0)
0