-
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: oauth sdk implementation (#799)
* chore: oauth sdk implementation
- Loading branch information
1 parent
6324a1c
commit b4c5734
Showing
14 changed files
with
406 additions
and
3 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
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,24 @@ | ||
from twilio.http.token_manager_initializer import TokenManagerInitializer | ||
|
||
# Dynamic import utility function | ||
def dynamic_import(module_name, class_name): | ||
from importlib import import_module | ||
module = import_module(module_name) | ||
return getattr(module, class_name) | ||
|
||
class OauthTokenBase: | ||
def get_oauth_token(self, domain: str, version: str, username: str, password: str): | ||
Domain = dynamic_import("twilio.base.domain", "Domain") | ||
Version = dynamic_import("twilio.base.version", "Version") | ||
BearerTokenHTTPClient = dynamic_import("twilio.http.bearer_token_http_client", "BearerTokenHTTPClient") | ||
OrgTokenManager = dynamic_import("twilio.http.orgs_token_manager", "OrgTokenManager") | ||
Client = dynamic_import("twilio.rest", "Client") | ||
try: | ||
orgs_token_manager = TokenManagerInitializer.get_token_manager() | ||
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version)) | ||
except Exception: | ||
orgs_token_manager = OrgTokenManager(grant_type='client_credentials', | ||
client_id=username, | ||
client_secret=password) | ||
TokenManagerInitializer().set_token_manager(orgs_token_manager) | ||
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version)) |
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,30 @@ | ||
import datetime | ||
import jwt | ||
|
||
from twilio.base.version import Version | ||
from twilio.http.token_manager import TokenManager | ||
from twilio.twilio_bearer_token_auth import TwilioBearerTokenAuth | ||
|
||
|
||
class BearerTokenHTTPClient: | ||
def __init__(self, orgs_token_manager: TokenManager): | ||
self.orgs_token_manager = orgs_token_manager | ||
|
||
def get_access_token(self, version: Version): | ||
if TwilioBearerTokenAuth.get_access_token() is None or self.is_token_expired( | ||
TwilioBearerTokenAuth.get_access_token() | ||
): | ||
access_token = self.orgs_token_manager.fetch_access_token(version) | ||
TwilioBearerTokenAuth.init(access_token) | ||
else: | ||
access_token = TwilioBearerTokenAuth.get_access_token() | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class NoAuthHTTPClient: | ||
def get_headers(self): | ||
headers = {} | ||
return headers |
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,42 @@ | ||
from twilio.base.version import Version | ||
from twilio.http.token_manager import TokenManager | ||
from twilio.rest.preview_iam.organizations.token import TokenList | ||
|
||
|
||
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 | ||
|
||
def fetch_access_token(self, version: Version): | ||
token_list = TokenList(version) | ||
token_instance = token_list.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 |
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,16 @@ | ||
from twilio.http.token_manager import TokenManager | ||
|
||
|
||
class TokenManagerInitializer: | ||
|
||
org_token_manager = None | ||
|
||
@classmethod | ||
def set_token_manager(cls, token_manager: TokenManager): | ||
cls.org_token_manager = token_manager | ||
|
||
@classmethod | ||
def get_token_manager(cls): | ||
if cls.org_token_manager is None: | ||
raise Exception('Token Manager not initialized') | ||
return cls.org_token_manager |
Oops, something went wrong.