-
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.
twilio python changes for orgs api uptake
- Loading branch information
Showing
16 changed files
with
202 additions
and
63 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,21 @@ | ||
from twilio.authStrategy.authType import AuthType | ||
from enum import Enum | ||
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 | ||
|
||
def get_auth_string(self) -> str: | ||
"""Return the authentication string.""" | ||
pass | ||
|
||
@abstractmethod | ||
def requires_authentication(self) -> bool: | ||
"""Return True if authentication is required, else False.""" | ||
pass |
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,11 @@ | ||
from enum import Enum | ||
|
||
class AuthType(Enum): | ||
TOKEN = 'token' | ||
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,17 @@ | ||
import base64 | ||
from enum import Enum | ||
|
||
|
||
class BasicAuthStrategy(AuthStrategy): | ||
def __init__(self, username: str, password: str): | ||
super().__init__(AuthType.BASIC) | ||
self.username = username | ||
self.password = password | ||
|
||
def get_auth_string(self) -> str: | ||
credentials = f"{self.username}:{self.password}" | ||
encoded = base64.b64encode(credentials.encode('ascii')).decode('ascii') | ||
return f"Basic {encoded}" | ||
|
||
def requires_authentication(self) -> bool: | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from auth_type import AuthType | ||
|
||
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,35 @@ | ||
import jwt | ||
import threading | ||
from datetime import datetime, timedelta | ||
|
||
from twilio.authStrategy.authType import AuthType | ||
from twilio.authStrategy.authStrategy import AuthStrategy | ||
from twilio.http.token_manager import TokenManager | ||
|
||
|
||
class TokenAuthStrategy(AuthStrategy): | ||
def __init__(self, token_manager: TokenManager): | ||
super().__init__(AuthType.TOKEN) | ||
self.token_manager = token_manager | ||
self.token = None | ||
self.lock = threading.Lock() | ||
|
||
def get_auth_string(self) -> str: | ||
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.token = self.token_manager.fetch_access_token() | ||
|
||
def is_token_expired(self, token): | ||
decoded_jwt = jwt.decode(token, options={"verify_signature": True}) | ||
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() |
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
Oops, something went wrong.