-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Organizations Api uptake for twilio-python (#815)
* feat: oauth sdk implementation and organization api uptake (#799)
- Loading branch information
Showing
25 changed files
with
2,899 additions
and
16 deletions.
There are no files selected for viewing
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 |
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
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,13 @@ | ||
from twilio.auth_strategy.auth_type import AuthType | ||
|
||
|
||
class CredentialProvider: | ||
def __init__(self, auth_type: AuthType): | ||
self._auth_type = auth_type | ||
|
||
@property | ||
def auth_type(self) -> AuthType: | ||
return self._auth_type | ||
|
||
def to_auth_strategy(self): | ||
raise NotImplementedError("Subclasses must implement this method") |
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,28 @@ | ||
from twilio.http.orgs_token_manager import OrgTokenManager | ||
from twilio.base.exceptions import TwilioException | ||
from twilio.credential.credential_provider import CredentialProvider | ||
from twilio.auth_strategy.auth_type import AuthType | ||
from twilio.auth_strategy.token_auth_strategy import TokenAuthStrategy | ||
|
||
|
||
class OrgsCredentialProvider(CredentialProvider): | ||
def __init__(self, client_id: str, client_secret: str, token_manager=None): | ||
super().__init__(AuthType.CLIENT_CREDENTIALS) | ||
|
||
if client_id is None or client_secret is None: | ||
raise TwilioException("Client id and Client secret are mandatory") | ||
|
||
self.grant_type = "client_credentials" | ||
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 | ||
) | ||
if self.auth_strategy is None: | ||
self.auth_strategy = TokenAuthStrategy(self.token_manager) | ||
return self.auth_strategy |
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,41 @@ | ||
from twilio.http.token_manager import TokenManager | ||
from twilio.rest import Client | ||
|
||
|
||
class OrgTokenManager(TokenManager): | ||
""" | ||
Orgs Token Manager | ||
""" | ||
|
||
def __init__( | ||
self, | ||
grant_type: str, | ||
client_id: str, | ||
client_secret: str, | ||
code: str = None, | ||
redirect_uri: str = None, | ||
audience: str = None, | ||
refreshToken: str = None, | ||
scope: str = None, | ||
): | ||
self.grant_type = grant_type | ||
self.client_id = client_id | ||
self.client_secret = client_secret | ||
self.code = code | ||
self.redirect_uri = redirect_uri | ||
self.audience = audience | ||
self.refreshToken = refreshToken | ||
self.scope = scope | ||
self.client = Client() | ||
|
||
def fetch_access_token(self): | ||
token_instance = self.client.preview_iam.v1.token.create( | ||
grant_type=self.grant_type, | ||
client_id=self.client_id, | ||
client_secret=self.client_secret, | ||
code=self.code, | ||
redirect_uri=self.redirect_uri, | ||
audience=self.audience, | ||
scope=self.scope, | ||
) | ||
return token_instance.access_token |
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,7 @@ | ||
from twilio.base.version import Version | ||
|
||
|
||
class TokenManager: | ||
|
||
def fetch_access_token(self, version: Version): | ||
pass |
Oops, something went wrong.