8000 First person grants · saroshfarhan/twilio-python@5647a0f · GitHub
[go: up one dir, main page]

Skip to content

Commit 5647a0f

Browse files
committed
First person grants
1 parent 366c029 commit 5647a0f

File tree

2 files changed

+75
-69
lines changed

2 files changed

+75
-69
lines changed

tests/test_access_token.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from nose.tools import assert_equal
44
from twilio.jwt import decode
5-
from twilio.access_token import AccessToken
5+
from twilio.access_token import AccessToken, ConversationGrant, IpMessagingGrant
66

77
ACCOUNT_SID = 'AC123'
88
SIGNING_KEY_SID = 'SK123'
@@ -28,54 +28,43 @@ def _validate_claims(self, payload):
2828
def test_empty_grants(self):
2929
scat = AccessToken(SIGNING_KEY_SID, ACCOUNT_SID, 'secret')
3030
token = str(scat)
31-
assert_is_not_none(token)
32-
payload = decode(token, 'secret')
33-
self._validate_claims(payload)
34-
assert_equal([], payload['grants'])
3531

36-
def test_single_grant(self):
37-
scat = AccessToken(SIGNING_KEY_SID, ACCOUNT_SID, 'secret')
38-
scat.add_grant('https://api.twilio.com/**')
39-
token = str(scat)
4032
assert_is_not_none(token)
4133
payload = decode(token, 'secret')
4234
self._validate_claims(payload)
43-
assert_equal(1, len(payload['grants']))
44-
assert_equal('https://api.twilio.com/**', payload['grants'][0]['res'])
45-
assert_equal(['*'], payload['grants'][0]['act'])
35+
assert_equal({}, payload['grants'])
4636

47-
def test_endpoint_grant(self):
37+
def test_conversations_grant(self):
4838
scat = AccessToken(SIGNING_KEY_SID, ACCOUNT_SID, 'secret')
49-
scat.add_endpoint_grant('bob')
39+
scat.add_grant(ConversationGrant())
40+
5041
token = str(scat)
5142
assert_is_not_none(token)
5243
payload = decode(token, 'secret')
5344
self._validate_claims(payload)
5445
assert_equal(1, len(payload['grants']))
55-
assert_equal('sip:bob@AC123.endpoint.twilio.com',
56-
payload['grants'][0]['res'])
57-
assert_equal(['listen', 'invite'], payload['grants'][0]['act'])
46+
assert_equal({}, payload['grants']['rtc'])
5847

59-
def test_rest_grant(self):
48+
def test_ip_messaging_grant(self):
6049
scat = AccessToken(SIGNING_KEY_SID, ACCOUNT_SID, 'secret')
61-
scat.add_rest_grant('/Apps')
50+
scat.add_grant(IpMessagingGrant())
51+
6252
token = str(scat)
6353
assert_is_not_none(token)
6454
payload = decode(token, 'secret')
6555
self._validate_claims(payload)
6656
assert_equal(1, len(payload['grants']))
67-
assert_equal('https://api.twilio.com/2010-04-01/Accounts/AC123/Apps',
68-
payload['grants'][0]['res'])
69-
assert_equal(['*'], payload['grants'][0]['act'])
57+
assert_equal({}, payload['grants']['ip_messaging'])
7058

71-
def test_enable_nts(self):
59+
def test_grants(self):
7260
scat = AccessToken(SIGNING_KEY_SID, ACCOUNT_SID, 'secret')
73-
scat.enable_nts()
61+
scat.add_grant(ConversationGrant())
62+
scat.add_grant(IpMessagingGrant())
63+
7464
token = str(scat)
7565
assert_is_not_none(token)
7666
payload = decode(token, 'secret')
7767
self._validate_claims(payload)
78-
assert_equal(1, len(payload['grants']))
79-
assert_equal('https://api.twilio.com/2010-04-01/Accounts/AC123/Tokens.json',
80-
payload['grants'][0]['res'])
81-
assert_equal(['POST'], payload['grants'][0]['act'])
68+
assert_equal(2, len(payload['grants']))
69+
assert_equal({}, payload['grants']['rtc'])
70+
assert_equal({}, payload['grants']['ip_messaging'])

