8000 Allow generating Jwt without nbf · thecodeflash/twilio-python@b8b3be6 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8b3be6

Browse files
author
Evan Fossier
committed
Allow generating Jwt without nbf
1 parent c832931 commit b8b3be6

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

tests/unit/jwt/test_jwt.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
class DummyJwt(Jwt):
1212
"""Jwt implementation that allows setting arbitrary payload and headers for testing."""
13-
def __init__(self, secret_key, issuer, subject=None, algorithm='HS256', nbf=None, ttl=3600,
14-
valid_until=None, headers=None, payload=None):
13+
def __init__(self, secret_key, issuer, subject=None, algorithm='HS256', nbf=Jwt.GENERATE,
14+
ttl=3600, valid_until=None, headers=None, payload=None):
1515
super(DummyJwt, self).__init__(
1616
secret_key=secret_key,
1717
issuer=issuer,
@@ -73,6 +73,18 @@ def test_encode_with_subject(self, time_mock):
7373
expected_payload={'iss': 'issuer', 'exp': 3600, 'nbf': 0, 'sub': 'subject'},
7474
)
7575

76+
@patch('time.time')
77+
def test_encode_without_nbf(self, time_mock):
78+
time_mock.return_value = 0.0
79+
80+
jwt = DummyJwt('secret_key', 'issuer', subject='subject', headers={}, payload={}, nbf=None)
81+
82+
self.assertJwtsEqual(
83+
jwt.to_jwt(), 'secret_key',
84+
expected_headers={'typ': 'JWT', 'alg': 'HS256'},
85+
expected_payload={'iss': 'issuer', 'exp': 3600, 'sub': 'subject'},
86+
)
87+
7688
@patch('time.time')
7789
def test_encode_custom_ttl(self, time_mock):
7890
time_mock.return_value = 0.0

twilio/jwt/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class JwtDecodeError(Exception):
2626

2727
class Jwt(object):
2828
"""Base class for building a Json Web Token"""
29-
def __init__(self, secret_key, issuer, subject=None, algorithm='HS256', nbf=None,
29+
GENERATE = object()
30+
31+
def __init__(self, secret_key, issuer, subject=None, algorithm='HS256', nbf=GENERATE,
3032
ttl=3600, valid_until=None):
3133
self.secret_key = secret_key
3234
""":type str: The secret used to encode the JWT"""
@@ -80,8 +82,12 @@ def payload(self):
8082

8183
payload = self._generate_payload().copy()
8284
payload['iss'] = self.issuer
83-
payload['nbf'] = self.nbf or int(time.time())
8485
payload['exp'] = int(time.time()) + self.ttl
86+
if self.nbf is not None:
87+
if self.nbf == self.GENERATE:
88+
payload['nbf'] = int(time.time())
89+
else:
90+
payload['nbf'] = self.nbf
8591
if self.valid_until:
8692
payload['exp'] = self.valid_until
8793
if self.subject:

twilio/jwt/access_token/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __str__(self):
2121
class AccessToken(Jwt):
2222
"""Access Token containing one or more AccessTokenGrants used to access Twilio Resources"""
2323
def __init__(self, account_sid, signing_key_sid, secret, grants=None,
24-
identity=None, nbf=None, ttl=3600, valid_until=None):
24+
identity=None, nbf=Jwt.GENERATE, ttl=3600, valid_until=None):
2525
grants = grants or []
2626
if any(not isinstance(g, AccessTokenGrant) for g in grants):
2727
raise ValueError('Grants must be instances of AccessTokenGrant.')

twilio/jwt/client/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
class ClientCapabilityToken(Jwt):
88
"""A token to control permissions with Twilio Client"""
99

10-
def __init__(self, account_sid, auth_token, nbf=None, ttl=3600, valid_until=None, **kwargs):
10+
def __init__(self, account_sid, auth_token, nbf=Jwt.GENERATE, ttl=3600, valid_until=None,
11+
**kwargs):
1112
"""
1213
:param str account_sid: The account sid to which this token is granted access.
1314
:param str auth_token: The secret key used to sign the token. Note, this auth token is not

twilio/jwt/taskrouter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, account_sid, auth_token, workspace_sid, channel_id, **kwargs)
2929
secret_key=auth_token,
3030
issuer=account_sid,
3131
algorithm='HS256',
32-
nbf=kwargs.get('nbf', None),
32+
nbf=kwargs.get('nbf', Jwt.GENERATE),
3333
ttl=kwargs.get('ttl', 3600),
3434
valid_until=kwargs.get('valid_until', None),
3535
)

0 commit comments

Comments
 (0)
0