8000 feat: Organizations Api uptake for twilio-python by AsabuHere · Pull Request #815 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

feat: Organizations Api uptake for twilio-python #815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b4c5734
feat: oauth sdk implementation (#799)
manisha1997 Jul 23, 2024
3e246e4
Python Orgs Api Changes
AsabuHere Sep 17, 2024
8395487
removing unwanted logs
AsabuHere Sep 17, 2024
bc5c16b
removing unwanted logs
AsabuHere Sep 17, 2024
a66f9e9
removing unwanted logs
AsabuHere Sep 17, 2024
b5a6490
removing unwanted logs
AsabuHere Sep 17, 2024
fac26ee
Fixing token fetch flow
AsabuHere Sep 17, 2024
15e15c0
twilio python changes for orgs api uptake
AsabuHere Sep 26, 2024
7b07ba7
twilio python changes for orgs api uptake
AsabuHere Sep 26, 2024
af11fd2
Update test_cluster.py
AsabuHere Sep 26, 2024
661785d
Update test_cluster.py
AsabuHere Sep 26, 2024
6a8c2d8
twilio python changes for orgs api uptake
AsabuHere Sep 26, 2024
1ba2f9b
twilio python changes for orgs api uptake
AsabuHere Sep 26, 2024
98708f0
twilio python changes for orgs api uptake
AsabuHere Sep 26, 2024
d78d5d5
twilio python changes for orgs api uptake
AsabuHere Sep 26, 2024
7bdf1b5
Merge branch 'main' into asabu_Python_changes
AsabuHere Sep 26, 2024
bc77770
twilio python changes for orgs api uptake
AsabuHere Sep 27, 2024
0211f23
twilio python changes for orgs api uptake
AsabuHere Sep 27, 2024
27dec32
twilio python changes for orgs api uptake
AsabuHere Sep 27, 2024
2959689
twilio python changes for orgs api uptake
AsabuHere Sep 27, 2024
b973065
Uptake of review comments
AsabuHere Oct 1, 2024
ceebd46
modified error messages
AsabuHere Oct 1, 2024
35b5015
Uptake of review comments
AsabuHere Oct 6, 2024
76fecab
Merge branch 'main' into asabu_Python_changes
AsabuHere Oct 6, 2024
e9eaa72
Organization api uptake changes
AsabuHere Dec 11, 2024
1c8420c
Organization api uptake changes
AsabuHere Dec 11, 2024
644f94b
Organization api uptake changes
AsabuHere Dec 11, 2024
66f3e28
Organization api uptake changes
AsabuHere Dec 11, 2024
88f6623
removing accept headers for delete operation
AsabuHere Dec 12, 2024
630c28c
Removing unwanted code
AsabuHere Dec 12, 2024
26ee4d3
Merge branch 'main' into asabu_Python_changes
AsabuHere Dec 12, 2024
ddb336b
Modified generated files
AsabuHere Dec 12, 2024
d79366e
removing unwamted logs
AsabuHere Dec 12, 2024
824ed9f
Formatting changes
AsabuHere Dec 12, 2024
3670f03
make prettier run
AsabuHere Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Uptake of review comments
  • Loading branch information
AsabuHere committed Oct 6, 2024
commit 35b5015909953ee62871e8ce03036fae647ee4df
27 changes: 17 additions & 10 deletions twilio/auth_strategy/token_auth_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
self.logger = logging.getLogger(__name__)

def get_auth_string(self) -> str:
if self.token is None:
self.fetch_token()
self.fetch_token()
return f"Bearer {self.token}"

def requires_authentication(self) -> bool:
Expand All @@ -28,15 +27,23 @@
def fetch_token(self):
self.logger.info("New token fetched for accessing organization API")
if self.token is None or self.token == "" or self.is_token_expired(self.token):
with self.lock:
with self.lock:
if self.token is None or self.token == "" or self.is_token_expired(self.token):
self.token = self.token_manager.fetch_access_token()

def is_token_expired(self, token):
print(f'token is {token}')
decoded_jwt = jwt.decode(token, options={"verify_signature": True}, algorithms=["RS256"])
expires_at = decoded_jwt.get("exp")
# Add a buffer of 30 seconds
buffer_seconds = 30
buffer_expires_at = expires_at - buffer_seconds
return buffer_expires_at < datetime.datetime.now().timestamp()
try:
decoded = jwt.decode(token, options={"verify_signature": False})
exp = decoded.get('exp')

if exp is None:
return True # No expiration time present, consider it expired

Check failure

Code scanning / SonarCloud

JWT should be signed and verified

<!--SONAR_ISSUE_KEY:AZO2vwAUbgEhLo82u4Au-->Don't use a JWT token without verifying its signature. <p>See more on <a href="https://sonarcloud.io/project/issues?id=twilio_twilio-python&issues=AZO2vwAUbgEhLo82u4Au&open=AZO2vwAUbgEhLo82u4Au&pullRequest=815">SonarQube Cloud</a></p>

Check failure

Code scanning / SonarCloud

JWT should be signed and verified High

Don't use a JWT token without verifying its signature. See more on SonarQube Cloud

# Check if the expiration time has passed
return datetime.fromtimestamp(exp) < datetime.utcnow()

except jwt.DecodeError:
return True # Token is invalid
except Exception as e:
print(f"An error occurred: {e}")
return True
9 changes: 7 additions & 2 deletions twilio/base/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ def request(
if self.credential_provider:
auth_strategy = self.credential_provider.to_auth_strategy()
headers["Authorization"] = auth_strategy.get_auth_string()
else:
elif self.username is not None and self.password is not None:
auth = self.get_auth(auth)
else:
auth = None


uri = self.get_hostname(uri)
Expand Down Expand Up @@ -150,11 +152,14 @@ async def request_async(

##If credential provider is provided by user, get the associated auth strategy
##Using the auth strategy, fetch the auth string and set it to authorization header

if self.credential_provider:
auth_strategy = self.credential_provider.to_auth_strategy()
headers["Authorization"] = auth_strategy.get_auth_string()
else:
elif self.username is not None and self.password is not None:
auth = self.get_auth(auth)
else:
auth = None

uri = self.get_hostname(uri)

Expand Down
6 changes: 4 additions & 2 deletions twilio/credential/orgs_credential_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ def __init__(self, client_id: str, client_secret: str, token_manager=None):
self.client_id = client_id
self.client_secret = client_secret
self.token_manager = token_manager
self.auth_strategy = None

def to_auth_strategy(self):
if self.token_manager is None:
self.token_manager = OrgTokenManager(self.grant_type, self.client_id, self.client_secret)

return TokenAuthStrategy(self.token_manager)
if self.auth_strategy is None:
self.auth_strategy = TokenAuthStrategy(self.token_manager)
return self.auth_strategy
6 changes: 1 addition & 5 deletions twilio/http/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ def request(
elif timeout <= 0:
raise ValueError(timeout)

if headers:
if "Requires-Authentication" in headers:
headers.pop("Requires-Authentication", None)
auth = None

kwargs = {
"method": method.upper(),
"url": url,
Expand All @@ -96,6 +91,7 @@ def request(
else:
kwargs["data"] = data
self.log_request(kwargs)
print(f'args : {kwargs}')
self._test_only_last_response = None
session = self.session or Session()
request = Request(**kwargs)
Expand Down
1 change: 1 addition & 0 deletions twilio/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def __init__(
self._events: Optional["Events"] = None
self._flex_api: Optional["FlexApi"] = None
self._frontline_api: Optional["FrontlineApi"] = None
self._iam: Optional["Iam"] = None
self._preview_iam: Optional["PreviewIam"] = None
self._insights: Optional["Insights"] = None
self._intelligence: Optional["Intelligence"] = None
Expand Down
Loading
0