twilio/access_token.py

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,84 @@
11
import time
22
import jwt
33

4-
ALL = '*'
54

6-
# HTTP Actions
7-
HTTP_DELETE = 'DELETE'
8-
HTTP_GET = 'GET'
9-
HTTP_POST = 'POST'
10-
HTTP_PUT = 'PUT'
5+
class IpMessagingGrant(object):
6+
def __init__(self, service_sid=None, endpoint_id=None,
7+
role_sid=None, credential_sid=None):
8+
self.service_sid = service_sid
9+
self.endpoint_id = endpoint_id
10+
self.role_sid = role_sid
11+
self.credential_sid = credential_sid
1112

12-
# Client Actions
13-
CLIENT_LISTEN = 'listen'
14-
CLIENT_INVITE = 'invite'
13+
@property
14+
def key(self):
15+
return "ip_messaging"
16+
17+
def to_payload(self):
18+
grant = {}
19+
if self.service_sid:
20+
grant['service_sid'] = self.service_sid
21+
if self.endpoint_id:
22+
grant['endpoint_id'] = self.endpoint_id
23+
if self.role_sid:
24+
grant['deployment_role_sid'] = self.role_sid
25+
if self.credential_sid:
26+
grant['push_credential_sid'] = self.credential_sid
27+
28+
return grant
29+
30+
31+
class ConversationGrant(object):
32+
def __init__(self, configuration_profile_sid=None):
33+
self.configuration_profile_sid = configuration_profile_sid
34+
35+
@property
36+
def key(self):
37+
return "rtc"
38+
39+
def to_payload(self):
40+
grant = {}
41+
if self.configuration_profile_sid:
42+
grant['configuration_profile_sid'] = self.configuration_profile_sid
43+
44+
return grant
1545

1646

1747
class AccessToken(object):
18-
def __init__(self, signing_key_sid, account_sid, secret, ttl=3600):
48+
def __init__(self, signing_key_sid, account_sid, secret,
49+
identity=None, ttl=3600):
1950
self.signing_key_sid = signing_key_sid
2051
self.account_sid = account_sid
2152
self.secret = secret
53+
54+
self.identity = identity
2255
self.ttl = ttl
2356
self.grants = []
2457

25-
def add_grant(self, resource, actions=ALL):
26-
if not isinstance(actions, list):
27-
actions = [actions]
28-
29-
self.grants.append({
30-
'res': resource,
31-
'act': actions,
32-
})
33-
return self
34-
35-
def add_rest_grant(self, uri, actions=ALL):
36-
resource = 'https://api.twilio.com/2010-04-01/Accounts/{0}/{1}'.format(
37-
self.account_sid,
38-
uri.lstrip('/'),
39-
)
40-
return self.add_grant(resource, actions)
41-
42-
def add_endpoint_grant(self, endpoint, actions=None):
43-
actions = actions or [CLIENT_LISTEN, CLIENT_INVITE]
44-
resource = 'sip:{0}@{1}.endpoint.twilio.com'.format(
45-
endpoint,
46-
self.account_sid
47-
)
48-
return self.add_grant(resource, actions)
49-
50-
def enable_nts(self):
51-
return self.add_rest_grant('/Tokens.json', HTTP_POST)
52-
53-
def to_jwt(self):
58+
def add_grant(self, grant):
59+
self.grants.append(grant)
60+
61+
def to_jwt(self, algorithm='HS256'):
5462
now = int(time.time())
5563
headers = {
56-
"cty": "twilio-sat;v=1"
64+
"typ": "JWT",
65+
"cty": "twilio-sat;v=2"
5766
}
67+
68+
grants = {}
69+
if self.identity:
70+
grants["identity"] = self.identity
71+
72+
for grant in self.grants:
73+
grants[grant.key] = grant.to_payload()
74+
5875
payload = {
5976
"jti": '{0}-{1}'.format(self.signing_key_sid, now),
6077
"iss": self.signing_key_sid,
6178
"sub": self.account_sid,
6279
"nbf": now,
6380
"exp": now + self.ttl,
64-
"grants": self.grants
81+
"grants": grants
6582
}
6683

6784
return jwt.encode(payload, self.secret, headers=headers)

0 commit comments

Comments
 (0)
0