8000 Merge pull request #39 from twilio/remove-nbf · randy3465/twilio-python@5f2d9c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f2d9c1

Browse files
committed
Merge pull request twilio#39 from twilio/remove-nbf
Make nbf optional in access tokens
2 parents eb84973 + ab9b121 commit 5f2d9c1

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

tests/test_access_token.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import time
12
import unittest
23

4+
from datetime import datetime
35
from nose.tools import assert_equal
46
from twilio.jwt import decode
57
from twilio.access_token import AccessToken, ConversationsGrant, IpMessagingGrant
@@ -13,18 +15,27 @@ def assert_is_not_none(obj):
1315
assert obj is not None, '%r is None' % obj
1416

1517

18+
def assert_in(obj1, obj2):
19+
assert obj1 in obj2, '%r is not in %r' % (obj1, obj2)
20+
21+
22+
def assert_greater_equal(obj1, obj2):
23+
assert obj1 > obj2, '%r is not greater than or equal to %r' % (obj1, obj2)
24+
25+
1626
class AccessTokenTest(unittest.TestCase):
1727
def _validate_claims(self, payload):
1828
assert_equal(SIGNING_KEY_SID, payload['iss'])
1929
assert_equal(ACCOUNT_SID, payload['sub'])
20-
assert_is_not_none(payload['nbf'])
30+
2131
assert_is_not_none(payload['exp'])
22-
assert_equal(payload['nbf'] + 3600, payload['exp'])
2332
assert_is_not_none(payload['jti'])
24-
assert_equal('{0}-{1}'.format(payload['iss'], payload['nbf']),
25-
payload['jti'])
2633
assert_is_not_none(payload['grants'])
2734

35+
assert_greater_equal(payload['exp'], int(time.time()))
36+
37+
assert_in(payload['iss'], payload['jti'])
38+
2839
def test_empty_grants(self):
2940
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
3041
token = str(scat)
@@ -34,27 +45,53 @@ def test_empty_grants(self):
3445
self._validate_claims(payload)
3546
assert_equal({}, payload['grants'])
3647

48+
def test_nbf(self):
49+
now = int(time.mktime(datetime.now().timetuple()))
50+
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret', nbf=now)
51+
token = str(scat)
52+
53+
assert_is_not_none(token)
54+
payload = decode(token, 'secret')
55+
self._validate_claims(payload)
56+
assert_equal(now, payload['nbf'])
57+
58+
def test_identity(self):
59+
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret', identity='test@twilio.com')
60+
token = str(scat)
61+
62+
assert_is_not_none(token)
63+
payload = decode(token, 'secret')
64+
self._validate_claims(payload)
65+
assert_equal({
66+
'identity': 'test@twilio.com'
67+
}, payload['grants'])
68+
3769
def test_conversations_grant(self):
3870
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
39-
scat.add_grant(ConversationsGrant())
71+
scat.add_grant(ConversationsGrant(configuration_profile_sid='CP123'))
4072

4173
token = str(scat)
4274
assert_is_not_none(token)
4375
payload = decode(token, 'secret')
4476
self._validate_claims(payload)
4577
assert_equal(1, len(payload['grants']))
46-
assert_equal({}, payload['grants']['rtc'])
78+
assert_equal({
79+
'configuration_profile_sid': 'CP123'
80+
}, payload['grants']['rtc'])
4781

4882
def test_ip_messaging_grant(self):
4983
scat = AccessToken(< 8000 span class=pl-c1>ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
50-
scat.add_grant(IpMessagingGrant())
84+
scat.add_grant(IpMessagingGrant(service_sid='IS123', push_credential_sid='CR123'))
5185

5286
token = str(scat)
5387
assert_is_not_none(token)
5488
payload = decode(token, 'secret')
5589
self._validate_claims(payload)
5690
assert_equal(1, len(payload['grants']))
57-
assert_equal({}, payload['grants']['ip_messaging'])
91+
assert_equal({
92+
'service_sid': 'IS123',
93+
'push_credential_sid': 'CR123'
94+
}, payload['grants']['ip_messaging'])
5895

5996
def test_grants(self):
6097
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')

twilio/access_token.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
class IpMessagingGrant(object):
66
""" Grant to access Twilio IP Messaging """
77
def __init__(self, service_sid=None, endpoint_id=None,
8-
role_sid=None, credential_sid=None):
8+
deployment_role_sid=None, push_credential_sid=None):
99
self.service_sid = service_sid
1010
self.endpoint_id = endpoint_id
11-
self.deployment_role_sid = role_sid
12-
self.push_credential_sid = credential_sid
11+
self.deployment_role_sid = deployment_role_sid
12+
self.push_credential_sid = push_credential_sid
1313

1414
@property
1515
def key(self):
@@ -49,13 +49,14 @@ def to_payload(self):
4949
class AccessToken(object):
5050
""" Access Token used to access Twilio Resources """
5151
def __init__(self, account_sid, signing_key_sid, secret,
52-
identity=None, ttl=3600):
52+
identity=None, ttl=3600, nbf=None):
5353
self.account_sid = account_sid
5454
self.signing_key_sid = signing_key_sid
5555
self.secret = secret
5656

5757
self.identity = identity
5858
self.ttl = ttl
59+
self.nbf = nbf
5960
self.grants = []
6061

6162
def add_grant(self, grant):
@@ -79,11 +80,13 @@ def to_jwt(self, algorithm='HS256'):
7980
"jti": '{0}-{1}'.format(self.signing_key_sid, now),
8081
"iss": self.signing_key_sid,
8182
"sub": self.account_sid,
82-
"nbf": now,
8383
"exp": now + self.ttl,
8484
"grants": grants
8585
}
8686

87+
if self.nbf is not None:
88+
payload['nbf'] = self.nbf
89+
8790
return jwt.encode(payload, self.secret, headers=headers,
8891
algorithm=algorithm)
8992

0 commit comments

Comments
 (0)
0