Skip to content

Commit 35b5015

Browse files
committed
Uptake of review comments
1 parent ceebd46 commit 35b5015

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

twilio/auth_strategy/token_auth_strategy.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def __init__(self, token_manager: TokenManager):
1818
self.logger = logging.getLogger(__name__)
1919

2020
def get_auth_string(self) -> str:
21-
if self.token is None:
22-
self.fetch_token()
21+
self.fetch_token()
2322
return f"Bearer {self.token}"
2423

2524
def requires_authentication(self) -> bool:
@@ -28,15 +27,23 @@ def requires_authentication(self) -> bool:
2827
def fetch_token(self):
2928
self.logger.info("New token fetched for accessing organization API")
3029
if self.token is None or self.token == "" or self.is_token_expired(self.token):
31-
with self.lock:
30+
with self.lock:
3231
if self.token is None or self.token == "" or self.is_token_expired(self.token):
3332
self.token = self.token_manager.fetch_access_token()
3433

3534
def is_token_expired(self, token):
36-
print(f'token is {token}')
37-
decoded_jwt = jwt.decode(token, options={"verify_signature": True}, algorithms=["RS256"])
38-
expires_at = decoded_jwt.get("exp")
39-
# Add a buffer of 30 seconds
40-
buffer_seconds = 30
41-
buffer_expires_at = expires_at - buffer_seconds
42-
return buffer_expires_at < datetime.datetime.now().timestamp()
35+
try:
36+
decoded = jwt.decode(token, options={"verify_signature": False})
37+
exp = decoded.get('exp')
38+
39+
if exp is None:
40+
return True # No expiration time present, consider it expired
41+
42+
# Check if the expiration time has passed
43+
return datetime.fromtimestamp(exp) < datetime.utcnow()
44+
45+
except jwt.DecodeError:
46+
return True # Token is invalid
47+
except Exception as e:
48+
print(f"An error occurred: {e}")
49+
return True

twilio/base/client_base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ def request(
9696
if self.credential_provider:
9797
auth_strategy = self.credential_provider.to_auth_strategy()
9898
headers["Authorization"] = auth_strategy.get_auth_string()
99-
else:
99+
elif self.username is not None and self.password is not None:
100100
auth = self.get_auth(auth)
101+
else:
102+
auth = None
101103

102104

103105
uri = self.get_hostname(uri)
@@ -150,11 +152,14 @@ async def request_async(
150152

151153
##If credential provider is provided by user, get the associated auth strategy
152154
##Using the auth strategy, fetch the auth string and set it to authorization header
155+
153156
if self.credential_provider:
154157
auth_strategy = self.credential_provider.to_auth_strategy()
155158
headers["Authorization"] = auth_strategy.get_auth_string()
156-
else:
159+
elif self.username is not None and self.password is not None:
157160
auth = self.get_auth(auth)
161+
else:
162+
auth = None
158163

159164
uri = self.get_hostname(uri)
160165

twilio/credential/orgs_credential_provider.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ def __init__(self, client_id: str, client_secret: str, token_manager=None):
1818
self.client_id = client_id
1919
self.client_secret = client_secret
2020
self.token_manager = token_manager
21+
self.auth_strategy = None
2122

2223
def to_auth_strategy(self):
2324
if self.token_manager is None:
2425
self.token_manager = OrgTokenManager(self.grant_type, self.client_id, self.client_secret)
25-
26-
return TokenAuthStrategy(self.token_manager)
26+
if self.auth_strategy is None:
27+
self.auth_strategy = TokenAuthStrategy(self.token_manager)
28+
return self.auth_strategy

twilio/http/http_client.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ def request(
7878
elif timeout <= 0:
7979
raise ValueError(timeout)
8080

81-
if headers:
82-
if "Requires-Authentication" in headers:
83-
headers.pop("Requires-Authentication", None)
84-
auth = None
85-
8681
kwargs = {
8782
"method": method.upper(),
8883
"url": url,
@@ -96,6 +91,7 @@ def request(
9691
else:
9792
kwargs["data"] = data
9893
self.log_request(kwargs)
94+
print(f'args : {kwargs}')
9995
self._test_only_last_response = None
10096
session = self.session or Session()
10197
request = Request(**kwargs)

twilio/rest/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def __init__(
136136
self._events: Optional["Events"] = None
137137
self._flex_api: Optional["FlexApi"] = None
138138
self._frontline_api: Optional["FrontlineApi"] = None
139+
self._iam: Optional["Iam"] = None
139140
self._preview_iam: Optional["PreviewIam"] = None
140141
self._insights: Optional["Insights"] = None
141142
self._intelligence: Optional["Intelligence"] = None

0 commit comments

Comments
 (0)