-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into snyk-fix-445944dcea8926350b116120651ec59c
- Loading branch information
Showing
532 changed files
with
21,507 additions
and
5,614 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
name: Bug report | ||
description: Report a bug with the twilio-python helper library. | ||
title: "[BUG] Describe the issue briefly" | ||
labels: "type: bug" | ||
body: | ||
- type: markdown | ||
attributes: | ||
value: | | ||
Thank you for reporting a bug in the twilio-python helper library. Please provide the details below to help us investigate and resolve the issue. | ||
- type: textarea | ||
attributes: | ||
label: Describe the bug | ||
description: Provide a clear and concise description of the issue. | ||
placeholder: A clear and concise description of the bug. | ||
validations: | ||
required: true | ||
|
||
- type: textarea | ||
attributes: | ||
label: Code snippet | ||
description: Provide the code snippet that reproduces the issue. | ||
placeholder: "```\n// Code snippet here\n```" | ||
validations: | ||
required: true | ||
|
||
- type: textarea | ||
attributes: | ||
label: Actual behavior | ||
description: Describe what actually happened. | ||
placeholder: A description of the actual behavior. | ||
validations: | ||
required: true | ||
|
||
- type: textarea | ||
attributes: | ||
label: Expected behavior | ||
description: Describe what you expected to happen. | ||
placeholder: A description of the expected outcome. | ||
validations: | ||
required: true | ||
|
||
- type: input | ||
attributes: | ||
label: twilio-python version | ||
description: Specify the version of the twilio-python helper library you are using. | ||
placeholder: e.g., 9.4.1 | ||
validations: | ||
required: true | ||
|
||
- type: input | ||
attributes: | ||
label: Python version | ||
description: Specify the version of Python you are using. | ||
placeholder: e.g., 3.9.1 | ||
validations: | ||
required: true | ||
|
||
- type: textarea | ||
attributes: | ||
label: Logs or error messages | ||
description: Provide relevant logs or error messages (if any). | ||
placeholder: "Error: Something went wrong..." | ||
|
||
- type: textarea | ||
attributes: | ||
label: Additional context | ||
description: Add any other context about the problem here. | ||
placeholder: Any additional |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
|
||
from twilio.rest import Client | ||
from twilio.credential.orgs_credential_provider import OrgsCredentialProvider | ||
|
||
ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID") | ||
API_KEY = os.environ.get("TWILIO_API_KEY") | ||
API_SECRET = os.environ.get("TWILIO_API_SECRET") | ||
|
||
CLIENT_ID = os.environ.get("TWILIO_CLIENT_ID") | ||
CLIENT_SECRET = os.environ.get("CLIENT_SECRET") | ||
ORGS_SID = os.environ.get("ORGS_SID") | ||
|
||
|
||
def example(): | ||
""" | ||
Some example usage of using organization resources | ||
""" | ||
self.client = Client( | ||
account_sid=ACCOUNT_SID, | ||
credential_provider=OrgsCredentialProvider(CLIENT_ID, CLIENT_SECRET), | ||
) | ||
|
||
accounts = self.client.preview_iam.organization( | ||
organization_sid=ORGS_SID | ||
).accounts.stream() | ||
for record in accounts: | ||
print(record) | ||
|
||
|
||
if __name__ == "__main__": | ||
example() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
|
||
from twilio.rest import Client | ||
from twilio.credential.client_credential_provider import ClientCredentialProvider | ||
|
||
ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID") | ||
API_KEY = os.environ.get("TWILIO_API_KEY") | ||
API_SECRET = os.environ.get("TWILIO_API_SECRET") | ||
FROM_NUMBER = os.environ.get("TWILIO_FROM_NUMBER") | ||
TO_NUMBER = os.environ.get("TWILIO_TO_NUMBER") | ||
|
||
CLIENT_ID = os.environ.get("TWILIO_CLIENT_ID") | ||
CLIENT_SECRET = os.environ.get("CLIENT_SECRET") | ||
|
||
|
||
def example(): | ||
""" | ||
Some example usage of message resources. | ||
""" | ||
self.client = Client( | ||
account_sid=ACCOUNT_SID, | ||
credential_provider=ClientCredentialProvider(CLIENT_ID, CLIENT_SECRET), | ||
) | ||
|
||
msg = self.client.messages.create( | ||
to=self.to_number, from_=self.from_number, body="hello world" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
example() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
__version_info__ = ("9", "3", "6") | ||
__version_info__ = ("9", "4", "3") | ||
__version__ = ".".join(__version_info__) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from twilio.auth_strategy.auth_type import AuthType | ||
from abc import abstractmethod | ||
|
||
|
||
class AuthStrategy(object): | ||
def __init__(self, auth_type: AuthType): | ||
self._auth_type = auth_type | ||
|
||
@property | ||
def auth_type(self) -> AuthType: | ||
return self._auth_type | ||
|
||
@abstractmethod | ||
def get_auth_string(self) -> str: | ||
"""Return the authentication string.""" | ||
|
||
@abstractmethod | ||
def requires_authentication(self) -> bool: | ||
"""Return True if authentication is required, else False.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from enum import Enum | ||
|
||
|
||
class AuthType(Enum): | ||
ORGS_TOKEN = "orgs_stoken" | ||
NO_AUTH = "noauth" | ||
BASIC = "basic" | ||
API_KEY = "api_key" | ||
CLIENT_CREDENTIALS = "client_credentials" | ||
|
||
def __str__(self): | ||
return self.value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from auth_type import AuthType | ||
from twilio.auth_strategy.auth_strategy import AuthStrategy | ||
|
||
|
||
class NoAuthStrategy(AuthStrategy): | ||
def __init__(self): | ||
super().__init__(AuthType.NO_AUTH) | ||
|
||
def get_auth_string(self) -> str: | ||
return "" | ||
|
||
def requires_authentication(self) -> bool: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import jwt | ||
import threading | ||
import logging | ||
from datetime import datetime | ||
|
||
from twilio.auth_strategy.auth_type import AuthType | ||
from twilio.auth_strategy.auth_strategy import AuthStrategy | ||
from twilio.http.token_manager import TokenManager | ||
|
||
|
||
class TokenAuthStrategy(AuthStrategy): | ||
def __init__(self, token_manager: TokenManager): | ||
super().__init__(AuthType.ORGS_TOKEN) | ||
self.token_manager = token_manager | ||
self.token = None | ||
self.lock = threading.Lock() | ||
logging.basicConfig(level=logging.INFO) | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def get_auth_string(self) -> str: | ||
self.fetch_token() | ||
return f"Bearer {self.token}" | ||
|
||
def requires_authentication(self) -> bool: | ||
return True | ||
|
||
def fetch_token(self): | ||
if self.token is None or self.token == "" or self.is_token_expired(self.token): | ||
with self.lock: | ||
if ( | ||
self.token is None | ||
or self.token == "" | ||
or self.is_token_expired(self.token) | ||
): | ||
self.logger.info("New token fetched for accessing organization API") | ||
self.token = self.token_manager.fetch_access_token() | ||
|
||
def is_token_expired(self, token): | ||
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 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 |
Oops, something went wrong.