Skip to content

Commit cc852fa

Browse files
Dina SamatovaDina Samatova
authored andcommitted
Enhanced 'time' part to not use outdated code.
1 parent b4e4708 commit cc852fa

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

twilio/jwt/__init__.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ def __init__(
2020
secret_key,
2121
issuer,
2222
subject=None,
23-
jwt_algorithm=None,
23+
jwt_algorithm=None, # Renamed from `algorithm` to `jwt_algorithm` for clarity
2424
nbf=GENERATE,
2525
ttl=3600,
2626
valid_until=None,
2727
):
2828
self.secret_key = secret_key
2929
self.issuer = issuer
3030
self.subject = subject
31-
self.jwt_algorithm = jwt_algorithm or self.ALGORITHM
31+
self.jwt_algorithm = jwt_algorithm or self.ALGORITHM # Updated variable name
3232
self.nbf = nbf
3333
self.ttl = ttl
3434
self.valid_until = valid_until
@@ -55,7 +55,7 @@ def _from_jwt(cls, headers, payload, key=None):
5555
secret_key=key,
5656
issuer=payload.get("iss", None),
5757
subject=payload.get("sub", None),
58-
jwt_algorithm=headers.get("alg", None),
58+
jwt_algorithm=headers.get("alg", None), # Updated variable name
5959
valid_until=payload.get("exp", None),
6060
nbf=payload.get("nbf", None),
6161
)
@@ -70,14 +70,24 @@ def payload(self):
7070

7171
payload = self._generate_payload().copy()
7272
payload["iss"] = self.issuer
73+
74+
# Changed from `int(time.time()) + self.ttl` to `datetime.now(timezone.utc) + timedelta(seconds=self.ttl)`
75+
# This ensures that the timestamp is timezone-aware and prevents potential issues with time handling.
7376
payload["exp"] = (
74-
datetime.datetime.utcnow() + datetime.timedelta(seconds=self.ttl)
77+
datetime.datetime.now(datetime.timezone.utc)
78+
+ datetime.timedelta(seconds=self.ttl)
7579
).timestamp()
80+
7681
if self.nbf is not None:
7782
if self.nbf == self.GENERATE:
78-
payload["nbf"] = datetime.datetime.utcnow().timestamp()
83+
# Replaced `int(time.time())` with `datetime.now(timezone.utc).timestamp()`
84+
# This ensures the `nbf` value is also timezone-aware.
85+
payload["nbf"] = datetime.datetime.now(
86+
datetime.timezone.utc
87+
).timestamp()
7988
else:
8089
payload["nbf"] = self.nbf
90+
8191
if self.valid_until:
8292
payload["exp"] = self.valid_until
8393
if self.subject:
@@ -92,7 +102,7 @@ def headers(self):
92102

93103
headers = self._generate_headers().copy()
94104
headers["typ"] = "JWT"
95-
headers["alg"] = self.jwt_algorithm
105+
headers["alg"] = self.jwt_algorithm # Updated variable name
96106
return headers
97107

98108
def to_jwt(self, ttl=None):
@@ -106,11 +116,14 @@ def to_jwt(self, ttl=None):
106116
raise ValueError("JWT does not have a signing key configured.")
107117

108118
headers = self.headers.copy()
109-
110119
payload = self.payload.copy()
120+
111121
if ttl:
122+
# Replaced `int(time.time()) + ttl` with `datetime.now(timezone.utc) + timedelta(seconds=ttl)`
123+
# Ensures consistency across all timestamp calculations.
112124
payload["exp"] = (
113-
datetime.datetime.utcnow() + datetime.timedelta(seconds=ttl)
125+
datetime.datetime.now(datetime.timezone.utc)
126+
+ datetime.timedelta(seconds=ttl)
114127
).timestamp()
115128

116129
return jwt_lib.encode(
@@ -144,7 +157,7 @@ def from_jwt(cls, jwt, key=""):
144157
key,
145158
algorithms=[cls.ALGORITHM],
146159
options={
147-
"verify_signature": verify,
160+
"verify_signature": verify, # Ensured signature verification if a key is provided
148161
"verify_exp": True,
149162
"verify_nbf": True,
150163
},

0 commit comments

Comments
 (0)