Skip to content

Commit

Permalink
Uptake of review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Oct 1, 2024
1 parent 2959689 commit b973065
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 34 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from twilio.authStrategy.authType import AuthType
from twilio.auth_strategy.auth_type import AuthType
from enum import Enum
from abc import abstractmethod

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum

class AuthType(Enum):
TOKEN = 'token'
ORGS_TOKEN = 'orgs_stoken'
NO_AUTH = 'noauth'
BASIC = 'basic'
API_KEY = 'api_key'
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import jwt
import threading
import logging
from datetime import datetime, timedelta

from twilio.authStrategy.authType import AuthType
from twilio.authStrategy.authStrategy import AuthStrategy
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.TOKEN)
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:
if self.token is None:
Expand All @@ -23,7 +26,7 @@ def requires_authentication(self) -> bool:
return True

def fetch_token(self):
print(f'token is fetch_token {self.token}')
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:
if self.token is None or self.token == "" or self.is_token_expired(self.token):
Expand Down
12 changes: 5 additions & 7 deletions twilio/base/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from twilio.http import HttpClient
from twilio.http.http_client import TwilioHttpClient
from twilio.http.response import Response
from twilio.authStrategy.authType import AuthType
from twilio.credential.credentialProvider import CredentialProvider
from twilio.auth_strategy.auth_type import AuthType
from twilio.credential.credential_provider import CredentialProvider


class ClientBase(object):
Expand Down Expand Up @@ -93,7 +93,6 @@ def request(

##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
auth_strategy = None ##Initialization
if self.credential_provider:
auth_strategy = self.credential_provider.to_auth_strategy()
headers["Authorization"] = auth_strategy.get_auth_string()
Expand Down Expand Up @@ -151,15 +150,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
auth_strategy = None ##Initialization
if self.credential_provider:
auth_strategy = self.credential_provider.to_auth_strategy()
if auth_strategy.auth_type == AuthType.TOKEN:
auth_strategy.fetch_token()
headers["Authorization"] = auth_strategy.get_auth_string()
headers["Authorization"] = auth_strategy.get_auth_string()
else:
auth = self.get_auth(auth)

uri = self.get_hostname(uri)

return await self.http_client.request(
method,
uri,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from twilio.authStrategy.authType import AuthType
from twilio.auth_strategy.auth_type import AuthType

class CredentialProvider:
def __init__(self, auth_type: AuthType):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from twilio.http.orgs_token_manager import OrgTokenManager
from twilio.base.exceptions import TwilioException
from twilio.credential.credentialProvider import CredentialProvider
from twilio.authStrategy.authType import AuthType
from twilio.authStrategy.tokenAuthStrategy import TokenAuthStrategy
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):
Expand Down
16 changes: 0 additions & 16 deletions twilio/http/token_manager_initializer.py

This file was deleted.

2 changes: 1 addition & 1 deletion twilio/rest/preview_iam/organizations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, domain: Domain):
@property
def accounts(self) -> AccountList:
if self._accounts is None:
self._accounts = AccountList(self)
self._accounts = AccountList(self, "OR64adedc0f4dc99b9113305f725677b47")
return self._accounts


Expand Down

0 comments on commit b973065

Please sign in to comment.